WheelDrive.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. // Based on the Vehicle Tools package from Unity
  4. using UnityEngine;
  5. using System;
  6. namespace TrafficSimulation{
  7. [Serializable]
  8. public enum DriveType{
  9. RearWheelDrive,
  10. FrontWheelDrive,
  11. AllWheelDrive
  12. }
  13. [Serializable]
  14. public enum UnitType{
  15. KMH,
  16. MPH
  17. }
  18. public class WheelDrive : MonoBehaviour
  19. {
  20. [Tooltip("Downforce applied to the vehicle")]
  21. public float downForce = 100f;
  22. [Tooltip("Maximum steering angle of the wheels")]
  23. public float maxAngle = 30f;
  24. [Tooltip("Speed at which we will reach the above steering angle (lerp)")]
  25. public float steeringLerp = 5f;
  26. [Tooltip("Max speed (in unit choosen below) when the vehicle is about to steer")]
  27. public float steeringSpeedMax = 20f;
  28. [Tooltip("Maximum torque applied to the driving wheels")]
  29. public float maxTorque = 300f;
  30. [Tooltip("Maximum brake torque applied to the driving wheels")]
  31. public float brakeTorque = 30000f;
  32. [Tooltip("Unit Type")]
  33. public UnitType unitType;
  34. [Tooltip("Min Speed - when driving (not including stops/brakes), in the unit choosen above. Should be > 0.")]
  35. public float minSpeed = 5;
  36. [Tooltip("Max Speed in the unit choosen above")]
  37. public float maxSpeed = 50;
  38. [Tooltip("Drag the wheel shape here.")]
  39. public GameObject leftWheelShape;
  40. public GameObject rightWheelShape;
  41. [Tooltip("Whether you want to animate the wheels")]
  42. public bool animateWheels = true;
  43. [Tooltip("The vehicle's drive type: rear-wheels drive, front-wheels drive or all-wheels drive.")]
  44. public DriveType driveType;
  45. private WheelCollider[] wheels;
  46. private float currentSteering = 0f;
  47. void OnEnable(){
  48. wheels = GetComponentsInChildren<WheelCollider>();
  49. for (int i = 0; i < wheels.Length; ++i)
  50. {
  51. var wheel = wheels [i];
  52. // Create wheel shapes only when needed.
  53. if (leftWheelShape != null && wheel.transform.localPosition.x < 0)
  54. {
  55. var ws = Instantiate (leftWheelShape);
  56. ws.transform.parent = wheel.transform;
  57. }
  58. else if(rightWheelShape != null && wheel.transform.localPosition.x > 0){
  59. var ws = Instantiate(rightWheelShape);
  60. ws.transform.parent = wheel.transform;
  61. }
  62. wheel.ConfigureVehicleSubsteps(10, 1, 1);
  63. }
  64. }
  65. public void Move(float _acceleration, float _steering, float _brake)
  66. {
  67. float nSteering = Mathf.Lerp(currentSteering, _steering, Time.deltaTime * steeringLerp);
  68. currentSteering = nSteering;
  69. Rigidbody rb = this.GetComponent<Rigidbody>();
  70. float angle = maxAngle * nSteering;
  71. float torque = maxTorque * _acceleration;
  72. float handBrake = _brake > 0 ? brakeTorque : 0;
  73. foreach (WheelCollider wheel in wheels){
  74. // Steer front wheels only
  75. if (wheel.transform.localPosition.z > 0) wheel.steerAngle = angle;
  76. if (wheel.transform.localPosition.z < 0) wheel.brakeTorque = handBrake;
  77. if (wheel.transform.localPosition.z < 0 && driveType != DriveType.FrontWheelDrive) wheel.motorTorque = torque;
  78. if (wheel.transform.localPosition.z >= 0 && driveType != DriveType.RearWheelDrive) wheel.motorTorque = torque;
  79. // Update visual wheels if allowed
  80. if(animateWheels){
  81. Quaternion q;
  82. Vector3 p;
  83. wheel.GetWorldPose(out p, out q);
  84. Transform shapeTransform = wheel.transform.GetChild (0);
  85. shapeTransform.position = p;
  86. shapeTransform.rotation = q;
  87. }
  88. }
  89. //Apply speed
  90. float s = GetSpeedUnit(rb.velocity.magnitude);
  91. if(s > maxSpeed) rb.velocity = GetSpeedMS(maxSpeed) * rb.velocity.normalized;
  92. //Apply downforce
  93. rb.AddForce(-transform.up * downForce * rb.velocity.magnitude);
  94. }
  95. public float GetSpeedMS(float _s){
  96. return unitType == UnitType.KMH ? _s / 3.6f : _s / 2.237f;
  97. }
  98. public float GetSpeedUnit(float _s){
  99. return unitType == UnitType.KMH ? _s * 3.6f : _s * 2.237f;
  100. }
  101. }
  102. }