-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerController.cs
More file actions
136 lines (106 loc) · 3.53 KB
/
PlayerController.cs
File metadata and controls
136 lines (106 loc) · 3.53 KB
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
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[Header("камера/camera")]
[SerializeField] private Camera _cameraComponent;
[Header("скорости/Speeds")]
[SerializeField] private float _walkSpeed = 10.0f;
[SerializeField] private float _runSpeed = 15.0f;
[SerializeField] private float _cameraSensitivity = 5.0f;
[SerializeField] private float _jumpHeight = 2.0f;
private float _speedCapacity;
private CharacterController _playerMovementComponent;
private float _moveX;
private float _moveZ;
private float _mouseX;
private float _mouseY;
private float _mouseVertical;
private bool _isGrounded;
private float _verticalVelocity;
private float _gravity = -9.0f;
private void Update()
{
if (!CheckScriptController()) return;
GetMovementController();
ApplyMovement();
GetRotateCameraController();
ApplyCameraRotation();
SpeedController();
GravityController();
JumpController();
}
private void GetMovementController()
{
_moveX = Input.GetAxis("Horizontal");
_moveZ = Input.GetAxis("Vertical");
}
private void ApplyMovement()
{
Vector3 MoveDirection = (transform.right * _moveX + transform.forward * _moveZ) * _speedCapacity;
MoveDirection.y = _verticalVelocity;
_playerMovementComponent.Move(MoveDirection * Time.deltaTime);
}
private void GetRotateCameraController()
{
_mouseX = Input.GetAxis("Mouse X") * _cameraSensitivity;
_mouseY = Input.GetAxis("Mouse Y") * _cameraSensitivity;
}
private void ApplyCameraRotation()
{
transform.Rotate(Vector3.up * _mouseX);
_mouseVertical -= _mouseY;
_mouseVertical = Mathf.Clamp(_mouseVertical, -90.0f, 90.0f);
_cameraComponent.transform.localRotation = Quaternion.Euler(_mouseVertical, 0.0f, 0.0f);
}
private void SpeedController()
{
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
_speedCapacity = _runSpeed;
else
_speedCapacity = _walkSpeed;
}
private bool CheckScriptController()
{
if (_cameraComponent == null)
{
Debug.Log("назначь камеру в инспекторе со скриптом!/assign a camera in the inspector with a script!");
return false;
}
if (_playerMovementComponent == null)
{
Debug.Log("назначь контроллер в инспекторе со скриптом!/assign a controller in the inspector with a script!");
return false;
}
return true;
}
private void GravityController()
{
_isGrounded = _playerMovementComponent.isGrounded;
if (_isGrounded)
{
if (_verticalVelocity < 0.0f)
_verticalVelocity = -2.0f;
}
_verticalVelocity += _gravity * Time.deltaTime;
}
private void JumpController()
{
if (Input.GetKeyDown(KeyCode.Space) && _isGrounded)
_verticalVelocity = Mathf.Sqrt(_jumpHeight * -2.0f * _gravity);
}
private void Awake()
{
ReceivingComponents();
CursorLocked();
}
private void ReceivingComponents()
{
_playerMovementComponent = GetComponent<CharacterController>();
_speedCapacity = _walkSpeed;
}
private void CursorLocked()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
}