-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyCamera.cs
137 lines (109 loc) · 4.67 KB
/
FlyCamera.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using UnityEngine;
public class FlyCamera : MonoBehaviour
{
/*
Writen by Alexwing 2021, modify Use it, edit it, steal it I don't care.
based in Windexglow
Simple flycam I made, since I couldn't find any others made public.
Made simple to use (drag and drop, done) for regular keyboard layout
Optional can anchor to terrain
WASD : basic movement
SHIFT : Makes camera accelerate
Mouse : Mouse look
Scroll wheel : Height over terrain
mouse click right: set lock/unlock movement
*/
[Tooltip("The terrain to follow")]
public Terrain anchorToTerrain;
private float marginToTerrain = 0;
[Tooltip("The min height of the terrain to fly over")]
public float minHeighToTerrain = 20f;
[Tooltip("The max height of the terrain to fly over")]
public float maxHeighToTerrain = 120f;
[Tooltip("Regular speed")]
public float mainSpeed = 2f;
[Tooltip("Rotation speed")]
public float rotationSpeed = 20f;
[Tooltip(" Multiplied by how long shift is held. Basically running.")]
public float shiftAdd = 125f;
[Tooltip("Maximum speed when holdin gshift")]
public float maxShift = 100f;
[Tooltip("How sensitive it with mouse")]
public float camSens = 0.25f;
private float totalRun = 1f;
public static bool lockMovement = false;
private void Awake()
{
marginToTerrain = ((maxHeighToTerrain - minHeighToTerrain) * 0.5f) + minHeighToTerrain;
}
private void Update()
{
Cursor.visible = lockMovement;
Cursor.lockState = lockMovement ? CursorLockMode.None : CursorLockMode.Locked;
if (!lockMovement)
{
// Mouse camera angle.
float h = Input.GetAxis("Mouse X") * rotationSpeed;
float v = Input.GetAxis("Mouse Y") * rotationSpeed;
Vector3 delta = new Vector3(h, v, 0f);
delta = new Vector3(-delta.y * camSens, delta.x * camSens, 0f);
delta = new Vector3(transform.eulerAngles.x + delta.x, transform.eulerAngles.y + delta.y, 0f);
transform.eulerAngles = delta;
// Keyboard commands
Vector3 p = GetBaseInput();
if (Input.GetKey(KeyCode.LeftShift))
{
totalRun += Time.deltaTime;
p = p * totalRun * shiftAdd;
p.x = Mathf.Clamp(p.x, -maxShift, maxShift * mainSpeed);
p.y = Mathf.Clamp(p.y, -maxShift, maxShift * mainSpeed);
p.z = Mathf.Clamp(p.z, -maxShift, maxShift * mainSpeed);
}
else
{
totalRun = Mathf.Clamp(mainSpeed * 0.5f, 1f, 1000f);
p = p * totalRun;
}
p = p * Time.deltaTime;
transform.Translate(p);
}
if (anchorToTerrain)
{
Vector3 newPosition = transform.position;
// Anchor to terrain.
float height = anchorToTerrain.SampleHeight(this.transform.position);
//block movement to terrain limits x and z
newPosition.x = Mathf.Clamp(newPosition.x, anchorToTerrain.transform.position.x, anchorToTerrain.transform.position.x + anchorToTerrain.terrainData.size.x);
newPosition.z = Mathf.Clamp(newPosition.z, anchorToTerrain.transform.position.z, anchorToTerrain.transform.position.z + anchorToTerrain.terrainData.size.z);
this.transform.position = new Vector3(newPosition.x, height + marginToTerrain, newPosition.z);
//mouse whell change marginToTerrain
marginToTerrain -= Input.GetAxis("Mouse ScrollWheel") * mainSpeed;
if (minHeighToTerrain > marginToTerrain)
{
marginToTerrain = minHeighToTerrain;
}
if (marginToTerrain > maxHeighToTerrain)
{
marginToTerrain = maxHeighToTerrain;
}
}
//lock movement with mouse click
if (Input.GetKeyDown(KeyCode.Escape) || Input.GetMouseButtonUp(1))
{
lockMovement = !lockMovement;
}
}
private Vector3 GetBaseInput()
{
// Returns the basic values, if it's 0 than it's not active.
Vector3 p_Velocity = new Vector3();
if (!lockMovement)
{
if (Input.GetKey(KeyCode.W)) p_Velocity += Vector3.forward;
if (Input.GetKey(KeyCode.S)) p_Velocity += Vector3.back;
if (Input.GetKey(KeyCode.A)) p_Velocity += Vector3.left;
if (Input.GetKey(KeyCode.D)) p_Velocity += Vector3.right;
}
return p_Velocity;
}
}