Goose30

car script

Apr 4th, 2026
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | Source Code | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. public class Car : MonoBehaviour
  6. {
  7. [Header("Wheel Details")]
  8. [SerializeField] private Wheel[] wheels;
  9. [SerializeField] private float maxTorque = 450f; // max engine torque
  10. [SerializeField] private float wheelFrictionCoefficient = 0.022f; // coefficient of friction for the tires, affects how much grip they have
  11. [SerializeField] private float brakingForce = 8000f; // max braking force the car can apply, should be much higher than max torque
  12. [SerializeField] private float frontAxleZ = 1.5f; // distance of front axle from the center of mass
  13. [SerializeField] private float rearAxleZ = -1.5f; // distance of rear axle from the center of mass
  14.  
  15. [Header("Suspension")]
  16. [SerializeField] private float suspensionForce = 90f; // Spring constant
  17. [SerializeField] private float suspensionDamping = 2.5f; // Damping constant
  18. [SerializeField] private float maxSuspensionForce = 200f; // Max force the suspension can apply
  19.  
  20. [Header("Car Details")]
  21. [SerializeField] private float mass = 2100f; // weight in kg
  22. [SerializeField] private float dragCoefficient = 0.3f; // affects how much air resistance the car has, should be between 0.25 and 0.35 for a typical car
  23. [SerializeField] private Vector3 centerOfGravity = Vector3.zero;
  24. private float rollingResistanceCoefficient; // roughly 30 times the drag coefficient
  25.  
  26. private Engine engine;
  27. private InputAction throttleInput;
  28. private InputAction steeringInput;
  29. private InputAction brakeInput;
  30. private Rigidbody rb;
  31. private float throttle;
  32. private float steering;
  33. private float brake;
  34. private float weight;
  35. private float wheelBase;
  36. private float driveWheelRPM = 0f;
  37. private float torque = 0f;
  38. private Vector3 dragForce = Vector3.zero;
  39. private Vector3 rollingResistanceForce = Vector3.zero;
  40. private float velocity = 0f;
  41.  
  42. void Start()
  43. {
  44. engine = GetComponent<Engine>();
  45. rb = GetComponent<Rigidbody>();
  46. throttleInput = InputSystem.actions.FindAction("Throttle");
  47. steeringInput = InputSystem.actions.FindAction("Steer");
  48. brakeInput = InputSystem.actions.FindAction("Brake");
  49. rollingResistanceCoefficient = 30 * dragCoefficient;
  50. weight = mass * Physics.gravity.magnitude;
  51. wheelBase = Mathf.Abs(frontAxleZ - rearAxleZ);
  52.  
  53. engine.StartEngine();
  54. }
  55.  
  56. void Update()
  57. {
  58. throttle = throttleInput.ReadValue<float>();
  59. steering = steeringInput.ReadValue<float>();
  60. brake = brakeInput.ReadValue<float>();
  61. }
  62.  
  63. void FixedUpdate()
  64. {
  65. velocity = rb.linearVelocity.magnitude;
  66. torque = engine.GetTorque(wheels[0].radius, throttle);
  67.  
  68. // Negative forces
  69. dragForce = -velocity * dragCoefficient * rb.linearVelocity.normalized;
  70. rollingResistanceForce = -velocity * rollingResistanceCoefficient * rb.linearVelocity.normalized; // eventually make per wheel based on weight distribution
  71.  
  72. // RPM calculations
  73. driveWheelRPM = (velocity / (2 * Mathf.PI * wheels[0].radius)) * 60f;
  74. engine.SetRPM(driveWheelRPM * engine.gearRatios[engine.gear] * engine.finalDriveRatio);
  75.  
  76. // Weight distribution for front and rear axles
  77. float acceleration = velocity / Time.deltaTime;
  78. float yForce = acceleration * mass * (centerOfGravity.y / wheelBase);
  79. float frontWeight = (weight * (Mathf.Abs(frontAxleZ - centerOfGravity.z) / wheelBase)) - yForce;
  80. float rearWeight = (weight * (Mathf.Abs(rearAxleZ - centerOfGravity.z) / wheelBase)) + yForce;
  81.  
  82. // Debug.Log($"rpm: {engine.rpm}, drive wheel rpm: {driveWheelRPM}, torque: {torque} ft-lb");
  83. // Debug.Log($"speed: {rb.linearVelocity.magnitude * 3.6f} km/h");
  84. // Debug.Log($"rear weight: {rearWeight} N, front weight: {frontWeight} N, y force: {yForce} N");
  85. // Debug.Log($"acceleration: {acceleration} m/s^2");
  86.  
  87. foreach (Wheel wheel in wheels)
  88. {
  89. GameObject wheelObject = wheel.gameObject;
  90. Transform wheelTransform = wheelObject.transform;
  91.  
  92. if (wheel.powered)
  93. {
  94. Vector3 driveForce = (transform.forward * torque + dragForce + rollingResistanceForce);
  95. Vector3 brakeForce = (brake * brakingForce * -transform.forward + dragForce + rollingResistanceForce);
  96.  
  97. wheelTransform.Rotate(Vector3.right, driveWheelRPM * 360f * Time.fixedDeltaTime); // rotate the wheel based on its RPM
  98.  
  99. Debug.Log($"drive force: {driveForce.magnitude} N, brake force: {brakeForce.magnitude} N ");
  100. if (rb.linearVelocity.normalized.z > 1f)
  101. {
  102. rb.AddForceAtPosition(brakeForce, wheelTransform.position);
  103. }
  104. rb.AddForceAtPosition(driveForce, wheelTransform.position);
  105. }
  106.  
  107. if (wheel.steering)
  108. {
  109. float targetAngle = wheel.steerAngle * steering;
  110. Quaternion targetRotation = Quaternion.Euler(0f, targetAngle, 0f);
  111. wheelTransform.localRotation = Quaternion.Lerp(
  112. wheelTransform.localRotation,
  113. targetRotation,
  114. Time.fixedDeltaTime * 100f
  115. );
  116. }
  117. }
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment