-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFPSMouseLook.cs
119 lines (93 loc) · 2.98 KB
/
FPSMouseLook.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("Control Script/FPSMouseLook")]
public class FPSMouseLook : MonoBehaviour
{
Rigidbody body;
//enum storing mouse axis
public enum RotationAxes
{
MouseXAndY = 0,
MouseX = 1,
MouseY = 2
}
//enum instance
public RotationAxes axes = RotationAxes.MouseXAndY;
//sensitivy for horizontal and vertical rotation
[SerializeField] float sensitivityHor = 9.0f;
[SerializeField] float sensitivityVert = 9.0f;
//min and max for vertical rotation
[SerializeField] float minVert = -45.0f;
[SerializeField] float maxVert = 45.0f;
// vertical rotation vector component
private float _rotationX = 0;
//on start
void Start()
{
body = GetComponent<Rigidbody>();
//freezes rigid body
freezeBodyRotation();
}
//on update
void Update()
{
getMouseInput();
}
//freezes Rotation of rigidBody, pervents game physics from influencing rotation
private void freezeBodyRotation()
{
if (body != null)
{
body.freezeRotation = true;
}
}
//determines rotation axis and calls corresponding rotation function
private void getMouseInput()
{
//horizontal rotation
if (axes == RotationAxes.MouseX)
{
xMouseLook();
}
//vertical rotation
else if (axes == RotationAxes.MouseY)
{
yMouseLook();
}
//simultaneous horizontal and vertical rotation
else
{
xyMouseLook();
}
}
// rotates horizontally and vertically based on X and Y Mouse Input, and sensitivity settings
private void xyMouseLook()
{
// calculates vertical rotation
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
//limits vertical rotation between min and max
_rotationX = Mathf.Clamp(_rotationX, minVert, maxVert);
//calculates horiztonal rotation
float delta = Input.GetAxis("Mouse X") * sensitivityHor;
float rotationY = transform.localEulerAngles.y + delta;
//performs rotation
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
//rotates vertically based on Y Mouse input and vertical sensitivity setting
private void yMouseLook()
{
//calculates and limits vertical rotation
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
_rotationX = Mathf.Clamp(_rotationX, minVert, maxVert);
float rotationY = transform.localEulerAngles.y;
// performs vertical rotation
transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
}
//rotates horizontally based on X Mouse input and horizontal sensitivity setting
private void xMouseLook()
{
//performs horizontal rotation
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
}
}