Guest User

Untitled

a guest
Mar 21st, 2019
112
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class CarEngine : MonoBehaviour
  6. {
  7.  
  8. public Transform path;
  9. public float maxSteerAngle = 45f;
  10. public float turnSpeed = 5f;
  11. public WheelCollider wheelFL;
  12. public WheelCollider wheelFR;
  13. public WheelCollider wheelRL;
  14. public WheelCollider wheelRR;
  15. public float maxMotorTorque = 80f;
  16. public float maxBrakeTorque = 150f;
  17. public static float currentSpeed;
  18. public float maxSpeed = 100f;
  19. public Vector3 centerOfMass;
  20. public bool isBraking = false;
  21. public Texture2D textureNormal;
  22. public Texture2D textureBraking;
  23. public Renderer carRenderer;
  24. public CarEngine thisScript;
  25.  
  26. public int counter = 0;
  27. private List<Transform> nodes;
  28. private int currectNode = 0;
  29. private float targetSteerAngle = 0;
  30.  
  31. private void Start()
  32. {
  33. counter = 0;
  34. GetComponent<Rigidbody>().centerOfMass = centerOfMass;
  35. thisScript = GetComponent<CarEngine>();
  36. Transform[] pathTransforms = path.GetComponentsInChildren<Transform>();
  37. nodes = new List<Transform>();
  38.  
  39. for (int i = 0; i < pathTransforms.Length; i++)
  40. {
  41. if (pathTransforms[i] != path.transform)
  42. {
  43. nodes.Add(pathTransforms[i]);
  44. }
  45. }
  46. }
  47.  
  48. private void FixedUpdate()
  49. {
  50. if (currectNode == 1 && counter > 3)
  51. {
  52. isBraking = true;
  53. Braking();
  54. wheelRL.brakeTorque = 100000;
  55. wheelRR.brakeTorque = 100000;
  56. wheelFL.brakeTorque = 100000;
  57. wheelFR.brakeTorque = 100000;
  58.  
  59. print("stop");
  60. GetComponent<CarEngine>().enabled = false;
  61.  
  62.  
  63. // disable scritp
  64. }
  65. else
  66. {
  67. ApplySteer();
  68. Drive();
  69. CheckWaypointDistance();
  70. Braking();
  71. LerpToSteerAngle();
  72. }
  73. }
  74.  
  75.  
  76. private void ApplySteer()
  77. {
  78. Vector3 relativeVector = transform.InverseTransformPoint(nodes[currectNode].position);
  79. float newSteer = (relativeVector.x / relativeVector.magnitude) * maxSteerAngle;
  80. targetSteerAngle = newSteer;
  81. }
  82.  
  83. private void Drive()
  84. {
  85. currentSpeed = 2 * Mathf.PI * wheelFL.radius * wheelFL.rpm * 60 / 1000;
  86.  
  87. if (currentSpeed < maxSpeed && !isBraking)
  88. {
  89. wheelFL.motorTorque = maxMotorTorque;
  90. wheelFR.motorTorque = maxMotorTorque;
  91. }
  92. else
  93. {
  94. wheelFL.motorTorque = 0;
  95. wheelFR.motorTorque = 0;
  96. }
  97. }
  98.  
  99. private void CheckWaypointDistance()
  100. {
  101. if (Vector3.Distance(transform.position, nodes[currectNode].position) < 6f)
  102. {
  103. if (currectNode == nodes.Count - 1)
  104. {
  105. currectNode = 0;
  106. counter++;
  107.  
  108. }
  109. else
  110. {
  111. counter++;
  112. currectNode++;
  113. }
  114. }
  115.  
  116. }
  117.  
  118. private void Braking()
  119. {
  120. if (isBraking)
  121. {
  122. carRenderer.material.mainTexture = textureBraking;
  123. wheelRL.brakeTorque = maxBrakeTorque;
  124. wheelRR.brakeTorque = maxBrakeTorque;
  125. }
  126. else
  127. {
  128. carRenderer.material.mainTexture = textureNormal;
  129. wheelRL.brakeTorque = 0;
  130. wheelRR.brakeTorque = 0;
  131. }
  132. }
  133. private void LerpToSteerAngle()
  134. {
  135. wheelFL.steerAngle = Mathf.Lerp(wheelFL.steerAngle, targetSteerAngle, Time.deltaTime * turnSpeed);
  136. wheelFR.steerAngle = Mathf.Lerp(wheelFR.steerAngle, targetSteerAngle, Time.deltaTime * turnSpeed);
  137. }
  138. }
RAW Paste Data