forked from Blightbuster/The-Forest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AeroplaneController.cs
297 lines (183 loc) · 6.17 KB
/
AeroplaneController.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
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
using System;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class AeroplaneController : MonoBehaviour
{
public float Altitude { get; private set; }
public float Throttle { get; private set; }
public bool AirBrakes { get; private set; }
public float ForwardSpeed { get; private set; }
public float EnginePower { get; private set; }
public float MaxEnginePower
{
get
{
return this.maxEnginePower;
}
}
public float RollAngle { get; private set; }
public float PitchAngle { get; private set; }
public float RollInput { get; private set; }
public float PitchInput { get; private set; }
public float YawInput { get; private set; }
public float ThrottleInput { get; private set; }
private void Start()
{
this.originalDrag = base.GetComponent<Rigidbody>().drag;
this.originalAngularDrag = base.GetComponent<Rigidbody>().angularDrag;
}
public void Move(float rollInput, float pitchInput, float yawInput, float throttleInput, bool airBrakes)
{
this.RollInput = rollInput;
this.PitchInput = pitchInput;
this.YawInput = yawInput;
this.ThrottleInput = throttleInput;
this.AirBrakes = airBrakes;
this.ClampInputs();
this.CalculateRollAndPitchAngles();
this.AutoLevel();
this.CalculateForwardSpeed();
this.ControlThrottle();
this.CalculateDrag();
this.CaluclateAerodynamicEffect();
this.CalculateLinearForces();
this.CalculateTorque();
this.CalculateAltitude();
}
private void ClampInputs()
{
this.RollInput = Mathf.Clamp(this.RollInput, -1f, 1f);
this.PitchInput = Mathf.Clamp(this.PitchInput, -1f, 1f);
this.YawInput = Mathf.Clamp(this.YawInput, -1f, 1f);
this.ThrottleInput = Mathf.Clamp(this.ThrottleInput, -1f, 1f);
}
private void CalculateRollAndPitchAngles()
{
Vector3 forward = base.transform.forward;
forward.y = 0f;
if (forward.sqrMagnitude > 0f)
{
forward.Normalize();
Vector3 vector = base.transform.InverseTransformDirection(forward);
this.PitchAngle = Mathf.Atan2(vector.y, vector.z);
Vector3 direction = Vector3.Cross(Vector3.up, forward);
Vector3 vector2 = base.transform.InverseTransformDirection(direction);
this.RollAngle = Mathf.Atan2(vector2.y, vector2.x);
}
}
private void AutoLevel()
{
this.bankedTurnAmount = Mathf.Sin(this.RollAngle);
if (this.RollInput == 0f)
{
this.RollInput = -this.RollAngle * this.autoRollLevel;
}
if (this.PitchInput == 0f)
{
this.PitchInput = -this.PitchAngle * this.autoPitchLevel;
this.PitchInput -= Mathf.Abs(this.bankedTurnAmount * this.bankedTurnAmount * this.autoTurnPitch);
}
}
private void CalculateForwardSpeed()
{
this.ForwardSpeed = Mathf.Max(0f, base.transform.InverseTransformDirection(base.GetComponent<Rigidbody>().velocity).z);
}
private void ControlThrottle()
{
if (this.immobilized)
{
this.ThrottleInput = -0.5f;
}
this.Throttle = Mathf.Clamp01(this.Throttle + this.ThrottleInput * Time.deltaTime * this.throttleChangeSpeed);
this.EnginePower = this.Throttle * this.maxEnginePower;
}
private void CalculateDrag()
{
float num = base.GetComponent<Rigidbody>().velocity.magnitude * this.dragIncreaseFactor;
base.GetComponent<Rigidbody>().drag = ((!this.AirBrakes) ? (this.originalDrag + num) : ((this.originalDrag + num) * this.airBrakesEffect));
base.GetComponent<Rigidbody>().angularDrag = this.originalAngularDrag * this.ForwardSpeed;
}
private void CaluclateAerodynamicEffect()
{
if (base.GetComponent<Rigidbody>().velocity.magnitude > 0f)
{
this.aeroFactor = Vector3.Dot(base.transform.forward, base.GetComponent<Rigidbody>().velocity.normalized);
this.aeroFactor *= this.aeroFactor;
Vector3 velocity = Vector3.Lerp(base.GetComponent<Rigidbody>().velocity, base.transform.forward * this.ForwardSpeed, this.aeroFactor * this.ForwardSpeed * this.aerodynamicEffect * Time.deltaTime);
base.GetComponent<Rigidbody>().velocity = velocity;
}
}
private void CalculateLinearForces()
{
Vector3 vector = Vector3.zero;
vector += this.EnginePower * base.transform.forward;
Vector3 normalized = Vector3.Cross(base.GetComponent<Rigidbody>().velocity, base.transform.right).normalized;
float num = Mathf.InverseLerp(this.zeroLiftSpeed, 0f, this.ForwardSpeed);
float d = this.ForwardSpeed * this.ForwardSpeed * this.lift * num * this.aeroFactor;
vector += d * normalized;
base.GetComponent<Rigidbody>().AddForce(vector);
}
private void CalculateTorque()
{
Vector3 a = Vector3.zero;
a += this.PitchInput * this.pitchEffect * base.transform.right;
a += this.YawInput * this.yawEffect * base.transform.up;
a += -this.RollInput * this.rollEffect * base.transform.forward;
a += this.bankedTurnAmount * this.bankedTurnEffect * base.transform.up;
base.GetComponent<Rigidbody>().AddTorque(a * this.ForwardSpeed * this.aeroFactor);
}
private void CalculateAltitude()
{
Ray ray = new Ray(base.transform.position - Vector3.up * 10f, -Vector3.up);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
this.Altitude = raycastHit.distance + 10f;
}
else
{
this.Altitude = base.transform.position.y;
}
}
public void Immobilize()
{
this.immobilized = true;
}
public void Reset()
{
this.immobilized = false;
}
[SerializeField]
private float maxEnginePower = 40f;
[SerializeField]
private float lift = 0.002f;
[SerializeField]
private float zeroLiftSpeed = 300f;
[SerializeField]
private float rollEffect = 1f;
[SerializeField]
private float pitchEffect = 1f;
[SerializeField]
private float yawEffect = 0.2f;
[SerializeField]
private float bankedTurnEffect = 0.5f;
[SerializeField]
private float aerodynamicEffect = 0.02f;
[SerializeField]
private float autoTurnPitch = 0.5f;
[SerializeField]
private float autoRollLevel = 0.2f;
[SerializeField]
private float autoPitchLevel = 0.2f;
[SerializeField]
private float airBrakesEffect = 3f;
[SerializeField]
private float throttleChangeSpeed = 0.3f;
[SerializeField]
private float dragIncreaseFactor = 0.001f;
private float originalDrag;
private float originalAngularDrag;
private float aeroFactor;
private bool immobilized;
private float bankedTurnAmount;
}