WheelDrive.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. private void Start()
  48. {
  49. wheels = GetComponentsInChildren<WheelCollider>();
  50. for (int i = 0; i < wheels.Length; ++i)
  51. {
  52. var wheel = wheels[i];
  53. // Create wheel shapes only when needed.
  54. if (leftWheelShape != null && wheel.transform.localPosition.x < 0)
  55. {
  56. var ws = Instantiate(leftWheelShape);
  57. ws.transform.parent = wheel.transform;
  58. }
  59. else if (rightWheelShape != null && wheel.transform.localPosition.x > 0)
  60. {
  61. var ws = Instantiate(rightWheelShape);
  62. ws.transform.parent = wheel.transform;
  63. }
  64. wheel.ConfigureVehicleSubsteps(10, 1, 1);
  65. }
  66. }
  67. public void Move(float _acceleration, float _steering, float _brake)
  68. {
  69. float nSteering = Mathf.Lerp(currentSteering, _steering, Time.deltaTime * steeringLerp);
  70. currentSteering = nSteering;
  71. Rigidbody rb = this.GetComponent<Rigidbody>();
  72. float angle = maxAngle * nSteering;
  73. float torque = maxTorque * _acceleration;
  74. float handBrake = _brake > 0 ? brakeTorque : 0;
  75. foreach (WheelCollider wheel in wheels){
  76. // Steer front wheels only
  77. if (wheel.transform.localPosition.z > 0) wheel.steerAngle = angle;
  78. if (wheel.transform.localPosition.z < 0) wheel.brakeTorque = handBrake;
  79. if (wheel.transform.localPosition.z < 0 && driveType != DriveType.FrontWheelDrive) wheel.motorTorque = torque;
  80. if (wheel.transform.localPosition.z >= 0 && driveType != DriveType.RearWheelDrive) wheel.motorTorque = torque;
  81. // Update visual wheels if allowed
  82. if(animateWheels){
  83. Quaternion q;
  84. Vector3 p;
  85. wheel.GetWorldPose(out p, out q);
  86. Transform shapeTransform = wheel.transform.GetChild (0);
  87. shapeTransform.position = p;
  88. shapeTransform.rotation = q;
  89. }
  90. }
  91. //Apply speed
  92. float s = GetSpeedUnit(rb.velocity.magnitude);
  93. if(s > maxSpeed) rb.velocity = GetSpeedMS(maxSpeed) * rb.velocity.normalized;
  94. //Apply downforce
  95. rb.AddForce(-transform.up * downForce * rb.velocity.magnitude);
  96. }
  97. public float GetSpeedMS(float _s){
  98. return unitType == UnitType.KMH ? _s / 3.6f : _s / 2.237f;
  99. }
  100. public float GetSpeedUnit(float _s){
  101. return unitType == UnitType.KMH ? _s * 3.6f : _s * 2.237f;
  102. }
  103. }
  104. }