Search Unity

My Flight Script

Discussion in 'Scripting' started by Sea_Laszlo, May 8, 2015.

  1. Sea_Laszlo

    Sea_Laszlo

    Joined:
    Jun 13, 2013
    Posts:
    12
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class AreoTest : MonoBehaviour
    6. {
    7.  
    8.  
    9.     // Create Transforms for each of the moveable parts
    10.     public Transform aileronLeft;
    11.     public Transform aileronRight;
    12.     public Transform stabilator;
    13.     public Transform rudder;
    14.     public Transform gearLeft;
    15.     public Transform gearRight;
    16.     public Transform gearNose;
    17.     public Transform wheelLeft;
    18.     public Transform wheelRight;
    19.     public Transform wheelNose;
    20.    
    21.     // Set the sensitivity of the controls
    22.     public float roll_sensitivity = 18f;
    23.     public float pitch_sensitivity = 5f;
    24.     public float yaw_sensitivity = 2f;
    25.    
    26.     // Landing gear retraction/deplopyment speed
    27.     public float leftGearSpeed = 2f;
    28.     public float rightGearSpeed = 2f;
    29.     public float noseGearSpeed = 3f;
    30.    
    31.     // Set limits to the +/- angles of the control surfaces
    32.     public float aileronAngleLimit = 15f;
    33.     public float stabilatorAngleLimit = 15f;
    34.     public float rudderAngleLimit = 15f;
    35.     public float weight = 14600F;
    36.     public float thrust = 1.5F;
    37.     public float wingArea = 50.0F;
    38.     public float dragCoeffiecent = 0.021F;
    39.     public float upForce = 0.0F;
    40.     public float simLift = 0.0F;
    41.     public float simDrag = 0.0F;
    42.     public float liftPercentage;
    43.     public float currentAirDensity;
    44.    
    45.     private Rigidbody rb;
    46.     private float gravity;
    47.     private float pitch = 0.0F;
    48.     private float roll = 0.0F;
    49.     // Keep track of which state the landing gear is in
    50.     private bool retracting = false;
    51.    
    52.     private float[] pressureDensity =
    53.     {
    54.           1.2F
    55.         , 1.1F
    56.         , 1.0F
    57.         , 0.91F
    58.         , 0.82F
    59.         , 0.74F
    60.         , 0.66F
    61.         , 0.59F
    62.         , 0.53F
    63.         , 0.47F
    64.         , 0.41F
    65.         , 0.36F
    66.         , 0.31F
    67.         , 0.27F
    68.         , 0.23F
    69.         , 0.19F
    70.         , 0.17F
    71.         , 0.14F
    72.         , 0.12F
    73.         , 0.10F
    74.         , 0.088F
    75.         , 0.075F
    76.         , 0.064F
    77.         , 0.054F
    78.         , 0.046F
    79.         , 0.039F
    80.         , 0.034F
    81.         , 0.029F
    82.         , 0.025F
    83.         , 0.021F
    84.         , 0.018F
    85.         , 0.015F
    86.         , 0.013F
    87.         , 0.011F
    88.         , 0.0096F
    89.         , 0.0082F
    90.        
    91.     };
    92.  
    93.  
    94.     // Use this for initialization
    95.     void Start ()
    96.     {
    97.         rb = GetComponent<Rigidbody>();
    98.        
    99.         // Assign the universal weight of gravity
    100.         gravity = Physics.gravity.magnitude;
    101.        
    102.        
    103.        
    104.         float tmpWeight = weight * 0.0001F; // 10,000 Simograms = 1
    105.         weight = tmpWeight;
    106.        
    107.        
    108.        
    109.         // Assign values
    110.         rb.mass = tmpWeight;
    111.         rb.drag = dragCoeffiecent;
    112.        
    113.        
    114.         thrust = rb.mass*gravity;
    115.        
    116.         // Check current air density
    117.         StartCoroutine(CheckAir());
    118.     }
    119.    
    120.     // Air is in kg/m3
    121.     IEnumerator CheckAir ()
    122.     {
    123.         int tmp = 0;
    124.         for(int d = 0; d < pressureDensity.Length; d++)
    125.         {
    126.             tmp = tmp + 1000;
    127.             if (transform.position.y <= tmp)
    128.             {
    129.                 currentAirDensity = pressureDensity[d];
    130.                 break;
    131.             }
    132.            
    133.         }
    134.        
    135.         yield return new WaitForSeconds(5);
    136.         StartCoroutine(CheckAir());
    137.     }
    138.    
    139.     void AreoPhysics ()
    140.     {
    141.         // Angle of attack
    142.         float angle = Mathf.Atan2(transform.forward.y, transform.forward.z);
    143.         float cL = LiftCoefficient(angle);
    144.         // Lift in Newtons
    145.         simLift = cL * ((currentAirDensity*rb.velocity.sqrMagnitude)/2) * wingArea;
    146.         simLift = simLift * 0.0001F;
    147.        
    148.        
    149.         float dragForce = dragCoeffiecent * ((currentAirDensity*rb.velocity.sqrMagnitude)/2) * wingArea;
    150.         simDrag = dragForce * 0.0001F; // 10,000 Kilograms = 1 Simogram
    151.  
    152.        
    153.     }
    154.    
    155.    
    156.    
    157.     // Update is called once per frame
    158.     void Update ()
    159.     {
    160.         // simLift mass
    161.        
    162.         roll = Input.GetAxis ("Horizontal");
    163.         pitch = Input.GetAxis ("Vertical");
    164.        
    165.         LandingGear();
    166.         ControlSurfaces();
    167.     }
    168.    
    169.     void LandingGear ()
    170.     {
    171.         // If g is pressed, retract/deploy landing gear
    172.         if (Input.GetKeyDown ("g")){
    173.             if (retracting == false){
    174.                 retracting = true;
    175.             } else{
    176.                 retracting = false;
    177.             }
    178.         }
    179.        
    180.         // Retracting the landing gear
    181.         if (retracting){
    182.            
    183.             // Target position for retraction of left gear
    184.             Quaternion gearLeftRetracted = Quaternion.Euler (0f, 0f, 90f);
    185.            
    186.             // Rotate smoothly toward target position
    187.             gearLeft.transform.localRotation = Quaternion.Slerp (gearLeft.transform.localRotation,
    188.                                                                  gearLeftRetracted, leftGearSpeed*Time.deltaTime);
    189.            
    190.             // Do the same for right landing gear
    191.             Quaternion gearRightRetracted = Quaternion.Euler (0f, 0f, -90f);      
    192.             gearRight.transform.localRotation = Quaternion.Slerp (gearRight.transform.localRotation,
    193.                                                                   gearRightRetracted, rightGearSpeed*Time.deltaTime);
    194.            
    195.             // Do the same for nose landing gear          
    196.             Quaternion gearNoseRetracted = Quaternion.Euler (-100f, 0f, 0f);
    197.             gearNose.transform.localRotation = Quaternion.Slerp (gearNose.transform.localRotation,
    198.                                                                  gearNoseRetracted, noseGearSpeed*Time.deltaTime);
    199.                                                                
    200.             wheelLeft.collider.enabled = false;
    201.             wheelRight.collider.enabled = false;
    202.             wheelNose.collider.enabled = false;
    203.            
    204.         } else { // Deploying the landing gear...
    205.            
    206.             // Target position for retraction of left gear
    207.             Quaternion gearLeftDeployed = Quaternion.Euler (0f, 0f, 0f);
    208.            
    209.             // Rotate smoothly toward target position
    210.             gearLeft.transform.localRotation = Quaternion.Slerp (gearLeft.transform.localRotation,
    211.                                                                  gearLeftDeployed, leftGearSpeed*Time.deltaTime);
    212.            
    213.             // Do the same for right landing gear
    214.             Quaternion gearRightDeployed = Quaternion.Euler (0f, 0f, 0f);      
    215.             gearRight.transform.localRotation = Quaternion.Slerp (gearRight.transform.localRotation,
    216.                                                                   gearRightDeployed, rightGearSpeed*Time.deltaTime);
    217.            
    218.             // Do the same for nose landing gear          
    219.             Quaternion gearNoseDeployed = Quaternion.Euler (0f, 0f, 0f);
    220.             gearNose.transform.localRotation = Quaternion.Slerp (gearNose.transform.localRotation,
    221.                                                                  gearNoseDeployed, noseGearSpeed*Time.deltaTime);
    222.                                                                
    223.             wheelLeft.collider.enabled = true;
    224.             wheelRight.collider.enabled = true;
    225.             wheelNose.collider.enabled = true;
    226.         }
    227.     }
    228.    
    229.    
    230.     void ControlSurfaces ()
    231.     {
    232.         // Move control surfaces
    233.         aileronRight.localRotation = Quaternion.Euler (roll*aileronAngleLimit, aileronRight.rotation.y,
    234.                                                        aileronRight.rotation.z);
    235.        
    236.         aileronLeft.localRotation = Quaternion.Euler (-roll*aileronAngleLimit, aileronLeft.rotation.y,
    237.                                                       aileronLeft.rotation.z);
    238.        
    239.         stabilator.localRotation = Quaternion.Euler (-pitch*stabilatorAngleLimit, stabilator.rotation.y,
    240.                                                      stabilator.rotation.z);
    241.     }
    242.    
    243.    
    244.  
    245.     void FixedUpdate ()
    246.     {
    247.         AreoPhysics();
    248.    
    249.         float speed = rb.velocity.magnitude;
    250.         Debug.Log(speed);
    251.        
    252.         // Vectors
    253.         Vector3 dragVector = (-rb.velocity).normalized * simDrag;
    254.         Vector3 liftVector = transform.up;
    255.         Vector3 energyV3   = transform.forward * thrust;
    256.         if(speed > 260)
    257.         {
    258.             energyV3 = transform.forward;
    259.         }
    260.        
    261.    
    262.         liftPercentage = Mathf.Clamp((simLift/weight),0,1.05F);
    263.         upForce = (gravity * rb.mass) * liftPercentage;
    264.  
    265.  
    266.         liftVector = liftVector * upForce;
    267.        
    268.        
    269.         rb.AddForce(energyV3-dragVector);
    270.         rb.AddForce(liftVector);
    271.        
    272.         // Add roll, pitch and yaw torques to rigidbody
    273.         rb.AddRelativeTorque (pitch*roll_sensitivity, 0f, -roll*roll_sensitivity);
    274.    
    275.         //rb.AddRelativeTorque (0f, yaw*yaw_sensitivity, 0f);
    276.        
    277.        
    278.         Debug.DrawRay(transform.position,dragVector,Color.red);
    279.         Debug.DrawRay(transform.position,liftVector,Color.green);
    280.     }
    281.    
    282.     // Appoximation of a typical curve showing section lift coefficient versus angle of attack for a cambered airfoil
    283.     float LiftCoefficient (float rad)
    284.     {
    285.         float cL;
    286.         float degrees = rad * Mathf.Rad2Deg;
    287.        
    288.         if (degrees >= -5.0F && degrees <= 25.0F)
    289.         {
    290.             cL = 1.5F * Mathf.Sin(rad) + 0.5F;
    291.         }
    292.        
    293.         else if (degrees > 25.0F && degrees < 65)
    294.         {
    295.             cL = 0.5F;
    296.         }
    297.         // Stall
    298.         else {
    299.             cL = 0.05F;
    300.         }
    301.        
    302.        
    303.         return cL;
    304.        
    305.     }
    306. }
    Everything works, kinda... I am still working on a good method for adding the appropriate torque with speed.
     
    akimqqq, LichiMan and SAOTA like this.