-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightAI.cs
More file actions
342 lines (279 loc) · 9.54 KB
/
FlightAI.cs
File metadata and controls
342 lines (279 loc) · 9.54 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using UnityEngine;
using System.Collections;
public class FlightAI : MonoBehaviour {
Rigidbody rigid;
Camera frontCamera;
Camera rightCamera;
Camera leftCamera;
Camera mainCamera;
bool accelerating = false;
bool decelerating = false;
public float moveSpeed = 500f;
float turnSpeed = 75f;
readonly float maxSpeed = 100000f;
readonly float minSpeed = 100f;
// Use this for initialization
void Start()
{
frontCamera = (Camera)GameObject.Find("Front Camera").GetComponent<Camera>();
rightCamera = (Camera)GameObject.Find("Right Camera").GetComponent<Camera>();
leftCamera = (Camera)GameObject.Find("Left Camera").GetComponent<Camera>();
mainCamera = (Camera)GameObject.Find("Main Camera").GetComponent<Camera>();
rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
/*Speed Control with camera fade in or out*/
mainCamera.enabled = true;
frontCamera.enabled = false;
leftCamera.enabled = false;
rightCamera.enabled = false;
/*if (Input.GetKey(KeyCode.Q))
{
moveSpeed += 5f;
if (!accelerating && mainCamera.fieldOfView != 80 && moveSpeed < maxSpeed)
{
StartCoroutine(cameraFadeOut());
}
}
if (Input.GetKeyUp(KeyCode.Q) || moveSpeed >= maxSpeed)
StartCoroutine(cameraStabilize());//mainCamera.fieldOfView = 60f;
if (Input.GetKey(KeyCode.Z))
{
moveSpeed -= 5f;
if (!decelerating && mainCamera.fieldOfView != 40 && moveSpeed > minSpeed)
{
StartCoroutine(cameraFadeIn());
}
}
if (Input.GetKeyUp(KeyCode.Z) || moveSpeed <= minSpeed)
StartCoroutine(cameraStabilize());// mainCamera.fieldOfView = 60f;
*/
if (moveSpeed < minSpeed)
moveSpeed = minSpeed;
else if (moveSpeed > maxSpeed)
moveSpeed = maxSpeed;
/*Camera Control*/
if (Input.GetKey(KeyCode.C) || Input.GetKey(KeyCode.Keypad5))
{
mainCamera.enabled = false;
leftCamera.enabled = false;
rightCamera.enabled = false;
frontCamera.enabled = true;
}
if (Input.GetKey(KeyCode.Keypad4))
{
mainCamera.enabled = false;
frontCamera.enabled = false;
rightCamera.enabled = false;
leftCamera.enabled = true;
}
if (Input.GetKey(KeyCode.Keypad6))
{
mainCamera.enabled = false;
frontCamera.enabled = false;
leftCamera.enabled = false;
rightCamera.enabled = true;
}
if (Input.GetKey(KeyCode.Space))
{
}
}
void FixedUpdate()
{
//Simulate arcade style airplane controls
Quaternion newRot = Quaternion.identity;
float roll = 0f;
float yaw = 0f;
float pitch = 0f;
float targetX = GameObject.Find("MainMockPlane").GetComponent<Transform>().position.x;
float targetY = GameObject.Find("MainMockPlane").GetComponent<Transform>().position.y;
float targetZ = GameObject.Find("MainMockPlane").GetComponent<Transform>().position.z;
/*
float targetX = 0;
float targetY = 0;
float targetZ = 0;
*/
float rotationx = transform.eulerAngles.x;
float rotationy = transform.eulerAngles.y;
float rotationz = transform.eulerAngles.z;
//print (transform.forward);
var tar = new Vector3 (targetX-transform.position.x, targetY-transform.position.y, targetZ-transform.position.z);
tar = tar / (Mathf.Sqrt (((targetZ - transform.position.z) * (targetZ - transform.position.z)) + ((targetY - transform.position.y) * (targetY - transform.position.y)) + ((targetX - transform.position.x) * (targetX - transform.position.x))));
//print (tar);
float softTilt = getSoftTilt(tar, transform.eulerAngles.z,targetX);
roll = getPlaneRoll(tar, targetX, targetY, targetZ, rotationz, softTilt, Time.deltaTime);
yaw = getPlaneYaw (tar, targetX, targetY, targetZ, moveSpeed, maxSpeed, rotationy, Time.deltaTime);
pitch = getPlanePitch (tar, targetX, targetY, targetZ, rotationx, Time.deltaTime);
//roll = 0;
//yaw = 0;
//pitch = 0;
newRot.eulerAngles = new Vector3(pitch, yaw, roll);
rigid.rotation *= newRot;
Vector3 newPos = Vector3.forward;
newPos = rigid.rotation * newPos;
rigid.velocity = newPos * (Time.deltaTime * moveSpeed);
}
/*Camera coroutines*/
IEnumerator cameraStabilize()
{
while (mainCamera.fieldOfView != 60)
{
if (mainCamera.fieldOfView > 60)
{
mainCamera.fieldOfView -= 1;
}
else {
mainCamera.fieldOfView += 1;
}
yield return new WaitForFixedUpdate();
}
}
IEnumerator cameraFadeOut()
{
for (float i = 60f; i <= 80f; i += 2f)
{
accelerating = true;
mainCamera.fieldOfView = i;
yield return new WaitForFixedUpdate();
accelerating = false;
}
}
IEnumerator cameraFadeIn()
{
for (float i = 60f; i >= 40f; i -= 2f)
{
decelerating = true;
mainCamera.fieldOfView = i;
yield return new WaitForFixedUpdate();
decelerating = false;
}
}
float getSoftTilt(Vector3 tar, float rotationz, float targetX){
float rightleftsoft = 0f;
float horizontalInput = 0f;
//get angle between current direction and target direction
float angle = Vector3.Angle (transform.forward, tar);
//get target position in plane coordinates
var worldToPlane = transform.InverseTransformPoint(GameObject.Find("MainMockPlane").GetComponent<Transform>().position);
/*print (angle);
print (worldToPlane);*/
//if target on left or right
if (worldToPlane.x > 1 ) {
horizontalInput = 1.0f;
} else if (worldToPlane.x < - 1){
horizontalInput = -1.0f;
}
if ((horizontalInput<=0)&&(rotationz >0)&&(rotationz <90)) rightleftsoft = rotationz*2.2f/100*-1;//linksrum || to the left
if ((horizontalInput>=0)&&(rotationz >270)) rightleftsoft= (7.92f-rotationz*2.2f/100);//rechtsrum ||to the right
if (rightleftsoft>1) rightleftsoft =1;
if (rightleftsoft<-1) rightleftsoft =-1;
if ((rightleftsoft>-0.01) && (rightleftsoft<0.01)) rightleftsoft=0;
return rightleftsoft;
}
float getPlaneRoll(Vector3 tar, float targetX, float targetY, float targetZ, float rotationZ, float softTilt, float timeElapsed){
float roll = 0f;
float horizontalInput=0f;
float turnSpeed = 100f;
float returnSpeed = 80f;
//get angle between current direction and target direction
float angle = Vector3.Angle (transform.forward, tar);
//get target position in plane coordinates
var worldToPlane = transform.InverseTransformPoint(GameObject.Find("MainMockPlane").GetComponent<Transform>().position);
/*print (angle);
print (worldToPlane);*/
//if target on left or right
if (worldToPlane.x > 1 ) {
horizontalInput = 1.0f;
} else if (worldToPlane.x < - 1){
horizontalInput = -1.0f;
}
// checks if plane is tilted in the direction opposite that of current horizontal manoeuvre
if (horizontalInput != 0) {
if ((rotationZ > 1) && (rotationZ < 180) && (horizontalInput > 0))
roll -= turnSpeed;
if ((rotationZ > 180) && (rotationZ < 359) && (horizontalInput < 0))
roll += turnSpeed;
else roll += turnSpeed * -(1.0f - Mathf.Abs (softTilt)) * horizontalInput;
} else {
if ((rotationZ > 1) && (rotationZ < 135))
roll -= returnSpeed;
if ((rotationZ > 225) && (rotationZ < 359))
roll += returnSpeed;
}
return roll * timeElapsed;
}
float getPlanePitch(Vector3 tar, float targetX, float targetY, float targetZ, float rotationX, float timeElapsed){
float verticalInput = 0f;
float pitch = 0f;
float turnSpeed = 80f;
float returnSpeed = 50f;
/* DEPRECATED
float hypX = Mathf.Sqrt (((targetZ - transform.position.z) * (targetZ - transform.position.z)) + ((targetY - transform.position.y) * (targetY - transform.position.y)));
//absolute to avoid negative angles (backspin)
float adjX = Mathf.Abs (targetZ - transform.position.z);
float angleLimit = (Mathf.Acos (adjX / hypX))*180/Mathf.PI;
//print(angleLimit);
*/
//decides if rotation should be up or down
if (tar.y > transform.forward.y + 0.05) {
verticalInput = -1.0f;
} else if (tar.y < transform.forward.y -0.05){
verticalInput = 1.0f;
}
//sets limit to rotation using the smalles value of angleLimit of 90deg
if (verticalInput !=0){
if (verticalInput < 0) {
//print (rotationX);
/*
if (rotationX > (360-angleLimit))
pitch += ((rotationX - (360-angleLimit)) / angleLimit) * verticalInput * 80;
else */if (rotationX > 270)
pitch += ((rotationX - 270) / 90.0f) * verticalInput * 80;
else
pitch += verticalInput * 80;
}else {
//print (rotationX);
/*
if (rotationX < 90 && rotationX < angleLimit)
pitch += (1.0f - rotationX / angleLimit) * verticalInput * 80;
else */if (rotationX < 90)
pitch += (1.0f - rotationX / 90.0f) * verticalInput * 80;
else
pitch += verticalInput * 80;
}
}
else{
if ((rotationX > 1) && (rotationX < 180)) pitch -= returnSpeed;
if ((rotationX > 180) && (rotationX <359)) pitch += returnSpeed;
}
return pitch * timeElapsed;
}
float getPlaneYaw(Vector3 tar, float targetX, float targetY, float targetZ, float speed, float maxSpeed, float rotationY, float timeElapsed){
float yaw = 0f;
float turnSpeed = 80f;
float horizontalInput = 0f;
float adjustedTurnSpeed = 80;
//get angle between current direction and target direction
float angle = Vector3.Angle (transform.forward, tar);
//get target position in plane coordinates
var worldToPlane = transform.InverseTransformPoint(GameObject.Find("MainMockPlane").GetComponent<Transform>().position);
/*print (angle);
print (worldToPlane);*/
//if target on left or right
if (worldToPlane.x > 1 ) {
horizontalInput = 1.0f;
} else if (worldToPlane.x < - 1){
horizontalInput = -1.0f;
}
if (horizontalInput !=0){
yaw += horizontalInput * adjustedTurnSpeed;
}
else{/*
if ((rotationY > 1) && (rotationY < 180)) pitch -= returnSpeed;
if ((rotationY > 180) && (rotationY <359)) pitch += returnSpeed;
*/}
return yaw * timeElapsed;
}
}