-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlyMovement.cs
More file actions
321 lines (295 loc) · 11 KB
/
FlyMovement.cs
File metadata and controls
321 lines (295 loc) · 11 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyMovement : MonoBehaviour
{
public float currentSpeed;
private FloatReference heightOffset;
private FloatReference marginOffset;
public float maxSpeed;
[HideInInspector] public float maxSpeedCopy;
public float acceleration;
[HideInInspector] public float accelerationCopy;
public float efficiency;
public float rotationSpeed;
[HideInInspector] public float rotationSpeedCopy;
public bool runTransition;
private int flyState; // 0 plan, 1 accelerate, -1 deccelerate (animaciones)
public bool isFalling;
private float deltaAngleRotationY;
//private float deltaAngleRotationX;
public BirdLookAtController lookAt;
BirdMovementController controller;
BirdAnimatorController animator;
public GameObject airTrail;
public GameObject vfxCrash;
public GameObject vfxFeatherTwirl;
public AudioSourceSplitScreen3D audioSourceFly;
public AudioSourceSplitScreen3D audioSourceChirp;
public AudioSourceSplitScreen3D audioSourceCrash;
public AudioSourceSplitScreen3D audioSourceTwirls;
public AudioClip[] damageChirpClips;
public AudioClip[] chirpClips;
void Awake()
{
controller = GetComponent<BirdMovementController>();
animator = GetComponent<BirdAnimatorController>();
heightOffset = lookAt.upOffset;
marginOffset = lookAt.rightOffset;
maxSpeedCopy = maxSpeed;
accelerationCopy = acceleration;
rotationSpeedCopy = rotationSpeed;
}
void OnEnable()
{
currentSpeed = 1;
}
public void Move()
{
if (controller.canRotate.Value)
{
Rotate();
}
if (controller.canMove.Value)
{
FlyMove();
}
controller.charController.Move(controller.direction * Time.deltaTime); // SE MUEVE
}
private void Rotate()
{
// La velocidad de rotacion aumenta o disminuye en funcion de la velocidad actual
if(currentSpeed > 0)
rotationSpeed = (maxSpeedCopy/currentSpeed) * rotationSpeedCopy;
rotationSpeed = Mathf.Clamp(rotationSpeed, rotationSpeedCopy, rotationSpeedCopy * 2);
// Si esta quieto
if (currentSpeed == 0)
{
controller.transform.rotation = Quaternion.Slerp(controller.transform.rotation,
Quaternion.Euler(-2, controller.transform.eulerAngles.y, 0), 5 * Time.deltaTime);
marginOffset.Value = Mathf.Lerp(marginOffset.Value, 0f, 2 * Time.deltaTime);
heightOffset.Value = Mathf.Lerp(heightOffset.Value, 0f, 2 * Time.deltaTime);
lookAt.SmoothCentre();
}
// Si esta moviendose
else
{
// Calculo de los angulos que hay entre el pajaro y el lookAt
deltaAngleRotationY = Mathf.DeltaAngle(controller.transform.eulerAngles.y, controller.lookAt.eulerAngles.y);
//deltaAngleRotationX = Mathf.DeltaAngle(controller.transform.eulerAngles.x, controller.lookAt.eulerAngles.x);
// Se realizan las rotaciones del pajaro relativas al angulo de giro
if (Mathf.Abs(deltaAngleRotationY) < 45)
{
controller.transform.rotation = Quaternion.Slerp(controller.transform.rotation,
Quaternion.Euler(controller.lookAt.eulerAngles.x, controller.lookAt.localEulerAngles.y,
-deltaAngleRotationY * 0.8f * rotationSpeed / 1.5f), rotationSpeed * Time.deltaTime);
}
else
{
controller.transform.rotation = Quaternion.Slerp(controller.transform.rotation,
Quaternion.Euler(-5, controller.lookAt.eulerAngles.y,
-deltaAngleRotationY * rotationSpeed / 1.5f), rotationSpeed * Time.deltaTime);
}
// Efecto de camara al curvar durante el vuelo
marginOffset.Value = Mathf.Lerp(marginOffset.Value, deltaAngleRotationY / 32, 4f * Time.deltaTime);
marginOffset.Value = Mathf.Clamp(marginOffset.Value, -2f, 2f);
//marginOffset.Value = Mathf.Lerp(marginOffset.Value, 0f, 2*Time.deltaTime);
heightOffset.Value = Mathf.Lerp(heightOffset.Value, deltaAngleRotationY / 32, 0.5f * Time.deltaTime);
heightOffset.Value = Mathf.Clamp(heightOffset.Value, -0.2f, 0.2f);
//heightOffset.Value = Mathf.Lerp(heightOffset.Value, 0f, 2*Time.deltaTime);
}
}
private void FlyMove()
{
animator.UpdateFly();
float angleX = Mathf.DeltaAngle(0, controller.transform.eulerAngles.x);
float inclinationMagnitude = Mathf.Sin(controller.transform.eulerAngles.x * Mathf.PI / 180);
float calculatedSpeed;
// Se calcula la velocidad calculada con proporciones distintas a la inclinacion en funcion de si baja o sube
if (angleX > 0)
{
calculatedSpeed = maxSpeed * (1 + 2 * inclinationMagnitude);
}
else
{
calculatedSpeed = maxSpeed * (1 + inclinationMagnitude / 5);
}
// MODO PLANEO
if (controller.inputV.Value == 0)
{
if (currentSpeed > 1)
{
Glide();
}
// SI BAJA
if (angleX > 0)
{
currentSpeed = Mathf.Lerp(currentSpeed,
calculatedSpeed, inclinationMagnitude * Time.deltaTime);
}
// SI SUBE
else
{
if (currentSpeed < 1)
{
Accelerate();
}
currentSpeed = Mathf.Lerp(currentSpeed,
0.5f, (0.1f - inclinationMagnitude) * (1 - efficiency) * Time.deltaTime);
}
}
// ACELERA
else if (controller.inputV.Value > 0)
{
// SI BAJA
if (angleX > 0)
{
// Usa la aceleracion del ave si la inclinacion no supera 30 grados y puede aumentar la velocidad
if (angleX < 30 && calculatedSpeed > currentSpeed)
{
Accelerate();
currentSpeed = Mathf.Lerp(currentSpeed,
calculatedSpeed, acceleration * Time.deltaTime);
}
// Usa la aceleracion gravitatoria si la inclinacion supera los 30 grados
else
{
Glide();
currentSpeed = Mathf.Lerp(currentSpeed,
calculatedSpeed, inclinationMagnitude * Time.deltaTime);
}
}
// SI SUBE
else
{
// Acelera si va por debajo de la velocidad alcanzable
if (currentSpeed < calculatedSpeed)
{
Accelerate();
currentSpeed = Mathf.Lerp(currentSpeed,
calculatedSpeed, acceleration * Time.deltaTime);
}
// No acelera si va mas rapido de la velocidad alcanzable (va con inercia), decelera naturalmente
else
{
Glide();
currentSpeed = Mathf.Lerp(currentSpeed,
0.5f, (0.1f - inclinationMagnitude) * (1 - efficiency) * Time.deltaTime);
}
}
}
// DECELERA
else
{
// SI BAJA
if (angleX > 0)
{
// Frena si la inclinacion no supera 30 grados
if (angleX < 30)
{
if (currentSpeed < 6)
{
Deccelerate();
}
else
{
Glide();
}
currentSpeed = Mathf.Lerp(currentSpeed,
0.5f, acceleration * 0.1f * Time.deltaTime);
}
// Usa la aceleracion gravitatoria si la inclinacion supera los 30 grados
else
{
Glide();
currentSpeed = Mathf.Lerp(currentSpeed,
calculatedSpeed, inclinationMagnitude * Time.deltaTime);
}
}
// SI SUBE
else
{
// Frena sin condiciones
if (currentSpeed < 6)
{
Deccelerate();
}
currentSpeed = Mathf.Lerp(currentSpeed,
0.5f, acceleration * 0.1f * Time.deltaTime);
}
}
controller.direction.Set(0, 0, currentSpeed);
// Transformamos la direccion de local a world space (relativa al transform del player)
controller.direction = controller.transform.TransformDirection(controller.direction);
}
public void HandleCollide()
{
audioSourceCrash.Play();
PlayDamageSound();
Destroy(Instantiate<GameObject>(vfxCrash, transform.position+Vector3.up*0.3f, Quaternion.identity), 2);
Deccelerate();
controller.canMove.Value = false;
controller.direction = -controller.transform.forward * 4;
controller.StartCoroutine(CollisionCoroutine());
}
private IEnumerator CollisionCoroutine()
{
currentSpeed = 0;
yield return new WaitForSeconds(0.5f);
currentSpeed = 0.5f;
controller.canMove.Value = true;
Glide();
}
private void Glide()
{
if (flyState != 0)
{
airTrail.SetActive(true);
animator.Glide();
audioSourceFly.Clip = controller.movementSounds[1];
audioSourceFly.Loop = true;
audioSourceFly.Volume = 0.1f;
audioSourceFly.Pitch = 0.5f;
audioSourceFly.Play();
flyState = 0;
}
}
private void Accelerate()
{
if (flyState != 1)
{
airTrail.SetActive(false);
animator.Accelerate();
audioSourceFly.Clip = controller.movementSounds[0];
audioSourceFly.Loop = true;
audioSourceFly.Volume = 0.07f;
audioSourceFly.Pitch = 2f;
audioSourceFly.Play();
flyState = 1;
}
}
private void Deccelerate()
{
if (flyState != -1)
{
airTrail.SetActive(false);
animator.Deccelerate();
audioSourceFly.Clip = controller.movementSounds[0];
audioSourceFly.Loop = true;
audioSourceFly.Volume = 0.17f;
audioSourceFly.Pitch = 3f;
audioSourceFly.Play();
flyState = -1;
}
}
public void PlayDamageSound()
{
audioSourceChirp.Clip = damageChirpClips[Random.Range(0, damageChirpClips.Length)];
audioSourceChirp.Play();
}
public void PlayChirp()
{
audioSourceChirp.Clip = chirpClips[Random.Range(0, chirpClips.Length)];
audioSourceChirp.Play();
}
}