-
Notifications
You must be signed in to change notification settings - Fork 1
/
PerspectiveCamera.cs
67 lines (51 loc) · 2.02 KB
/
PerspectiveCamera.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
using UnityEngine;
namespace Perspective
{
class PerspectiveCamera : CameraController
{
public float CameraSensitivity = 90;
public float ClimbSpeed = 4;
public float NormalMoveSpeed = 10;
private float _rotationX = 90;
private float _rotationY;
private CharacterController _cc;
void Start()
{
GetComponent<Camera>().farClipPlane = 150;
transform.position = new Vector3(1, 10, 1);
_cc = gameObject.AddComponent<CharacterController>();
_cc.radius = 0.1f;
_cc.height = 0.4f;
_cc.center = new Vector3(0, 0, 0);
}
void Update()
{
}
void FixedUpdate()
{
if (Input.GetMouseButton(2) || Input.GetKey(KeyCode.LeftControl))
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
_rotationX += Input.GetAxis("Mouse X") * CameraSensitivity * Time.fixedDeltaTime;
_rotationY += Input.GetAxis("Mouse Y") * CameraSensitivity * Time.fixedDeltaTime;
_rotationY = Mathf.Clamp(_rotationY, -90, 90);
}
else
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
transform.localRotation = Quaternion.AngleAxis(_rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(_rotationY, Vector3.left);
Vector3 pos = new Vector3();
pos += transform.forward * NormalMoveSpeed * (Input.GetAxis("Vertical") + Mathf.Clamp(-Input.GetAxis("Mouse ScrollWheel"), -2, 2));
pos += transform.right * NormalMoveSpeed * Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.Q))
pos += transform.up * ClimbSpeed;
if (Input.GetKey(KeyCode.E))
pos -= transform.up * ClimbSpeed;
_cc.Move(pos * Time.fixedDeltaTime);
}
}
}