-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovementController.cs
133 lines (81 loc) · 3.08 KB
/
MovementController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
[SerializeField]
float speed = 5f;
[SerializeField]
float lookSensitivity = 3f;
[SerializeField]
GameObject fpsCamera;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private float CameraUpAndDownRotation = 0f;
private float CurrentCameraUpAndDownRotation = 0f;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
//Calculate movement velocity as a 3D vector
float _xMovement = Input.GetAxis("Horizontal");
float _zMovement = Input.GetAxis("Vertical");
Vector3 _movementHorizontal = transform.right * _xMovement;
Vector3 _movementVertical = transform.forward * _zMovement;
//Final Movement vector
Vector3 _movementVelocity = (_movementHorizontal + _movementVertical).normalized * speed;
//Apply movement
Move(_movementVelocity);
if (Cursor.visible == true)
{
//transform.localEulerAngles = new Vector3(0f, 0f, 0f);
return;
}
//Calculate rotation as a 3D vector for turning around
float _yRotation = Input.GetAxis("Mouse X");
Vector3 _rotationVector = new Vector3(0,_yRotation,0)*lookSensitivity;
//Apply rotation
Rotate(_rotationVector);
//Calculate look up and down camera rotation
float _cameraUpDownRotation = Input.GetAxis("Mouse Y")*lookSensitivity;
//Apply camera rotation
RotateCamera(_cameraUpDownRotation);
}
protected void LateUpdate()
{
//transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y,0 );
}
void Move(Vector3 movementVelocity)
{
velocity = movementVelocity;
}
void Rotate(Vector3 rotationVector)
{
rotation = rotationVector;
}
void RotateCamera(float cameraUpAndDownRotation)
{
CameraUpAndDownRotation = cameraUpAndDownRotation;
}
//Run every physics iteration
private void FixedUpdate()
{
if (velocity!=Vector3.zero)
{
rb.MovePosition(rb.position+velocity*Time.fixedDeltaTime);
}
rb.MoveRotation(rb.rotation*Quaternion.Euler(rotation));
if (fpsCamera!=null)
{
CurrentCameraUpAndDownRotation -= CameraUpAndDownRotation;
CurrentCameraUpAndDownRotation = Mathf.Clamp(CurrentCameraUpAndDownRotation, -85, 85);
fpsCamera.transform.localEulerAngles = new Vector3(CurrentCameraUpAndDownRotation,0,0);
//transform.localEulerAngles = new Vector3(CurrentCameraUpAndDownRotation,transform.eulerAngles.y,transform.eulerAngles.z);
}
}
}