Andy73

CarController

Aug 7th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ----------- CAR TUTORIAL-----------------
  2.  
  3. // These variables allow the script to power the wheels of the car.
  4. var FrontLeftWheel : WheelCollider;
  5. var FrontRightWheel : WheelCollider;
  6.  
  7. // These variables are for the gears, the array is the list of ratios. The script
  8. // uses the defined gear ratios to determine how much torque to apply to the wheels.
  9. var GearRatio : float[];
  10. var CurrentGear : int = 0;
  11.  
  12. // These variables are just for applying torque to the wheels and shifting gears.
  13. // using the defined Max and Min Engine RPM, the script can determine what gear the
  14. // car needs to be in.
  15. var EngineTorque : float = 230.0;
  16. var MaxEngineRPM : float = 3000.0;
  17. var MinEngineRPM : float = 1000.0;
  18. private var EngineRPM : float = 0.0;
  19.  
  20.  
  21.  
  22. function Start () {
  23.     // I usually alter the center of mass to make the car more stable. I'ts less likely to flip this way.
  24. rigidbody.centerOfMass += Vector3(0, -.75, .25);
  25.     }
  26.  
  27. function Update () {
  28.    
  29.         // Compute the engine RPM based on the average RPM of the two wheels, then call the shift gear function
  30.     EngineRPM = (FrontLeftWheel.rpm + FrontRightWheel.rpm)/2 * GearRatio[CurrentGear];
  31.     ShiftGears();
  32.  
  33.     // set the audio pitch to the percentage of RPM to the maximum RPM plus one, this makes the sound play
  34.     // up to twice it's pitch, where it will suddenly drop when it switches gears.
  35.     audio.pitch = Mathf.Abs(EngineRPM / MaxEngineRPM) + 1.0 ;
  36.     // this line is just to ensure that the pitch does not reach a value higher than is desired.
  37.     if ( audio.pitch > 2.0 ) {
  38.         audio.pitch = 2.0;
  39.     }
  40.  
  41.     // finally, apply the values to the wheels. The torque applied is divided by the current gear, and
  42.     // multiplied by the user input variable.
  43.     FrontLeftWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
  44.     FrontRightWheel.motorTorque = EngineTorque / GearRatio[CurrentGear] * Input.GetAxis("Vertical");
  45.        
  46.     // the steer angle is an arbitrary value multiplied by the user input.
  47.     FrontLeftWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
  48.     FrontRightWheel.steerAngle = 10 * Input.GetAxis("Horizontal");
  49. }
  50.  
  51. function ShiftGears() {
  52.     // this funciton shifts the gears of the vehcile, it loops through all the gears, checking which will make
  53.     // the engine RPM fall within the desired range. The gear is then set to this "appropriate" value.
  54.     if ( EngineRPM >= MaxEngineRPM ) {
  55.         var AppropriateGear : int = CurrentGear;
  56.        
  57.         for ( var i = 0; i < GearRatio.length; i ++ ) {
  58.             if ( FrontLeftWheel.rpm * GearRatio[i] < MaxEngineRPM ) {
  59.                 AppropriateGear = i;
  60.                 break;
  61.             }
  62.         }
  63.        
  64.         CurrentGear = AppropriateGear;
  65.     }
  66.    
  67.     if ( EngineRPM <= MinEngineRPM ) {
  68.         AppropriateGear = CurrentGear;
  69.        
  70.         for ( var j = GearRatio.length-1; j >= 0; j -- ) {
  71.             if ( FrontLeftWheel.rpm * GearRatio[j] > MinEngineRPM ) {
  72.                 AppropriateGear = j;
  73.                 break;
  74.             }
  75.         }
  76.        
  77.         CurrentGear = AppropriateGear;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment