-
Notifications
You must be signed in to change notification settings - Fork 286
/
PlayerController.cs
90 lines (68 loc) · 2.06 KB
/
PlayerController.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
using UnityEngine;
[RequireComponent(typeof(ConfigurableJoint))]
[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {
[SerializeField]
private float speed = 5f;
[SerializeField]
private float lookSensitivity = 3f;
[SerializeField]
private float thrusterForce = 1000f;
[Header("Spring settings:")]
[SerializeField]
private JointDriveMode jointMode = JointDriveMode.Position;
[SerializeField]
private float jointSpring = 20f;
[SerializeField]
private float jointMaxForce = 40f;
private PlayerMotor motor;
private ConfigurableJoint joint;
void Start ()
{
motor = GetComponent<PlayerMotor>();
joint = GetComponent<ConfigurableJoint>();
SetJointSettings(jointSpring);
}
void Update ()
{
//Calculate movement velocity as a 3D vector
float _xMov = Input.GetAxisRaw("Horizontal");
float _zMov = Input.GetAxisRaw("Vertical");
Vector3 _movHorizontal = transform.right * _xMov;
Vector3 _movVertical = transform.forward * _zMov;
// Final movement vector
Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;
//Apply movement
motor.Move(_velocity);
//Calculate rotation as a 3D vector (turning around)
float _yRot = Input.GetAxisRaw("Mouse X");
Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
//Apply rotation
motor.Rotate(_rotation);
//Calculate camera rotation as a 3D vector (turning around)
float _xRot = Input.GetAxisRaw("Mouse Y");
float _cameraRotationX = _xRot * lookSensitivity;
//Apply camera rotation
motor.RotateCamera(_cameraRotationX);
// Calculate the thrusterforce based on player input
Vector3 _thrusterForce = Vector3.zero;
if (Input.GetButton ("Jump"))
{
_thrusterForce = Vector3.up * thrusterForce;
SetJointSettings(0f);
} else
{
SetJointSettings(jointSpring);
}
// Apply the thruster force
motor.ApplyThruster(_thrusterForce);
}
private void SetJointSettings (float _jointSpring)
{
joint.yDrive = new JointDrive {
mode = jointMode,
positionSpring = _jointSpring,
maximumForce = jointMaxForce
};
}
}