-
Notifications
You must be signed in to change notification settings - Fork 0
/
FPSController.cs
184 lines (150 loc) · 5.04 KB
/
FPSController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class FPSController : MonoBehaviour {
//
// Editor vars
//
[Header("Player camera")]
public Camera playerCamera;
public Sprite aimImage;
public Sprite targetImage;
public float lookSpeed = 2.0f;
public float cameraZoomLookSpeed = 0.5f;
public float aimSpeed = 0.75f;
public float lookXLimit = 45.0f;
[Header("Player attributes")]
public float walkingSpeed = 7.5f;
public float cameraToggleSpeed = 2f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
[Header("Options")]
public bool canMove = true;
public bool canSprint = true;
public bool canCrouch = true;
//
// Private vars
//
CharacterController characterController;
private bool cameraToggle = false;
private bool gamePaused = false;
// Movement
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0;
// Camera Vars
private float zoomFOV = 20;
private float normalFOV = 60;
private float zoomIncreaseRate = 175f;
// FPS Controller
//
private FPSUIManager uiManager;
private FPSFootSteps footSteps;
void Start() {
characterController = GetComponent<CharacterController>();
uiManager = GetComponent<FPSUIManager>();
footSteps = GetComponent<FPSFootSteps>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update() {
TogglePauseMenu();
PlayerMove();
ToggleCamera();
}
// Public Helpers
//
// Private Functions
//
private void TogglePauseMenu() {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (!gamePaused) {
gamePaused = true;
uiManager.PauseGame();
} else {
gamePaused = false;
uiManager.UnpauseGame();
}
}
}
private void PlayerMove() {
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
// Press c to toggle crouch
bool isCrouching = Input.GetKey(KeyCode.C);
float curSpeedX = 0f;
float curSpeedY = 0f;
float playerSpeed = 0f;
// Can't move during paused game
canMove = !gamePaused;
if (canMove) {
if (isRunning) {
playerSpeed = runningSpeed;
} else {
playerSpeed = walkingSpeed;
}
curSpeedX = playerSpeed * Input.GetAxis("Vertical");
curSpeedY = playerSpeed * Input.GetAxis("Horizontal");
}
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
// Player Jump
if (Input.GetButton("Jump") && canMove && characterController.isGrounded) {
moveDirection.y = jumpSpeed;
} else {
moveDirection.y = movementDirectionY;
}
if (characterController.isGrounded && (moveDirection.x != 0 || moveDirection.z != 0)) {
footSteps.PlayStepClip(isRunning);
}
// Apply some gravity
if (!characterController.isGrounded) {
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
float cameraMoveSpeed = lookSpeed;
if (cameraToggle) {
cameraMoveSpeed = cameraZoomLookSpeed;
}
if (canMove) {
rotationX += -Input.GetAxis("Mouse Y") * cameraMoveSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * cameraMoveSpeed, 0);
}
}
private void ToggleCamera() {
if(Input.GetMouseButtonDown(1)) {
if (cameraToggle) {
DisableInGameCamera();
} else {
EnableInGameCamera();
}
}
// Smooth zoom
if (cameraToggle && playerCamera.fieldOfView >= zoomFOV) {
playerCamera.fieldOfView -= zoomIncreaseRate*Time.deltaTime;
} else if (!cameraToggle && playerCamera.fieldOfView <= normalFOV) {
playerCamera.fieldOfView += zoomIncreaseRate*Time.deltaTime;
}
}
IEnumerator WaitToggleCameraUI(bool enabled) {
yield return new WaitForSeconds(0.25f);
cameraUICanvas.SetActive(enabled);
}
private void EnableInGameCamera() {
StartCoroutine(WaitToggleCameraUI(true));
cameraToggle = true;
}
private void DisableInGameCamera() {
cameraUICanvas.SetActive(false); // disable right away
cameraToggle = false;
}
}