Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics-based flight script...hard to control though...

Discussion in 'Scripting' started by ASMach, Apr 1, 2012.

  1. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    Hi guys,

    I've come up with the following code for a physics-based flight model. It's a very simple script:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var rollSpeed : float = 100.0; //rolling rotation speed
    5.  
    6. var pitchSpeed : float = 100.0; //vertical rotation speed
    7.  
    8. var yawSpeed : float = 100.0; //horizontal rotation speed
    9.  
    10. var accelerate : float = 20000; //main engine thrust
    11.  
    12. var velocity;
    13.  
    14. var rotSpeed = 15;
    15.  
    16. var planform;
    17.  
    18. var thrust : float = 0;
    19. var maxThrust : float = 600000;
    20.    
    21. var liftFactor : float = 0.1;
    22.  
    23. var crashThreshold = 10.0;
    24.  
    25. var crashSound : AudioSource;
    26.  
    27. private var oldVelocity : float;
    28.    
    29. private var speed : float = 0.0;
    30.  
    31.  
    32. // Use this for initialization
    33. function Start ()
    34. {
    35.     animation["Flying"].wrapMode = WrapMode.Loop;
    36.    
    37.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    38.     crashThreshold *= crashThreshold;   // So it works with sqrMagnitude
    39. }
    40.  
    41. function CrashCheck()
    42. {
    43.     if (oldVelocity - rigidbody.velocity.sqrMagnitude > crashThreshold) {
    44.         // TODO: Proper Crash Code
    45.         crashSound.Play();
    46.         Debug.Log("Penguin Crashed!!!");
    47.     }
    48.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    49. }
    50.    
    51. function KeepInBounds()
    52. {
    53.     //Wrap the penguin around the playfield so that you cannot fly under the landscape
    54.        
    55.     if (transform.position.x >= 1900.0) transform.position.x = 0;
    56.     else if (transform.position.x <= -1900.0) transform.position.x = 1900.0;
    57.     else if (transform.position.z >= 1900.0) transform.position.z = 0;
    58.     else if (transform.position.z <= -1900.0) transform.position.z = 1900.0;
    59.  
    60.     //Limit the height
    61.     if (transform.position.y > 1000) transform.position.y = 1000;
    62.        
    63.     animation.CrossFade("Flying");
    64. }
    65.     function FixedUpdate()
    66.     {
    67.         UpdateFunction();
    68.         CrashCheck();
    69.         KeepInBounds();
    70.     }
    71.    
    72.     function UpdateFunction()
    73.     {
    74.          
    75.         var addRot : Quaternion = Quaternion.identity;
    76.        
    77.         var roll : float = 0;
    78.         var pitch : float = 0;
    79.         var yaw : float = 0;
    80.         var lift : float = 0;
    81.        
    82.         roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed);
    83.         pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
    84.         yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
    85.         thrust = Input.GetAxis("Throttle") * (Time.deltaTime * accelerate);
    86.         lift = Input.GetAxis("Throttle") * (Time.deltaTime * rigidbody.velocity.y * liftFactor);
    87.  
    88.         rigidbody.AddTorque(-pitch, yaw, -roll);
    89.         if (thrust >= 0)
    90.         rigidbody.AddRelativeForce(0,thrust, lift);
    91.        
    92.         //TODO: Add force at two points on a wing for stability
    93.        
    94.         KeepInBounds();
    95.     }
    96.  
    However, it's a bit tricky to fly. For example, if you use keyboard controls for the four axes, you have to be really careful with pressing the keys, otherwise you can easily enter a spin. Moreover, if you try using a gamepad with the right stick controlling, say, pitch and roll, with the right stick controlling the throttle and yaw, you can also get into a spin extremely easily. Does anyone have any suggestions for making it more stable? After all, you can't have something suddenly go from moving predictably on one axis, only for it to go totally haywire the next moment.

    MachCUBED
     
    Last edited: Apr 2, 2012
  2. Heu

    Heu

    Joined:
    Feb 13, 2012
    Posts:
    349
    Well if its physic-based i think you can try to change up the physic to your liking by going to

    Edit>Project Settings>Physic

    Then adjust till you like it. I'm not entirely sure about this though.
     
  3. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    The biggest problem with your code is that I believe AddTorque requires a vector (or components thereof) in world space, not local space. So you ought to use AddRelativeTorque instead here.

    Also, you might need to think about the shape of the object you're flying and set an appropriate inertia tensor. This could for example make it more responsive to roll torque than yaw or pitch torque.

    Your model is also ignoring air resistance, and this is quite important for aircraft as most aircraft are designed to be self-stabilising - so if left to their own devices they will yaw to align with the direction of travel, and roll to level.

    You don't need a complicated aerodynamic model, but I think you do need to put in an extra force proportional to the non-forwards velocity of the plane. Then consider fading out the yaw and pitch inputs when the plane is not flying fairly close to the current facing direction in the corresponding axis - this prevents excessive inputs from causing more and more spin in an unrealistic way.

    Overall though, I expect your main problem is applying your torques in world space when you should be doing it in local space.
     
  4. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    Thanks for the tips George Foot, I can't believe I didn't spot the AddRelativeTorque fluke. I fixed it, so now it works significantly better. It's still a bit tricky, so I'll also implement the inertia tensor fixes and other suggestions that you made, then post the results here.
     
    Last edited: Apr 2, 2012
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    There is probably a way to calculate it from world torque, but there is no variable that stores the localtorque.. would be really useful as to stop your plane from spinning, you either have to set the angulardrag or add negative force.
     
  6. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    The angular drag was initially 0.5, with a main drag of 0.5 as well. Changing the angular drag to 2 while leaving the main drag the same fixed my spin problem, while setting the angular drag to 5 made things too sluggish.
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    The problem with that method is it stops all rotation... if you only want it to stop rotating around one axis, you need to apply negative force...
     
  8. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    After dealing with some other aspects of my game, I decided to try refining my code to add negative force as additional rotational drag for the roll axis (and only the roll axis). This is what I came up with:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var rollSpeed : float = 100.0; //rolling rotation speed
    5.  
    6. var pitchSpeed : float = 100.0; //vertical rotation speed
    7.  
    8. var yawSpeed : float = 100.0; //horizontal rotation speed
    9.  
    10. var accelerate : float = 20000; //main engine thrust
    11.  
    12. var velocity;
    13.  
    14. var rotSpeed = 15;
    15.  
    16. var leftForwardWing : GameObject;
    17.  
    18. var rightForwardWing : GameObject;
    19.  
    20. var leftTailPlane : GameObject;
    21.  
    22. var rightTailPlane : GameObject;
    23.  
    24. var thrust : float = 0;
    25.  
    26. var maxThrust : float = 600000;
    27.  
    28. var rollDragFactor : float = 1;
    29.  
    30. var liftFactor : float = 0.1;
    31.  
    32. var crashThreshold = 10.0;
    33.  
    34. var crashSound : AudioSource;
    35.  
    36. private var oldRoll : float = 0;
    37.  
    38. private var oldVelocity : float;
    39.    
    40. private var speed : float = 0.0;
    41.  
    42. // Use this for initialization
    43. function Start ()
    44. {
    45.     animation["Flying"].wrapMode = WrapMode.Loop;
    46.    
    47.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    48.     crashThreshold *= crashThreshold;   // So it works with sqrMagnitude
    49.        
    50.     //posX = transform.position.x;
    51.     //posY = transform.position.y;
    52.     //posZ = transform.position.z;
    53.        
    54.     //rigidbody.AddForce(0,accelerate.y,0);
    55. }
    56.  
    57. function CrashCheck()
    58. {
    59.     if (oldVelocity - rigidbody.velocity.sqrMagnitude > crashThreshold) {
    60.         // TODO: Proper Crash Code
    61.         crashSound.Play();
    62.         Debug.Log("Penguin Crashed!!!");
    63.     }
    64.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    65. }
    66.    
    67. function KeepInBounds()
    68. {
    69.     //Wrap the penguin around the playfield so that you cannot fly under the landscape
    70.        
    71.     if (transform.position.x >= 1900.0) transform.position.x = 0;
    72.     else if (transform.position.x <= -1900.0) transform.position.x = 1900.0;
    73.     else if (transform.position.z >= 1900.0) transform.position.z = 0;
    74.     else if (transform.position.z <= -1900.0) transform.position.z = 1900.0;
    75.  
    76.     //Limit the height
    77.     if (transform.position.y > 1000) transform.position.y = 1000;
    78.        
    79.     animation.CrossFade("Flying");
    80. }
    81. function FixedUpdate()
    82. {
    83.     UpdateFunction();
    84.     CrashCheck();
    85.     KeepInBounds();
    86. }
    87.    
    88. function UpdateFunction()
    89. {
    90.     //Get inputs
    91.        
    92.     var roll : float = 0;
    93.     var pitch : float = 0;
    94.     var yaw : float = 0;
    95.     var lift : float = 0;
    96.        
    97.     roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed);
    98.     pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
    99.     yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
    100.     thrust = Input.GetAxis("Throttle") * (Time.deltaTime * accelerate);
    101.     lift = Input.GetAxis("Throttle") * (Time.deltaTime * rigidbody.velocity.y * liftFactor);
    102.        
    103.     oldRoll = roll;
    104.        
    105.         //Rotate the penguin on its axes
    106.     if (Input.GetAxis("Roll") == 0)
    107.         rigidbody.AddRelativeTorque(-pitch, yaw, oldRoll * rollDragFactor);
    108.     else
    109.         rigidbody.AddRelativeTorque(-pitch, yaw, -roll);
    110.        
    111.     // Move forwards
    112.     if (thrust >= 0)
    113.         rigidbody.AddRelativeForce(0,0, thrust);
    114.        
    115.     // Generate lift
    116.     rigidbody.AddForceAtPosition(Vector3(0, lift, 0), leftForwardWing.transform.position);
    117.     rigidbody.AddForceAtPosition(Vector3(0, lift, 0), rightForwardWing.transform.position);
    118.    
    119.     rigidbody.AddForceAtPosition(Vector3(0, lift, 0), leftTailPlane.transform.position);
    120.     rigidbody.AddForceAtPosition(Vector3(0, lift, 0), rightTailPlane.transform.position);
    121.        
    122.     KeepInBounds();
    123. }
    124.  
     
    Last edited: Apr 7, 2012
  9. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    You can get the angular velocity from the physics, convert it into local space, isolate components and apply any asymmetric angular drag functions you like. It all behaves mostly like the linear equivalents, and you won't go far wrong by just doing the obvious thing.
     
  10. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    I recently threw together a Javascript that may help you with this flight problem...


    var speed = 50.0;
    var rotateSpeed = 10.0;

    function Update () {
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * rotateSpeed;
    var y = Input.GetAxis("Roll") * Time.deltaTime * rotateSpeed;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * rotateSpeed;
    var accel = Input.GetAxis("Jump") * Time.deltaTime * speed;
    rigidbody.AddRelativeForce(0, 0, accel);
    rigidbody.AddRelativeTorque(z, x, -y);
    }


    Just attach this and a rigidbody (yes gravity for atmospheric flight, none for space) to your plane or spaceship game object and set the drag and angular drag (on rigidbody) to something between 1 and 10, then set the speed (on script inspector) to the drag * 1000, and the rotateSpeed (on script inspector) to the angular drag * 200 for a simple flight style. You can mess with the speed, drag, rotateSpeed, and angular drag to affect how your little flyer handles. You'll also need to create a new input (edit>project settings>input) called "Roll" and set the buttons to get its input for the script. Let me know if you need any specifications for the settings on the "Roll" input.

    Controls: w/s for down/up angle, a/d for left/right turn, q/e for left/right rotate (do a barrel roll when you test it out!), space bar for acceleration.

    You could also change "rigidbody.AddRelativeForce" and "rigidbody.AddRelativeTorque" to "transform.Translate" and "transform.Rotate," respectively, in order to make the controls more responsive, but it's also kinda dull to fly that way...

    C&C appreciated!

    -Orbitus2
     
  11. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    Asa matter of fat, your solution is pretty similar to mine:

    Code (csharp):
    1.  
    2.  
    3. var rollSpeed : float = 100.0; //rolling rotation speed
    4.  
    5. var pitchSpeed : float = 100.0; //vertical rotation speed
    6.  
    7. var yawSpeed : float = 100.0; //horizontal rotation speed
    8.  
    9. ...
    10.  
    11. function UpdateOrientation()
    12. {
    13.  
    14.         var roll : float = 0;
    15.         var pitch : float = 0;
    16.         var yaw : float = 0;
    17.        
    18.         //Get inputs
    19.        
    20.         if (isControllable)
    21.         {
    22.             roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed);
    23.             pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
    24.             yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
    25.            
    26.             thrust += Input.GetAxis("Throttle") * (Time.deltaTime * accelerate);
    27.            
    28.             if (Mathf.Abs(thrust) > maxThrust)
    29.             {
    30.                 if (thrust > 0)
    31.                     thrust = maxThrust;
    32.             }
    33.             else if (thrust < 0)
    34.                     thrust = 0;
    35.            
    36.             animation["Flying"].speed = (rigidbody.velocity.magnitude * thrust / 10000);
    37.            
    38.             //Rotate the penguin on its axes
    39.             if (Input.GetAxis("Roll") == 0)
    40.                 rigidbody.AddRelativeTorque(-pitch, yaw, -oldRoll * rollDragFactor);
    41.             else
    42.                 rigidbody.AddRelativeTorque(-pitch, yaw, -roll);
    43.         }
    44.         else
    45.         {
    46.             thrust = (Time.deltaTime * accelerate);
    47.             lift = thrust * (rigidbody.velocity.z * liftFactor);
    48.         }
    49.        
    50.         lift = Mathf.Abs(rigidbody.velocity.z * liftFactor);
    51.        
    52.         oldRoll = roll;
    53.        
    54.         // Move forwards
    55.         if (thrust >= 0)
    56.             rigidbody.AddRelativeForce(0,lift, thrust);
    57.            
    58.         // Fire exhaust
    59.        
    60.         if (thrust <= 0)
    61.         {
    62.             exhaust.Stop();
    63.             exhaust.Clear();
    64.         }
    65.         else
    66.         {
    67.             exhaust.Play();
    68.             exhaustLight.intensity = Mathf.Abs(thrust / maxThrust);
    69.             if (thrust > 0)
    70.                 exhaust.emissionRate = (thrust / maxThrust) * 2000;
    71.         }
    72. }
    73.  
    You can adjust the different rotation speeds to create different turning effects. Thanks for the contribution.

    MachCUBED
     
  12. OrbitusII

    OrbitusII

    Joined:
    Jul 4, 2011
    Posts:
    175
    No problem, and you were the one who got me thinking, so thanks to you as well!

    -Orbitus2
     
  13. ASMach

    ASMach

    Joined:
    Nov 26, 2011
    Posts:
    43
    Here's a new version for iOS devices. Unfortunately, it's really touchy on roll inputs, while being sluggish on pitch inputs. This data is from a rigidbody with a mass of 0.5kg, angular drag of 0.05, and drag of 0.0025.

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var rollSpeed : float = 0.0005; //rolling rotation speed
    5.  
    6. var pitchSpeed : float = 125.0; //vertical rotation speed
    7.  
    8. var yawSpeed : float = 1000.0; //horizontal rotation speed
    9.  
    10. var accelerate : float = 600; //main engine thrust
    11.  
    12. var velocity;
    13.  
    14. var thrust : float = 1;
    15.  
    16. var groundHit : RaycastHit;
    17.  
    18. var maxThrust : float = 5;
    19.  
    20. var rollDragFactor : float = 5;
    21.  
    22. var liftFactor : float = 0.3;
    23.  
    24. var crashThreshold = 10.0;
    25.  
    26. var crashSound : AudioClip;
    27.  
    28. var stallWarning : AudioClip;
    29.  
    30. var voiceSounds: AudioClip[];
    31.  
    32. var poopChute : ParticleSystem;
    33.  
    34. var maxHeight : int = 1000;
    35.  
    36. var flightBounds : Vector2 = Vector2(1000,1000);
    37.  
    38. var maxLife : int = 100;
    39.  
    40. var life : int = maxLife;
    41.  
    42. var throttlePad : Joystick;
    43.  
    44. private var oldRoll : float = 0;
    45.  
    46. private var oldVelocity : float;
    47.    
    48. private var speed : float = 0.0;
    49.  
    50. private var lift : float = 0;
    51.  
    52. private var sensitivity : float = 1.0;
    53.  
    54. private var controlType : String;
    55.  
    56. protected var isControllable = true;
    57.  
    58. private var gyroBool : boolean;
    59.  
    60. private var gyro : Gyroscope;
    61.  
    62. private var rotFix : Quaternion;
    63.  
    64.  
    65. // Use this for initialization
    66.  
    67. function Start ()
    68. {
    69.     animation["Flying"].wrapMode = WrapMode.Loop;
    70.    
    71.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    72.     crashThreshold *= crashThreshold;   // So it works with sqrMagnitude
    73.    
    74.     NotificationCenter.DefaultCenter().AddObserver(this, "PlayerDeactivated");
    75.     NotificationCenter.DefaultCenter().AddObserver(this, "PlayerControllable");
    76.    
    77.     //Setup controls
    78.    
    79.      gyroBool = SystemInfo.supportsGyroscope;
    80.      
    81.      if (gyroBool)
    82.      {
    83.  
    84.         gyro = Input.gyro;
    85.  
    86.         gyro.enabled = true;
    87.  
    88.         if (Screen.orientation == ScreenOrientation.LandscapeRight)
    89.         {
    90.             rotFix = Quaternion(0,0,0.7071,0.7071);
    91.         }
    92.     }
    93.     else
    94.     {
    95.         // Add accelerometer controls and/or keyboard/gamepad controls
    96.         print("NO GYRO");
    97.  
    98.     }
    99.    
    100.     sensitivity = PlayerPrefs.GetFloat("Sensitivity", 1.0);
    101.    
    102.     // Setup lifespan and thrust from PlayerPrefs
    103.    
    104.     var defaultMaxThrust : int = maxThrust;
    105.     maxThrust = PlayerPrefs.GetInt("Thrust", defaultMaxThrust);
    106.    
    107.     var defaultMaxLife : int = maxLife;
    108.     maxLife = PlayerPrefs.GetInt("Life", defaultMaxLife);
    109.    
    110.     life = maxLife;
    111. }
    112.  
    113. function DistressHonk()
    114. {
    115.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    116.     life -= oldVelocity / 100;
    117.        
    118.     if (life <= 0)
    119.     {
    120.         life = 0;
    121.         PlayerDeactivated();
    122.     }
    123.    
    124.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    125. }
    126.    
    127. function KeepInBounds()
    128. {
    129.     //Wrap the penguin around the playfield so that you cannot fly under the landscape
    130.        
    131.     if (transform.position.x >= flightBounds.x / 2) transform.position.x = flightBounds.x / 2;
    132.     else if (rigidbody.position.x <= -(flightBounds.x / 2)) rigidbody.position.x = -(flightBounds.x / 2);
    133.     else if (rigidbody.position.z >= flightBounds.y / 2) rigidbody.position.z = flightBounds.y / 2;
    134.     else if (rigidbody.position.z <= -(flightBounds.y / 2)) rigidbody.position.z = -(flightBounds.y / 2);
    135.  
    136.     //Limit the height
    137.     if (transform.position.y > maxHeight) transform.position.y = maxHeight;
    138. }
    139. function FixedUpdate()
    140. {
    141.     animation.CrossFade("Flying");
    142.  
    143.     Physics.Raycast( rigidbody.position - Vector3.up, -Vector3.up, groundHit );
    144.    
    145.     UpdateOrientation();
    146.     CheckWarnings();
    147.     KeepInBounds();
    148.     PlayVoiceAudio();
    149. }
    150.    
    151. function UpdateOrientation()
    152. {
    153.  
    154.         var roll : float = 0;
    155.         var pitch : float = 0;
    156.         var yaw : float = 0;
    157.        
    158.         var dir : Vector3 = Vector3.zero;
    159.        
    160.         //Get inputs
    161.        
    162.         if (isControllable)
    163.         {
    164.             // we assume that the device is held parallel to the ground
    165.             // and the Home button is in the right hand
    166.            
    167.             // remap the device acceleration axis to game coordinates:
    168.             //  1) XY plane of the device is mapped onto XZ plane
    169.             //  2) rotated 90 degrees around Y axis
    170.            
    171.             if (gyroBool)
    172.             {
    173.                 dir.x = gyro.rotationRateUnbiased.y; // Pitch
    174.                 dir.z = gyro.rotationRateUnbiased.x; // Yaw
    175.                 dir.y = -gyro.rotationRateUnbiased.z; // Roll
    176.             }
    177.             else
    178.             {
    179.                 dir.x = -Input.acceleration.y; // Pitch
    180.                 dir.z = Input.acceleration.x; // Yaw
    181.                 dir.y = Input.acceleration.z; // Roll
    182.             }
    183.            
    184.             // clamp acceleration vector to the unit sphere
    185.             if (dir.sqrMagnitude > 1)
    186.                 dir.Normalize();
    187.  
    188.             // Make it move at a consistent rate per time step...
    189.             dir *= Time.deltaTime;
    190.            
    191.             roll = dir.y * (Time.deltaTime * rollSpeed) * sensitivity;
    192.             pitch = dir.x * (Time.deltaTime * pitchSpeed) * sensitivity;
    193.             yaw = dir.z * (Time.deltaTime * yawSpeed) * sensitivity;
    194.            
    195.             thrust += (throttlePad.position.y) * (Time.deltaTime * accelerate);
    196.    
    197.             /*
    198.             // Code for Desktop version, if one is made in the future.
    199.            
    200.             roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed) * sensitivity;
    201.             pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed) * sensitivity;
    202.             yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed) * sensitivity;
    203.            
    204.             thrust += Input.GetAxis("Throttle") * (Time.deltaTime * accelerate);
    205.             */
    206.            
    207.             if (Mathf.Abs(thrust) > maxThrust)
    208.             {
    209.                 if (thrust > 0)
    210.                     thrust = maxThrust;
    211.             }
    212.             else if (thrust < 0)
    213.                     thrust = 0;
    214.            
    215.             animation["Flying"].speed = (rigidbody.velocity.magnitude / 10);
    216.            
    217.             //Rotate the pigeon on its axes
    218.             rigidbody.AddRelativeTorque(-pitch, yaw, -roll);
    219.         }
    220.         else
    221.         {
    222.             thrust = (Time.deltaTime * accelerate);
    223.             lift = thrust * (rigidbody.velocity.z * liftFactor);
    224.         }
    225.        
    226.         lift = Mathf.Abs(rigidbody.velocity.z * liftFactor);
    227.        
    228.         oldRoll = roll;
    229.        
    230.         // Move forwards
    231.         if (thrust >= 0)
    232.             rigidbody.AddRelativeForce(0,lift, thrust);
    233. }
    234.  
    235. function PlayVoiceAudio()
    236. {
    237. /*
    238.     if (rigidbody.velocity.magnitude * 3.6 == 100 || rigidbody.velocity.magnitude * 3.6 == 200 || rigidbody.velocity.magnitude * 3.6 == 500 || rigidbody.velocity.magnitude * 3.6 == 1000)
    239.     {
    240.         //Honk
    241.         if (audioSource.isPlaying)
    242.             return; // don't play a new sound while the last hasn't finished
    243.        
    244.         audioSource.clip = voiceSounds[Random.Range(0,voiceSounds.length)];
    245.         audioSource.Play();
    246.     }
    247.     */
    248. }
    249.  
    250. function CheckWarnings()
    251. {
    252.  
    253.     // Stall warning - for when there is no lift being generated
    254.    
    255.     //Debug.Log("Lift force:" + lift);
    256.    
    257.     if (lift <= 0)
    258.     {
    259.         audio.clip = stallWarning;
    260.         if (audio.isPlaying == false)
    261.             audio.Play();
    262.     }
    263.     else
    264.     {
    265.         if (audio.clip == stallWarning)
    266.             audio.Stop();
    267.     }
    268.  
    269. }
    270.  
    271. function OnCollisionEnter ()
    272. {
    273.     audio.clip = crashSound;
    274.     audio.Play();
    275.     DistressHonk();
    276. }
    277.  
    278. function PlayerDeactivated ()
    279. {
    280.     isControllable = false;
    281. }
    282.  
    283. function PlayerControllable ()
    284. {
    285.     isControllable = true;
    286. }
    287.  
    Does anyone have any feedback or suggestions?

    MachCUBED
     
    Last edited: Jul 30, 2012