1 using System.Collections;
2 using System.Collections.Generic;
14 [Header(
"Characteristics Properties")]
16 private float rbLerpSpeed;
19 [Header(
"Lift Properties")]
20 private float maxLiftPower;
21 public AnimationCurve liftCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
22 private float flapLiftPower;
25 [Header(
"Drag Properties")]
26 private float dragFactor;
27 private float flapDragFactor;
30 [Header(
"Control Properties")]
31 private float pitchSpeed;
32 private float rollSpeed;
33 private float yawSpeed;
34 public AnimationCurve controlSurfaceEfficiency = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
36 public float forwardSpeed;
37 public float ForwardSpeed
39 get{
return forwardSpeed;}
50 private float startDrag;
51 private float startAngularDrag;
54 private float normalizeMPH;
56 private float angleOfAttack;
57 private float pitchAngle;
58 private float rollAngle;
60 private float csEfficiencyValue;
64 const float mpsToMph = 2.23694f;
67 #region BuiltIn Methods
69 maxMPH = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/maxMPH"];
70 rbLerpSpeed = (
float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/rbLerpSpeed"];
72 maxLiftPower = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/maxLiftPower"] ;
73 flapLiftPower = (
float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/flapLiftPower"];
75 dragFactor = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/dragFactor"];
76 flapDragFactor = (
float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/flapDragFactor"];
78 pitchSpeed = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/pitchSpeed"];
79 rollSpeed = (
float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/rollSpeed"];
80 yawSpeed = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+
"/yawSpeed"];
84 #region Custom Methods
96 startAngularDrag = rb.angularDrag;
99 maxMPS = maxMPH / mpsToMph;
110 CalculateForwardSpeed();
115 HandleControlSurfaceEfficiency();
122 HandleRigidbodyTransform();
128 void CalculateForwardSpeed()
131 Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
132 forwardSpeed = Mathf.Max(0f, localVelocity.z);
136 mph = forwardSpeed * mpsToMph;
138 normalizeMPH = Mathf.InverseLerp(0f, maxMPH, mph);
148 angleOfAttack = Vector3.Dot(rb.velocity.normalized, transform.forward);
149 angleOfAttack *= angleOfAttack;
152 Vector3 liftDir = transform.up;
153 float liftPower = liftCurve.Evaluate(normalizeMPH) * maxLiftPower;
159 Vector3 finalLiftForce = liftDir * (liftPower) * angleOfAttack;
160 rb.AddForce(finalLiftForce);
169 float speedDrag = forwardSpeed * dragFactor;
172 float flapDrag = input.Flaps * flapDragFactor;
175 float finalDrag = startDrag + speedDrag + flapDrag;
178 rb.angularDrag = startAngularDrag * forwardSpeed;
184 void HandleRigidbodyTransform()
186 if(rb.velocity.magnitude > 1f)
188 Vector3 updatedVelocity = Vector3.Lerp(rb.velocity, transform.forward * forwardSpeed, forwardSpeed * angleOfAttack * Time.deltaTime * rbLerpSpeed);
189 rb.velocity = updatedVelocity;
192 Quaternion updatedRotation = Quaternion.Slerp(rb.rotation, Quaternion.LookRotation(rb.velocity, transform.up), Time.deltaTime * rbLerpSpeed);
193 rb.MoveRotation(updatedRotation);
200 void HandleControlSurfaceEfficiency()
202 csEfficiencyValue = controlSurfaceEfficiency.Evaluate(normalizeMPH);
210 Vector3 flatForward = transform.forward;
212 flatForward = flatForward.normalized;
213 pitchAngle = Vector3.Angle(transform.forward, flatForward);
216 Vector3 pitchTorque = input.Pitch * pitchSpeed * transform.right * csEfficiencyValue;
217 rb.AddTorque(pitchTorque);
225 Vector3 flatRight = transform.right;
227 flatRight = flatRight.normalized;
228 rollAngle = Vector3.SignedAngle(transform.right, flatRight, transform.forward);
231 Vector3 rollTorque = -input.Roll * rollSpeed * transform.forward * csEfficiencyValue;
232 rb.AddTorque(rollTorque);
240 Vector3 yawTorque = input.Yaw * yawSpeed * transform.up * csEfficiencyValue;
241 rb.AddTorque(yawTorque);
249 float bankSide = Mathf.InverseLerp(-90f, 90f, rollAngle);
250 float bankAmount = Mathf.Lerp(-1f, 1f, bankSide);
251 Vector3 bankTorque = bankAmount * rollSpeed * transform.up;
252 rb.AddTorque(bankTorque);