Search Unity

how to make multiple physics per wing

Discussion in 'Scripting' started by Aviation_Simmer, May 7, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    Hey! I have a airplane with a wingscript. but It calculates only once per wing. can I multiply that?
    thanks!

    Code (CSharp):
    1. /////////////////////////////////
    2. ///COPYRIGHT ⒸAVIATION SIMMER
    3. ///GAME | UNITY FLIGHT SIMULATOR
    4. /////////////////////////////////
    5.  
    6. using UnityEngine;
    7. using UnityEditor;
    8.  
    9. [CustomEditor(typeof(SimpleWing))]
    10. [CanEditMultipleObjects]
    11. public class SimpleWingEditor : Editor
    12. {
    13.     private bool showDebug = false;
    14.  
    15.     public override void OnInspectorGUI()
    16.     {
    17.         base.OnInspectorGUI();
    18.  
    19.         SimpleWing wing = (SimpleWing)target;
    20.  
    21.         EditorGUILayout.Space();
    22.         showDebug = EditorGUILayout.ToggleLeft("Show debug values", showDebug);
    23.  
    24.         if (showDebug)
    25.         {
    26.             EditorGUILayout.Space();
    27.             EditorGUILayout.LabelField("Wing Area: ", wing.WingArea.ToString("0.00"));
    28.             EditorGUILayout.LabelField("Angle of Attack: ", wing.AngleOfAttack.ToString("0.00"));
    29.  
    30.             EditorGUILayout.Space();
    31.             EditorGUILayout.LabelField("Lift Coefficient: ", wing.LiftCoefficient.ToString("0.00"));
    32.             EditorGUILayout.LabelField("Lift Force: ", wing.LiftForce.ToString("0.00"));
    33.  
    34.             EditorGUILayout.Space();
    35.             EditorGUILayout.LabelField("Drag Coefficient: ", wing.DragCoefficient.ToString("0.00"));
    36.             EditorGUILayout.LabelField("Drag Force: ", wing.DragForce.ToString("0.00"));
    37.  
    38.             if (Application.isPlaying)
    39.             {
    40.                 Repaint();
    41.             }
    42.         }
    43.     }
    44. }
    Code (CSharp):
    1. /////////////////////////////////
    2. ///COPYRIGHT ⒸAVIATION SIMMER
    3. ///GAME | UNITY FLIGHT SIMULATOR
    4. /////////////////////////////////
    5.  
    6. using UnityEngine;
    7.  
    8. public class SimpleWing : MonoBehaviour
    9. {
    10.     [Tooltip("Size of the wing. The bigger the wing, the more lift it provides.")]
    11.     public Vector2 dimensions = new Vector2(5f, 2f);
    12.  
    13.     [Tooltip("When true, wing forces will be applied only at the center of mass.")]
    14.     public bool applyForcesToCenter = false;
    15.  
    16.     [Tooltip("Lift coefficient curve.")]
    17.     public WingCurves wing;
    18.     [Tooltip("The higher the value, the more lift the wing applie at a given angle of attack.")]
    19.     public float liftMultiplier = 1f;
    20.     [Tooltip("The higher the value, the more drag the wing incurs at a given angle of attack.")]
    21.     public float dragMultiplier = 1f;
    22.  
    23.     private Rigidbody rigid;
    24.  
    25.     private Vector3 liftDirection = Vector3.up;
    26.  
    27.     private float liftCoefficient = 0f;
    28.     private float dragCoefficient = 0f;
    29.     private float liftForce = 0f;
    30.     private float dragForce = 0f;
    31.     private float angleOfAttack = 0f;
    32.  
    33.     public float AngleOfAttack
    34.     {
    35.         get
    36.         {
    37.             if (rigid != null)
    38.             {
    39.                 Vector3 localVelocity = transform.InverseTransformDirection(rigid.velocity);
    40.                 return angleOfAttack * -Mathf.Sign(localVelocity.y);
    41.             }
    42.             else
    43.             {
    44.                 return 0.0f;
    45.             }
    46.         }
    47.     }
    48.  
    49.     public float WingArea
    50.     {
    51.         get { return dimensions.x * dimensions.y; }
    52.     }
    53.  
    54.     public float LiftCoefficient { get { return liftCoefficient; } }
    55.     public float LiftForce { get { return liftForce; } }
    56.     public float DragCoefficient { get { return dragCoefficient; } }
    57.     public float DragForce { get { return dragForce; } }
    58.  
    59.     public Rigidbody Rigidbody
    60.     {
    61.         set { rigid = value; }
    62.     }
    63.  
    64.     private void Awake()
    65.     {
    66.         // I don't especially like doing this, but there are many cases where wings might not
    67.         // have the rigidbody on themselves (e.g. they are on a child gameobject of a plane).
    68.         rigid = GetComponentInParent<Rigidbody>();
    69.     }
    70.  
    71.     private void Start()
    72.     {
    73.         if (rigid == null)
    74.         {
    75.             Debug.LogError(name + ": SimpleWing has no rigidbody on self or parent!");
    76.         }
    77.  
    78.         if (wing == null)
    79.         {
    80.             Debug.LogError(name + ": SimpleWing has no defined wing curves!");
    81.         }
    82.     }
    83.  
    84.     private void Update()
    85.     {
    86.         // Prevent division by zero.
    87.         if (dimensions.x <= 0f)
    88.             dimensions.x = 0.01f;
    89.         if (dimensions.y <= 0f)
    90.             dimensions.y = 0.01f;
    91.     }
    92.  
    93.     private void LateUpdate()
    94.     {
    95.         if (rigid != null)
    96.         {
    97.             if (angleOfAttack < 12)
    98.             Debug.DrawRay(transform.position, liftDirection * liftForce * 0.00005f, Color.green);
    99.             Debug.DrawRay(transform.position, -rigid.velocity.normalized * dragForce * 0.00005f, Color.blue);
    100.  
    101.             if (angleOfAttack > 12)
    102.             Debug.DrawRay(transform.position, liftDirection * liftForce * 0.00005f, Color.red);
    103.             Debug.DrawRay(transform.position, -rigid.velocity.normalized * dragForce * 0.00005f, Color.red);
    104.         }
    105.     }
    106.  
    107.     private void FixedUpdate()
    108.     {
    109.         if (rigid != null && wing != null)
    110.         {
    111.             Vector3 forceApplyPos = (applyForcesToCenter) ? rigid.transform.TransformPoint(rigid.centerOfMass) : transform.position;
    112.  
    113.             Vector3 localVelocity = transform.InverseTransformDirection(rigid.GetPointVelocity(transform.position));
    114.             localVelocity.x = 0f;
    115.  
    116.             // Angle of attack is used as the look up for the lift and drag curves.
    117.             angleOfAttack = Vector3.Angle(Vector3.forward, localVelocity);
    118.             liftCoefficient = wing.GetLiftAtAOA(angleOfAttack);
    119.             dragCoefficient = wing.GetDragAtAOA(angleOfAttack);
    120.  
    121.             // Calculate lift/drag.
    122.             liftForce = localVelocity.sqrMagnitude * liftCoefficient * WingArea * liftMultiplier;
    123.             dragForce = localVelocity.sqrMagnitude * dragCoefficient * WingArea * dragMultiplier;
    124.  
    125.             // Vector3.Angle always returns a positive value, so add the sign back in.
    126.             liftForce *= -Mathf.Sign(localVelocity.y);
    127.  
    128.             // Lift is always perpendicular to air flow.
    129.             liftDirection = Vector3.Cross(rigid.velocity, transform.right).normalized;
    130.             rigid.AddForceAtPosition(liftDirection * liftForce, forceApplyPos, ForceMode.Force);
    131.  
    132.             // Drag is always opposite of the velocity.
    133.             rigid.AddForceAtPosition(-rigid.velocity.normalized * dragForce, forceApplyPos, ForceMode.Force);
    134.         }
    135.     }
    136.  
    137. // Prevent this code from throwing errors in a built game.
    138. #if UNITY_EDITOR
    139.     private void OnDrawGizmosSelected()
    140.     {
    141.         Matrix4x4 oldMatrix = Gizmos.matrix;
    142.  
    143.         Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
    144.         Gizmos.DrawWireCube(Vector3.zero, new Vector3(dimensions.x, 0f, dimensions.y));
    145.  
    146.         Gizmos.matrix = oldMatrix;
    147.     }
    148. #endif
    149. }
    upload_2022-5-7_12-30-39.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Multiply that... by what?

    You might want to check out this project:

    https://github.com/gasgiant/Aircraft-Physics

    I used it effortlessly for the core of my Pilot Kurt game... the "air feel" is spot on.