-
Notifications
You must be signed in to change notification settings - Fork 286
/
PlayerMotor.cs
86 lines (69 loc) · 1.8 KB
/
PlayerMotor.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
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera cam;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private float cameraRotationX = 0f;
private float currentCameraRotationX = 0f;
private Vector3 thrusterForce = Vector3.zero;
[SerializeField]
private float cameraRotationLimit = 85f;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
// Gets a movement vector
public void Move (Vector3 _velocity)
{
velocity = _velocity;
}
// Gets a rotational vector
public void Rotate(Vector3 _rotation)
{
rotation = _rotation;
}
// Gets a rotational vector for the camera
public void RotateCamera(float _cameraRotationX)
{
cameraRotationX = _cameraRotationX;
}
// Get a force vector for our thrusters
public void ApplyThruster (Vector3 _thrusterForce)
{
thrusterForce = _thrusterForce;
}
// Run every physics iteration
void FixedUpdate ()
{
PerformMovement();
PerformRotation();
}
//Perform movement based on velocity variable
void PerformMovement ()
{
if (velocity != Vector3.zero)
{
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}
if (thrusterForce != Vector3.zero)
{
rb.AddForce(thrusterForce * Time.fixedDeltaTime, ForceMode.Acceleration);
}
}
//Perform rotation
void PerformRotation ()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler (rotation));
if (cam != null)
{
// Set our rotation and clamp it
currentCameraRotationX -= cameraRotationX;
currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
//Apply our rotation to the transform of our camera
cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
}
}
}