Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. var wheelFL : Transform;
  2. var wheelFR : Transform;
  3. var wheelML : Transform;
  4. var wheelMR : Transform;
  5. var wheelBL : Transform;
  6. var wheelBR : Transform;
  7.  
  8. //automatic transmission shift points
  9. var shiftDownRPM = 2500;
  10. var shiftUpRPM = 5500;
  11.  
  12. //gear ratios
  13. var gearRatios = [-2.66, 2.66, 1.78, 1.30, 1.00];
  14. var finalDriveRatio = 3.4;
  15.  
  16.  
  17.  
  18. var frontWheelSteering = false;
  19. var middleWheelSteering = false;
  20. var rearWheelSteering = false;
  21.  
  22. var frontWheelDrive = false;
  23. var middleWheelDrive = false;
  24. var rearWheelDrive = false;
  25.  
  26. var minSteeringAngle = 0.00;
  27. var maxSteeringAngle = 0.00;
  28. var highSpeed = 0.00;
  29.  
  30. var motorMass = 1.00;
  31. var motorDrag = 0.00;
  32. var maxAcceleration = 0.00;
  33. var maxMotorSpeed = 0.00;
  34. var minTorque = 0.00;
  35. var maxTorque = 0.00;
  36.  
  37. var centerOfMass : Vector3;
  38. rigidbody.centerOfMass = centerOfMass;
  39.  
  40.  
  41. private var queryUserInput = true;
  42. private var engineRPM : float;
  43. private var onGround = false;
  44. private var wheelRPM : float;
  45. private var gear = 1;
  46.  
  47.  
  48. private var scriptWheelFL : Wheel;
  49. private var scriptWheelFR : Wheel;
  50. private var scriptWheelML : Wheel;
  51. private var scriptWheelMR : Wheel;
  52. private var scriptWheelBL : Wheel;
  53. private var scriptWheelBR : Wheel;
  54. scriptWheelFL = wheelFL.GetComponent(Wheel);
  55. scriptWheelFR = wheelFR.GetComponent(Wheel);
  56. scriptWheelML = wheelML.GetComponent(Wheel);
  57. scriptWheelMR = wheelMR.GetComponent(Wheel);
  58. scriptWheelBL = wheelBL.GetComponent(Wheel);
  59. scriptWheelBR = wheelBR.GetComponent(Wheel);
  60. scriptWheelFL.car = this;
  61. scriptWheelFR.car = this;
  62. scriptWheelML.car = this;
  63. scriptWheelMR.car = this;
  64. scriptWheelBL.car = this;
  65. scriptWheelBR.car = this;
  66.  
  67. var steeringInput = 0.00;
  68. var motorInput = 0.00;
  69.  
  70. var motorTorque = 0.00;
  71. var motorAccel = 0.00;
  72. var motorSpeed = 0.00;
  73. var steeringAngle = 0.00;
  74.  
  75. if(frontWheelDrive) scriptWheelFL.driven = scriptWheelFR.driven = true;
  76. if(middleWheelDrive) scriptWheelML.driven = scriptWheelMR.driven = true;
  77. if(rearWheelDrive) scriptWheelBL.driven = scriptWheelBR.driven = true;
  78.  
  79. function FixedUpdate ()
  80. {
  81. steeringInput = Input.GetAxis("Horizontal");
  82. motorInput = Input.GetAxis("Vertical");
  83.  
  84. if(frontWheelSteering) wheelFL.localRotation = wheelFR.localRotation = Quaternion.LookRotation(Vector3(steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
  85. if(middleWheelSteering) wheelML.localRotation = wheelMR.localRotation = Quaternion.LookRotation(Vector3(-steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
  86. if(rearWheelSteering) wheelBL.localRotation = wheelBR.localRotation = Quaternion.LookRotation(Vector3(-steeringInput * (steeringAngle / 90), 0, 1 + (-1 * Mathf.Abs(steeringInput * (steeringAngle / 90))) ));
  87.  
  88. if(frontWheelDrive || middleWheelDrive || rearWheelDrive)
  89. {
  90. motorTorque = Mathf.Lerp(maxTorque / 30, minTorque / 30, motorSpeed / maxMotorSpeed) * Mathf.Abs(motorInput);
  91. if(motorTorque < 1) motorTorque = 1;
  92. motorAccel = Mathf.Lerp(maxAcceleration, 0, motorSpeed / maxMotorSpeed);
  93. steeringAngle = Mathf.Lerp(maxSteeringAngle, minSteeringAngle, rigidbody.velocity.magnitude / highSpeed);
  94.  
  95. motorSpeed += motorInput * motorAccel / motorMass * Time.fixedDeltaTime;
  96.  
  97. if(frontWheelDrive)
  98. {
  99. scriptWheelFL.speed = scriptWheelFR.speed = motorSpeed;
  100. }
  101.  
  102. if(middleWheelDrive)
  103. {
  104. scriptWheelML.speed = scriptWheelMR.speed = motorSpeed;
  105. }
  106.  
  107. if(rearWheelDrive)
  108. {
  109. scriptWheelBL.speed = scriptWheelBR.speed = motorSpeed;
  110. }
  111.  
  112. motorSpeed += -motorSpeed * motorDrag / motorTorque * Time.fixedDeltaTime;
  113. }
  114. }
  115.  
  116. //Automatically shift gears
  117. function AutomaticTransmission()
  118. {
  119. if(gear>0)
  120. {
  121. if(engineRPM>shiftUpRPM&&gear<gearRatios.length-1)
  122. gear++;
  123. if(engineRPM<shiftDownRPM&&gear>1)
  124. gear--;
  125. }
  126. }
  127.  
  128. function AddForceOnMotor (force : float)
  129. {
  130. motorSpeed += force / motorTorque;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement