Advertisement
Cortezz1ty

Untitled

May 31st, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections.Generic;
  4.  
  5. public class CarController : MonoBehaviour
  6. {
  7. public enum ControlMode
  8. {
  9. Keyboard,
  10. Buttons
  11. };
  12.  
  13. public enum Axel
  14. {
  15. Front,
  16. Rear
  17. }
  18.  
  19. [Serializable]
  20. public struct Wheel
  21. {
  22. public GameObject wheelModel;
  23. public WheelCollider wheelCollider;
  24. public GameObject wheelEffectObj;
  25. public ParticleSystem smokeParticle;
  26. public Axel axel;
  27. }
  28.  
  29. public ControlMode control;
  30.  
  31. public float maxAcceleration = 30.0f;
  32. public float brakeAcceleration = 50.0f;
  33.  
  34. public float turnSensitivity = 1.0f;
  35. public float maxSteerAngle = 30.0f;
  36.  
  37. public Vector3 _centerOfMass;
  38.  
  39. public List<Wheel> wheels;
  40.  
  41. float moveInput;
  42. float steerInput;
  43.  
  44. private Rigidbody carRb;
  45.  
  46. private CarLights carLights;
  47.  
  48. void Start()
  49. {
  50. carRb = GetComponent<Rigidbody>();
  51. carRb.centerOfMass = _centerOfMass;
  52.  
  53. carLights = GetComponent<CarLights>();
  54. }
  55.  
  56. void Update()
  57. {
  58. GetInputs();
  59. AnimateWheels();
  60. WheelEffects();
  61. }
  62.  
  63. void LateUpdate()
  64. {
  65. Move();
  66. Steer();
  67. Brake();
  68. }
  69.  
  70. public void MoveInput(float input)
  71. {
  72. moveInput = input;
  73. }
  74.  
  75. public void SteerInput(float input)
  76. {
  77. steerInput = input;
  78. }
  79.  
  80. void GetInputs()
  81. {
  82. if (control == ControlMode.Keyboard)
  83. {
  84. moveInput = Input.GetAxis("Vertical");
  85. steerInput = Input.GetAxis("Horizontal");
  86. }
  87. }
  88.  
  89. void Move()
  90. {
  91. foreach (var wheel in wheels)
  92. {
  93.  
  94. wheel.wheelCollider.motorTorque = moveInput * 600 * maxAcceleration * Time.deltaTime ;
  95.  
  96.  
  97.  
  98.  
  99. }
  100. }
  101.  
  102. void Steer()
  103. {
  104. foreach (var wheel in wheels)
  105. {
  106. if (wheel.axel == Axel.Front)
  107. {
  108. var _steerAngle = steerInput * turnSensitivity * maxSteerAngle;
  109. wheel.wheelCollider.steerAngle = Mathf.Lerp(wheel.wheelCollider.steerAngle, _steerAngle, 0.6f);
  110. }
  111. }
  112. }
  113.  
  114. void Brake()
  115. {
  116. if (Input.GetKey(KeyCode.Space) || moveInput == 0)
  117. {
  118. foreach (var wheel in wheels)
  119. {
  120. wheel.wheelCollider.brakeTorque = 300 * brakeAcceleration * Time.deltaTime;
  121. }
  122.  
  123. carLights.isBackLightOn = true;
  124. carLights.OperateBackLights();
  125. }
  126. else
  127. {
  128. foreach (var wheel in wheels)
  129. {
  130. wheel.wheelCollider.brakeTorque = 0 ;
  131. }
  132.  
  133. carLights.isBackLightOn = false;
  134. carLights.OperateBackLights();
  135. }
  136. }
  137.  
  138. void AnimateWheels()
  139. {
  140. foreach (var wheel in wheels)
  141. {
  142. Quaternion rot;
  143. Vector3 pos;
  144. wheel.wheelCollider.GetWorldPose(out pos, out rot);
  145. wheel.wheelModel.transform.position = pos;
  146. wheel.wheelModel.transform.rotation = rot;
  147. }
  148. }
  149.  
  150. void WheelEffects()
  151. {
  152. foreach (var wheel in wheels)
  153. {
  154. var dirtParticleMainSettings = wheel.smokeParticle.main;
  155.  
  156. if (Input.GetKey(KeyCode.Space) && wheel.axel == Axel.Rear && wheel.wheelCollider.isGrounded == true && carRb.velocity.magnitude >= 10.0f) // carRb.velocity.magnitude >= 10.0f)
  157. {
  158.  
  159. wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = true;
  160. wheel.smokeParticle.Emit(1);
  161. }
  162. else
  163. {
  164. wheel.wheelEffectObj.GetComponentInChildren<TrailRenderer>().emitting = false;
  165. }
  166. }
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement