-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFPCamera.cs
More file actions
57 lines (45 loc) · 1.52 KB
/
FPCamera.cs
File metadata and controls
57 lines (45 loc) · 1.52 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPCamera : MonoBehaviour
{
public float horizontalSensitivity = 10.0f;
public float verticalSensitivity = 10.0f;
private float v;
private float h;
private float totalV;
private float totalH;
private Quaternion tempRotation;
public float clampAngle = 70.0f;
public Transform playerTransform;
void Start()
{
// lock cursor
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Read user input
h = horizontalSensitivity * -(Input.GetAxis("Mouse X"));
v = verticalSensitivity * Input.GetAxis("Mouse Y");
// Accumulate total rotation from all input to date
totalH = totalH -= h;
totalV = totalV -= v;
//clamp the camera rotation - limit rollover
totalV = Mathf.Clamp(totalV, -clampAngle, clampAngle);
//Rotate player controller to run to look at directin
playerTransform.rotation = Quaternion.Euler(totalV, totalH, 0.0f);
// Calculate the single Quaternion rotation necessary to get us here
tempRotation = Quaternion.Euler(totalV, totalH, 0.0f);
// Apply that single rotation to the camera (from the origin)
transform.rotation = tempRotation;
OutOfLock();
}
private void OutOfLock()
{
// escape key release of lock
if (Input.GetKeyDown(KeyCode.Escape)) {
Cursor.lockState = CursorLockMode.None;
}
}
}