BuggyBuddy.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. namespace Valve.VR.InteractionSystem.Sample
  5. {
  6. public class BuggyBuddy : MonoBehaviour
  7. {
  8. public Transform turret;
  9. float turretRot;
  10. [Tooltip("Maximum steering angle of the wheels")]
  11. public float maxAngle = 30f;
  12. [Tooltip("Maximum Turning torque")]
  13. public float maxTurnTorque = 30f;
  14. [Tooltip("Maximum torque applied to the driving wheels")]
  15. public float maxTorque = 300f;
  16. [Tooltip("Maximum brake torque applied to the driving wheels")]
  17. public float brakeTorque = 30000f;
  18. [Tooltip("If you need the visual wheels to be attached automatically, drag the wheel shape here.")]
  19. public GameObject[] wheelRenders;
  20. [Tooltip("The vehicle's speed when the physics engine can use different amount of sub-steps (in m/s).")]
  21. public float criticalSpeed = 5f;
  22. [Tooltip("Simulation sub-steps when the speed is above critical.")]
  23. public int stepsBelow = 5;
  24. [Tooltip("Simulation sub-steps when the speed is below critical.")]
  25. public int stepsAbove = 1;
  26. private WheelCollider[] m_Wheels;
  27. public AudioSource au_motor;
  28. [HideInInspector]
  29. public float mvol;
  30. public AudioSource au_skid;
  31. float svol;
  32. public WheelDust skidsample;
  33. float skidSpeed = 3;
  34. public Vector3 localGravity;
  35. [HideInInspector]
  36. public Rigidbody body;
  37. public float rapidfireTime = 0;
  38. private float shootTimer;
  39. [HideInInspector]
  40. public Vector2 steer;
  41. [HideInInspector]
  42. public float throttle;
  43. [HideInInspector]
  44. public float handBrake;
  45. [HideInInspector]
  46. public Transform controllerReference;
  47. [HideInInspector]
  48. public float speed;
  49. public Transform centerOfMass;
  50. private void Start()
  51. {
  52. body = GetComponent<Rigidbody>();
  53. m_Wheels = GetComponentsInChildren<WheelCollider>();
  54. body.centerOfMass = body.transform.InverseTransformPoint(centerOfMass.position) * body.transform.lossyScale.x;
  55. }
  56. /*
  57. private void TurretInput()
  58. {
  59. Vector2 tIn = TurretControl.joystick();
  60. Vector3 tur = new Vector3(tIn.x, 0, tIn.y);
  61. tur = TurretControl.transform.TransformDirection(tur);
  62. tur = transform.InverseTransformDirection(tur);
  63. tur = Vector3.ProjectOnPlane(tur, Vector3.up);
  64. turretRot = VectorMath.FindAngle(Vector3.forward, tur, Vector3.up) * Mathf.Rad2Deg;
  65. Vector3 turup = Vector3.forward;
  66. turret.localRotation = Quaternion.Euler(turup * turretRot);
  67. if (rapidfireTime == 0)
  68. {
  69. if (TurretControl.GetPressDown(KnucklesButton.Trigger))
  70. {
  71. Fire();
  72. }
  73. }else
  74. {
  75. if (shootTimer > rapidfireTime&& TurretControl.GetPress(KnucklesButton.Trigger))
  76. {
  77. Fire();
  78. shootTimer = 0;
  79. }
  80. shootTimer += Time.deltaTime;
  81. }
  82. }
  83. */
  84. private void Update()
  85. {
  86. m_Wheels[0].ConfigureVehicleSubsteps(criticalSpeed, stepsBelow, stepsAbove);
  87. //TurretInput();
  88. //keyboard input for testing
  89. //Vector3 move = Vector3.forward * Input.GetAxis("Vertical") + Vector3.right * Input.GetAxis("Horizontal");
  90. //driving input
  91. //float forward = maxTorque * move.magnitude;
  92. float forward = maxTorque * throttle;
  93. if (steer.y < -0.5f)
  94. forward *= -1;
  95. float angle = maxAngle * steer.x;
  96. speed = transform.InverseTransformVector(body.velocity).z;
  97. float forw = Mathf.Abs(speed);
  98. angle /= 1 + forw / 20;
  99. // if (Mathf.Abs(move.z) < 0.1f && Mathf.Abs(move.x) > 0.5)
  100. // forward *= 3;
  101. //float forward = maxTorque * throttle; not fun lawrence steering
  102. float fVol = Mathf.Abs(forward);
  103. mvol = Mathf.Lerp(mvol, Mathf.Pow((fVol / maxTorque), 0.8f) * Mathf.Lerp(0.4f, 1.0f, (Mathf.Abs(m_Wheels[2].rpm) / 200)) * Mathf.Lerp(1.0f, 0.5f, handBrake), Time.deltaTime * 9);
  104. au_motor.volume = Mathf.Clamp01(mvol);
  105. float motorPitch = Mathf.Lerp(0.8f, 1.0f, mvol);
  106. au_motor.pitch = Mathf.Clamp01(motorPitch);
  107. svol = Mathf.Lerp(svol, skidsample.amt / skidSpeed, Time.deltaTime * 9);
  108. au_skid.volume = Mathf.Clamp01(svol);
  109. float skidPitch = Mathf.Lerp(0.9f, 1.0f, svol);
  110. au_skid.pitch = Mathf.Clamp01(skidPitch);
  111. //float forward = maxTorque * Input.GetAxis("Vertical");
  112. //bool stopped = Mathf.Abs(transform.InverseTransformDirection(GetComponent<Rigidbody>().velocity).z) < 1.0f;
  113. for (int i = 0; i < wheelRenders.Length; i++)
  114. {
  115. WheelCollider wheel = m_Wheels[i];
  116. if (wheel.transform.localPosition.z > 0)
  117. {
  118. // front wheels
  119. wheel.steerAngle = angle;
  120. //4wd?
  121. wheel.motorTorque = forward;
  122. }
  123. if (wheel.transform.localPosition.z < 0) // back wheels
  124. {
  125. }
  126. // wheel.brakeTorque = Mathf.Lerp(Mathf.Abs(forward) < 0.1f ? 1 : 0, brakeTorque, handBrake);
  127. wheel.motorTorque = forward;
  128. if (wheel.transform.localPosition.x < 0) // left wheels
  129. {
  130. }
  131. if (wheel.transform.localPosition.x >= 0) // right wheels
  132. {
  133. }
  134. // Update visual wheels if they exist, and the colliders are enabled
  135. if (wheelRenders[i] != null && m_Wheels[0].enabled)
  136. {
  137. Quaternion q;
  138. Vector3 p;
  139. wheel.GetWorldPose(out p, out q);
  140. Transform shapeTransform = wheelRenders[i].transform;
  141. shapeTransform.position = p;
  142. shapeTransform.rotation = q;
  143. }
  144. }
  145. steer = Vector2.Lerp(steer, Vector2.zero, Time.deltaTime * 4);
  146. }
  147. private void FixedUpdate()
  148. {
  149. body.AddForce(localGravity, ForceMode.Acceleration);
  150. }
  151. public static float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)
  152. {
  153. // If the vector the angle is being calculated to is 0...
  154. if (toVector == Vector3.zero)
  155. // ... the angle between them is 0.
  156. return 0f;
  157. // Create a float to store the angle between the facing of the enemy and the direction it's travelling.
  158. float angle = Vector3.Angle(fromVector, toVector);
  159. // Find the cross product of the two vectors (this will point up if the velocity is to the right of forward).
  160. Vector3 normal = Vector3.Cross(fromVector, toVector);
  161. // The dot product of the normal with the upVector will be positive if they point in the same direction.
  162. angle *= Mathf.Sign(Vector3.Dot(normal, upVector));
  163. // We need to convert the angle we've found from degrees to radians.
  164. angle *= Mathf.Deg2Rad;
  165. return angle;
  166. }
  167. }
  168. }