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

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

  1. MachCUBED

    MachCUBED

    New Member

    Joined:
    Nov 26, 2011
    Messages:
    40
    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.  
    13. var rotSpeed = 15;
    14.  
    15. var planform;
    16.  
    17. var thrust : float = 0;
    18. var maxThrust : float = 600000;
    19.    
    20. var liftFactor : float = 0.1;
    21.  
    22. var crashThreshold = 10.0;
    23.  
    24. var crashSound : AudioSource;
    25.  
    26. private var oldVelocity : float;
    27.    
    28. private var speed : float = 0.0;
    29.  
    30.  
    31. // Use this for initialization
    32. function Start ()
    33. {
    34.     animation["Flying"].wrapMode = WrapMode.Loop;
    35.    
    36.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    37.     crashThreshold *= crashThreshold;   // So it works with sqrMagnitude
    38. }
    39.  
    40. function CrashCheck()
    41. {
    42.     if (oldVelocity - rigidbody.velocity.sqrMagnitude > crashThreshold) {
    43.         // TODO: Proper Crash Code
    44.         crashSound.Play();
    45.         Debug.Log("Penguin Crashed!!!");
    46.     }
    47.     oldVelocity = rigidbody.velocity.sqrMagnitude;
    48. }
    49.    
    50. function KeepInBounds()
    51. {
    52.     //Wrap the penguin around the playfield so that you cannot fly under the landscape
    53.        
    54.     if (transform.position.x >= 1900.0) transform.position.x = 0;
    55.     else if (transform.position.x <= -1900.0) transform.position.x = 1900.0;
    56.     else if (transform.position.z >= 1900.0) transform.position.z = 0;
    57.     else if (transform.position.z <= -1900.0) transform.position.z = 1900.0;
    58.  
    59.     //Limit the height
    60.     if (transform.position.y > 1000) transform.position.y = 1000;
    61.        
    62.     animation.CrossFade("Flying");
    63. }
    64.     function FixedUpdate()
    65.     {
    66.         UpdateFunction();
    67.         CrashCheck();
    68.         KeepInBounds();
    69.     }
    70.    
    71.     function UpdateFunction()
    72.     {
    73.          
    74.         var addRot : Quaternion = Quaternion.identity;
    75.        
    76.         var roll : float = 0;
    77.         var pitch : float = 0;
    78.         var yaw : float = 0;
    79.         var lift : float = 0;
    80.        
    81.         roll = Input.GetAxis("Roll") * (Time.deltaTime * rollSpeed);
    82.         pitch = Input.GetAxis("Pitch") * (Time.deltaTime * pitchSpeed);
    83.         yaw = Input.GetAxis("Yaw") * (Time.deltaTime * yawSpeed);
    84.         thrust = Input.GetAxis("Throttle") * (Time.deltaTime * accelerate);
    85.         lift = Input.GetAxis("Throttle") * (Time.deltaTime * rigidbody.velocity.y * liftFactor);
    86.  
    87.         rigidbody.AddTorque(-pitch, yaw, -roll);
    88.         if (thrust >= 0)
    89.         rigidbody.AddRelativeForce(0,thrust, lift);
    90.        
    91.         //TODO: Add force at two points on a wing for stability
    92.        
    93.         KeepInBounds();
    94.     }
    95.  
    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

    Member

    Joined:
    Feb 13, 2012
    Messages:
    240
    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

    New Member

    Joined:
    Feb 22, 2012
    Messages:
    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. MachCUBED

    MachCUBED

    New Member

    Joined:
    Nov 26, 2011
    Messages:
    40
    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

    Member

    Joined:
    Nov 15, 2011
    Messages:
    3,509
    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. MachCUBED

    MachCUBED

    New Member

    Joined:
    Nov 26, 2011
    Messages:
    40
    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

    Member

    Joined:
    Nov 15, 2011
    Messages:
    3,509
    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. MachCUBED

    MachCUBED

    New Member

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

    George Foot

    New Member

    Joined:
    Feb 22, 2012
    Messages:
    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

    Member

    Joined:
    Jul 4, 2011
    Messages:
    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. MachCUBED

    MachCUBED

    New Member

    Joined:
    Nov 26, 2011
    Messages:
    40
    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

    Member

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

    -Orbitus2
  13. MachCUBED

    MachCUBED

    New Member

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

    MachCUBED
    Last edited: Jul 30, 2012