Guest User

Wheel.cs

a guest
Nov 16th, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Wheel : MonoBehaviour
  4. {
  5. // Start is called once before the first execution of Update after the MonoBehaviour is created
  6. public bool Steering = true;
  7. public bool active = true;
  8. public float SpringLength = 0.5f;
  9. public float Velocity = 0;
  10. public float Strength = 50;
  11. public float DampeningMultiplier = 2f;
  12. public float SteerAngle = 0;
  13. public float MaxSteerAngle = 0;
  14. public float MaxSpeed = 60;
  15. public float friction = 0.9f;
  16. public bool onGround = false;
  17. public WheelCurve wc;
  18. public Vector2 Input = Vector2.zero;
  19. public Transform VisualWheel;
  20. public float startAngle = 0;
  21. public bool crashed = false;
  22.  
  23. private float visualRotationAngle = 0;
  24. private float visualStartY = 0;
  25. private float vwheelpos = 0;
  26. private float wheeldistance = 0;
  27. private float WheelRadius = 0.5f;
  28. private Rigidbody carRB;
  29. private float suslerp = 0;
  30. private float mult = 1;
  31. private float oldvelo = 0;
  32.  
  33. public float GetSpeed()
  34. {
  35. return Vector3.Dot(carRB.transform.forward,carRB.linearVelocity);
  36. }
  37.  
  38. public float getSteeringAngle()
  39. {
  40. return 6 * Input.x;
  41. }
  42.  
  43. void Start()
  44. {
  45. carRB = transform.parent.GetComponent<Rigidbody>();
  46.  
  47. if (VisualWheel != null)
  48. {
  49. WheelRadius = VisualWheel.GetComponent<Renderer>().bounds.size.y;
  50. visualStartY = VisualWheel.localPosition.y;
  51. startAngle = VisualWheel.localRotation.eulerAngles.y;
  52. SpringLength += WheelRadius / 2;
  53. }
  54.  
  55. }
  56.  
  57. // Update is called once per frame
  58. void FixedUpdate()
  59. {
  60. RaycastHit hit;
  61. wheeldistance = SpringLength;
  62. if (Physics.Raycast(transform.position, transform.up * -1, out hit, SpringLength))
  63. {
  64.  
  65. Vector3 WheelWorldVel = carRB.GetPointVelocity(transform.position);
  66.  
  67. float vel = Vector3.Dot(transform.up, WheelWorldVel);
  68. float CarSpeed = GetSpeed();
  69.  
  70. if (Mathf.Abs(Input.x) > 0.1f && Steering == true)
  71. {
  72. transform.localRotation = Quaternion.Euler(0, getSteeringAngle(), 0);
  73. }
  74. else
  75. {
  76. transform.localRotation = Quaternion.Euler(0, Mathf.Lerp(transform.localRotation.eulerAngles.y, 0, 1), 0);
  77. }
  78. if (Mathf.Abs(Input.y) > 0.1f)
  79. {
  80.  
  81. float NormalizedSpeed = Mathf.Clamp01(Mathf.Abs(CarSpeed * carRB.mass) / (MaxSpeed / carRB.mass));
  82. float AvailableTorque = NormalizedSpeed * Input.y;
  83. carRB.AddForceAtPosition(transform.forward * (MaxSpeed / 8) * AvailableTorque * (carRB.mass), transform.position);
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92. float steeringvel = Vector3.Dot(transform.right, WheelWorldVel);
  93.  
  94. float desiredVelChange = -steeringvel * friction;
  95. float desiredAccel = desiredVelChange / Time.fixedDeltaTime;
  96. if (VisualWheel != null)
  97. {
  98. if (Mathf.Abs(desiredAccel) > 300 && onGround == true)
  99. {
  100. VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Play();
  101.  
  102. }
  103. else
  104. {
  105. VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Stop();
  106. }
  107. }
  108. //carRB.AddForceAtPosition(transform.right * (-oldvelo * 0.5f), transform.position);
  109. oldvelo = (carRB.mass * 0.025f) * desiredAccel;
  110. carRB.AddForceAtPosition(transform.right * oldvelo, transform.position);
  111.  
  112. Debug.DrawLine(transform.position, transform.position + (transform.right * desiredAccel), Color.white, 0f, false);
  113.  
  114. Velocity = ((SpringLength - hit.distance) * (Strength)) - (vel * (DampeningMultiplier));
  115. Debug.DrawLine(transform.position, transform.position + (transform.up * Velocity), Color.white, 0f, false);
  116. carRB.AddForceAtPosition(transform.up * Velocity * carRB.mass * (Time.fixedDeltaTime * 60), transform.position);
  117.  
  118. onGround = true;
  119. wheeldistance = hit.distance;
  120. }
  121. else
  122. {
  123. onGround = false;
  124. if (VisualWheel != null)
  125. {
  126. VisualWheel.Find("Smoke").GetComponent<ParticleSystem>().Stop();
  127. }
  128. }
  129. if (VisualWheel != null)
  130. {
  131. vwheelpos = VisualWheel.localPosition.y;
  132. suslerp = 0;
  133. }
  134. }
  135.  
  136. void Update()
  137. {
  138. if (onGround == false)
  139. {
  140. mult = Mathf.Max(mult - (Time.deltaTime * 3), Mathf.Max(Mathf.Abs(Input.y),0));
  141. }
  142. else
  143. {
  144. mult = 1;
  145. }
  146. visualRotationAngle -= GetSpeed() * mult * (Time.deltaTime * 120);
  147. if (VisualWheel != null)
  148. {
  149. if (Steering == true)
  150. {
  151. VisualWheel.localRotation = Quaternion.Euler(0, Mathf.Lerp(VisualWheel.localRotation.eulerAngles.y, (Input.x * 30) + startAngle, Time.deltaTime * 6), visualRotationAngle);
  152. }
  153. else
  154. {
  155. VisualWheel.localRotation = Quaternion.Euler(0, startAngle, visualRotationAngle);
  156. }
  157. suslerp = Mathf.Min(suslerp + (Time.deltaTime / Time.fixedDeltaTime), 1);
  158. VisualWheel.localPosition = new Vector3(VisualWheel.localPosition.x, Mathf.Lerp(vwheelpos, visualStartY - (wheeldistance - (WheelRadius / 2)), suslerp), VisualWheel.localPosition.z);
  159. }
  160. }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment