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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
*
* Adventure Creator
* by Chris Burton, 2013-2014
*
* "ThirdPersonCamera.cs"
*
* This is attached to a scene-based camera, similar to GameCamera and GameCamera2D.
* It should not be a child of the Player, but instead scene-specific.
*
*/
using UnityEngine;
using System.Collections;
using AC;
public class GameCameraThirdPerson : _Camera
{
public enum RotationLock { Free, Locked };
public RotationLock spinLock = RotationLock.Free;
public RotationLock pitchLock = RotationLock.Locked;
public bool targetIsPlayer = true;
public Transform target;
public float horizontalOffset = 0f;
public float verticalOffset = 2f;
public float distance = 2f;
public bool allowMouseWheelZooming = false;
public bool detectCollisions = true;
public float minDistance = 1f;
public float maxDistance = 3f;
public bool toggleCursor = false;
public float spinSpeed = 5f;
public float spinAccleration = 5f;
public string spinAxis = "";
public bool invertSpin = false;
public bool alwaysBehind = false;
public float pitchSpeed = 3f;
public float pitchAccleration = 20f;
public float maxPitch = 40f;
public string pitchAxis = "";
public bool invertPitch = false;
private float collisionOffset = 0f;
private float deltaDistance = 0f;
private float deltaSpin = 0f;
private float deltaPitch = 0f;
private float roll = 0f;
private float spin = 0f;
private float pitch = 0f;
private StateHandler stateHandler;
private PlayerInput playerInput;
private void Awake ()
{
if (GameObject.FindWithTag (Tags.gameEngine) && GameObject.FindWithTag (Tags.gameEngine).GetComponent <PlayerInput>())
{
playerInput = GameObject.FindWithTag (Tags.gameEngine).GetComponent <PlayerInput>();
}
}
private void Start ()
{
ResetTarget ();
Vector3 angles = transform.eulerAngles;
roll = angles.x;
spin = angles.y;
if (GameObject.FindWithTag (Tags.persistentEngine) && GameObject.FindWithTag (Tags.persistentEngine).GetComponent <StateHandler>())
{
stateHandler = GameObject.FindWithTag (Tags.persistentEngine).GetComponent <StateHandler>();
}
}
public override void ResetTarget ()
{
if (targetIsPlayer && GameObject.FindWithTag (Tags.player) && GameObject.FindWithTag (Tags.player).GetComponent <Player>())
{
target = GameObject.FindWithTag (Tags.player).transform;
}
}
private void LateUpdate ()
{
if (detectCollisions && target != null)
{
Vector3 desiredPosition = target.position - (transform.rotation * Vector3.forward * distance + new Vector3 (0f, -verticalOffset, 0f)) + (transform.rotation * Vector3.right * horizontalOffset);
RaycastHit hit;
if (Physics.Linecast (target.position + new Vector3 (0, verticalOffset, 0f), desiredPosition, out hit))
{
collisionOffset = distance - hit.distance;
}
else
{
collisionOffset = 0f;
}
}
}
private void FixedUpdate ()
{
if (!target)
{
return;
}
if (stateHandler == null || stateHandler.gameState == AC.GameState.Normal)
{
if (allowMouseWheelZooming && minDistance < maxDistance)
{
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
deltaDistance = Mathf.Lerp (deltaDistance, Mathf.Min (spinSpeed, maxDistance - distance), spinAccleration/5f * Time.deltaTime);
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
deltaDistance = Mathf.Lerp (deltaDistance, -Mathf.Min (spinSpeed, distance - minDistance), spinAccleration/5f * Time.deltaTime);
}
else
{
deltaDistance = Mathf.Lerp (deltaDistance, 0f, spinAccleration * Time.deltaTime);
}
distance += deltaDistance;
distance = Mathf.Clamp (distance, minDistance, maxDistance);
}
Vector2 inputMovement = new Vector2 (Input.GetAxis (spinAxis), Input.GetAxis (pitchAxis));
if (playerInput && toggleCursor && !playerInput.CanMoveMouse ())
{
inputMovement = playerInput.freeAim;
}
if ((toggleCursor && (playerInput == null || !playerInput.CanMoveMouse ())) || !toggleCursor)
{
if (spinLock == RotationLock.Free)
{
if (inputMovement.x > 0f)
{
deltaSpin = Mathf.Lerp (deltaSpin, spinSpeed, spinAccleration * Time.deltaTime);
}
else if (inputMovement.x < 0f)
{
deltaSpin = Mathf.Lerp (deltaSpin, -spinSpeed, spinAccleration * Time.deltaTime);
}
else
{
deltaSpin = Mathf.Lerp (deltaSpin, 0f, spinAccleration * Time.deltaTime);
}
if (invertSpin)
{
spin -= deltaSpin;
}
else
{
spin += deltaSpin;
}
}
else if (alwaysBehind)
{
spin = Mathf.LerpAngle (spin, target.eulerAngles.y, spinAccleration * Time.deltaTime);
}
if (pitchLock == RotationLock.Free)
{
if (inputMovement.y > 0f)
{
deltaPitch = Mathf.Lerp (deltaPitch, -pitchSpeed, pitchAccleration * Time.deltaTime);
}
else if (inputMovement.y < 0f)
{
deltaPitch = Mathf.Lerp (deltaPitch, pitchSpeed, pitchAccleration * Time.deltaTime);
}
else
{
deltaPitch = Mathf.Lerp (deltaPitch, 0f, pitchAccleration * Time.deltaTime);
}
if (invertPitch)
{
pitch += deltaPitch;
}
else
{
pitch -= deltaPitch;
}
pitch = Mathf.Clamp (pitch, -maxPitch, maxPitch);
}
}
if (pitchLock == RotationLock.Locked)
{
pitch = maxPitch;
}
}
Quaternion rotation = Quaternion.Euler (pitch, spin, roll);
transform.rotation = rotation;
transform.position = target.position - (rotation * Vector3.forward * (distance - Mathf.Min (collisionOffset, distance - minDistance)) + new Vector3 (0f, -verticalOffset, 0f)) + (rotation * Vector3.right * horizontalOffset);
}
private void OnDestroy ()
{
stateHandler = null;
playerInput = null;
}
}
|