Advertisement
Guest User

Untitled

a guest
Sep 16th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. using UnityEngine;
  2. using TMPro;
  3.  
  4. public enum DrivetrainType
  5. {
  6. FWD,
  7. RWD,
  8. }
  9.  
  10. public class CarMovement : MonoBehaviour
  11. {
  12. [SerializeField] private UIController _UIController;
  13. [SerializeField] DrivetrainType drivetrainType;
  14. private Rigidbody rb;
  15.  
  16. [SerializeField] WheelCollider wheelFL;
  17. [SerializeField] WheelCollider wheelFR;
  18. [SerializeField] WheelCollider wheelRL;
  19. [SerializeField] WheelCollider wheelRR;
  20.  
  21. private float rbVelocityLimit = 15f;
  22. [Range(300f, 900f)]
  23. [SerializeField] private float accelartionStraight;
  24.  
  25. private float currentYRotation;
  26. private float targetYRotation;
  27.  
  28. public bool IsActive;
  29.  
  30. [SerializeField] private AnimationCurve curve;
  31.  
  32.  
  33.  
  34. [SerializeField] private TrailRenderer trailRendererRR;
  35. [SerializeField] private TrailRenderer trailRendererRL;
  36.  
  37. private Trail trailRR;
  38. private Trail trailRL;
  39.  
  40.  
  41.  
  42.  
  43. private float currentScore;
  44. public float CurrentScore
  45. {
  46. get
  47. {
  48. return currentScore;
  49. }
  50. set
  51. {
  52. currentScore = value;
  53. _UIController.UpdateScoreView((int)value);
  54. RaceLoop.Instance.Score = (int)value;
  55. }
  56. }
  57. [SerializeField] private float driftScoreFactor;
  58.  
  59. private float horizontalForce, verticalForce;
  60.  
  61. private void Awake()
  62. {
  63. rb = GetComponent<Rigidbody>();
  64.  
  65. trailRL = trailRendererRL.GetComponent<Trail>();
  66. trailRR = trailRendererRR.GetComponent<Trail>();
  67.  
  68. wheelFL.transform.localEulerAngles = Vector3.zero;
  69. wheelFR.transform.localEulerAngles = Vector3.zero;
  70.  
  71.  
  72. }
  73.  
  74. public void UpdateInputAxis(float horizontal, float vertical)
  75. {
  76. horizontalForce = horizontal;
  77. verticalForce = vertical;
  78. }
  79.  
  80. private void FixedUpdate()
  81. {
  82. if (!IsActive)
  83. {
  84. PerformBrake(1f);
  85. trailRendererRL.emitting = false;
  86. trailRendererRR.emitting = false;
  87. return;
  88. }
  89.  
  90. float currentVelocity = rb.linearVelocity.magnitude;
  91.  
  92. targetYRotation = horizontalForce * curve.Evaluate(currentVelocity);
  93.  
  94. RotateWheels(targetYRotation);
  95.  
  96. wheelFL.steerAngle = targetYRotation;
  97. wheelFR.steerAngle = targetYRotation;
  98.  
  99. currentYRotation = targetYRotation;
  100.  
  101. if(verticalForce > 0)
  102. {
  103. ResetBrakes();
  104.  
  105. if (drivetrainType == DrivetrainType.FWD)
  106. {
  107. wheelFL.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
  108. wheelFR.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
  109.  
  110. wheelRL.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
  111. wheelRR.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
  112. }
  113. else if (drivetrainType == DrivetrainType.RWD)
  114. {
  115. wheelFL.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
  116. wheelFR.motorTorque = verticalForce * accelartionStraight * (2.02f - currentVelocity / rbVelocityLimit);
  117.  
  118. wheelRL.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
  119. wheelRR.motorTorque = verticalForce * accelartionStraight * (2.8f - currentVelocity / rbVelocityLimit);
  120. }
  121. }
  122. else {
  123. PerformBrake(0.05f);
  124. }
  125.  
  126. float moveDirection = Vector3.Dot(transform.forward, rb.linearVelocity);
  127. if (moveDirection < -0.5f && verticalForce > 0)
  128. {
  129. PerformBrake(Mathf.Abs(verticalForce));
  130. }
  131. else if (moveDirection > 0.5f && verticalForce < 0)
  132. {
  133. PerformBrake(Mathf.Abs(verticalForce));
  134. }
  135. else if (verticalForce < 0)
  136. {
  137. PerformMoveBackwards(verticalForce);
  138. }
  139.  
  140. HandledDriftData();
  141. }
  142.  
  143. private void HandledDriftData()
  144. {
  145. float driftDirection = Vector3.Dot(transform.right, rb.linearVelocity);
  146. int driftValue = Mathf.Abs((int)driftDirection);
  147.  
  148. if (driftValue > 3 && trailRL.IsGrounded && trailRR.IsGrounded)
  149. {
  150. trailRendererRL.emitting = true;
  151. trailRendererRR.emitting = true;
  152. CurrentScore += Time.deltaTime * driftScoreFactor;
  153. }
  154. else
  155. {
  156. trailRendererRL.emitting = false;
  157. trailRendererRR.emitting = false;
  158. }
  159. }
  160.  
  161. private void ResetBrakes()
  162. {
  163. wheelFL.brakeTorque = 0f;
  164. wheelFR.brakeTorque = 0f;
  165. wheelRL.brakeTorque = 0f;
  166. wheelRR.brakeTorque = 0f;
  167. }
  168.  
  169. private void PerformBrake(float force)
  170. {
  171. wheelFL.brakeTorque = force * 150f * 0.3f * accelartionStraight;
  172. wheelFR.brakeTorque = force * 150f * 0.3f * accelartionStraight;
  173. wheelRL.brakeTorque = force * 150f * 0.7f * accelartionStraight;
  174. wheelRR.brakeTorque = force * 150f * 0.7f * accelartionStraight;
  175. }
  176.  
  177. private void PerformMoveBackwards(float force)
  178. {
  179. ResetBrakes();
  180.  
  181. wheelFL.motorTorque = force * accelartionStraight * 0.4f;
  182. wheelFR.motorTorque = force * accelartionStraight * 0.4f;
  183. wheelRL.motorTorque = force * accelartionStraight * 0.4f;
  184. wheelRR.motorTorque = force * accelartionStraight * 0.4f;
  185. }
  186.  
  187. private void RotateWheels(float targetYAngle)
  188. {
  189. wheelFL.transform.localEulerAngles = new Vector3(wheelFL.transform.localEulerAngles.x, targetYAngle, wheelFL.transform.localEulerAngles.z);
  190. wheelFR.transform.localEulerAngles = new Vector3(wheelFR.transform.localEulerAngles.x, targetYAngle, wheelFR.transform.localEulerAngles.z);
  191. }
  192.  
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement