BicycleController - Copy.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using Valve.VR.InteractionSystem;
  7. public enum Controller
  8. {
  9. Sensors,
  10. SpeedSensorOnly,
  11. Keyboard,
  12. KeyboardSpeedPolarSteer
  13. }
  14. public class BicycleController : MonoBehaviour
  15. {
  16. [Header("GameObjects")] public GameObject rearWheel;
  17. public GameObject frontWheel;
  18. public GameObject crank;
  19. public GameObject bike;
  20. //public GameObject pedalL;
  21. //public GameObject pedalR;
  22. public GameObject fork;
  23. public Transform centerOfMass;
  24. [Header("Configuration")] public Controller controller = Controller.Sensors;
  25. [Header("Values")] public float oneRotationSpeed = 2.7f;
  26. public float crankMultiplier = 2f;
  27. public List<AxleInfo> axleInfos;
  28. public float maxMotorTorque = 1000;
  29. public float maxSteeringAngle = 5f;
  30. [Range(0, 1)] public float relativeLeanAmount = 0.01f;
  31. public Transform leftWheels;
  32. public Transform rightWheels;
  33. private float currentSteerAngle = 0f;
  34. private float desiredSpeed = 0f;
  35. private float currentLeaningAngle = 0f;
  36. public float rotSpeed = 10;
  37. private Vector3[] wheelPositions;
  38. public Rigidbody rb;
  39. private Quaternion startForkRot;
  40. private Vector3 upDirection = Vector3.up;
  41. private float calculatedWheelSpeed;
  42. private float currentMotorTorque;
  43. private readonly float maxSpeed = 11.111f;
  44. // Start is called before the first frame update
  45. void Start()
  46. {
  47. //rb = GetComponent<Rigidbody>();
  48. rb.centerOfMass = centerOfMass.localPosition;
  49. //startForkRot = fork.transform.localRotation;
  50. wheelPositions = new Vector3[axleInfos.Count];
  51. for (int i = 0; i < axleInfos.Count; i++)
  52. {
  53. wheelPositions[i] = axleInfos[i].wheel.center;
  54. }
  55. }
  56. private void OnGUI()
  57. {
  58. GUI.TextField(new Rect(114, 10, 280, 20),
  59. $"Wanted speed {(desiredSpeed * 3.6):n2} km/h; Current Speed {(rb.velocity.magnitude * 3.6):n2}");
  60. }
  61. // Update is called once per frame
  62. void Update()
  63. {
  64. if (controller == Controller.Keyboard || controller == Controller.KeyboardSpeedPolarSteer)
  65. {
  66. desiredSpeed = Input.GetAxis("Vertical") * 4.3333f;
  67. }
  68. if (controller == Controller.Keyboard)
  69. {
  70. currentSteerAngle = Input.GetAxis("Horizontal") * maxSteeringAngle;
  71. }
  72. RotateMeshes();
  73. //RotateFork();
  74. Debug.Log("rotation: " + currentSteerAngle);
  75. }
  76. public void FixedUpdate()
  77. {
  78. ApplyWheelForce();
  79. Lean();
  80. //RotateStraight();
  81. }
  82. void RotateMeshes()
  83. {
  84. //RotateObject(crank, 1);
  85. //RotateObject(pedalL, -1);
  86. //RotateObject(pedalR, -1);
  87. RotateObject(rearWheel, crankMultiplier);
  88. RotateObject(frontWheel, crankMultiplier);
  89. }
  90. void RotateFork()
  91. {
  92. fork.transform.localRotation = startForkRot;
  93. fork.transform.RotateAround(fork.transform.position, fork.transform.up, maxSteeringAngle * currentSteerAngle);
  94. }
  95. void Lean()
  96. {
  97. upDirection = Vector3.Normalize(Vector3.up + transform.right *
  98. (maxSteeringAngle * relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude) / 100);
  99. }
  100. void ApplyWheelForce()
  101. {
  102. ControlSteer(axleInfos.Where(a => a.steering).Select(a => a.wheel));
  103. ControlTorque(axleInfos.Where(a => a.motor).Select(a => a.wheel));
  104. }
  105. private void ControlSteer(IEnumerable<WheelCollider> colliders)
  106. {
  107. float steering = maxSteeringAngle * currentSteerAngle * 0.2f;
  108. leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude * 0.2f);
  109. rightWheels.localPosition = Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude * 0.2f);
  110. colliders.ForEach(c => c.steerAngle = steering);
  111. }
  112. private void ControlTorque(IEnumerable<WheelCollider> colliders)
  113. {
  114. var currentSpeed = rb.velocity.magnitude;
  115. var speedDif = desiredSpeed - currentSpeed;
  116. var ratio = speedDif / maxSpeed;
  117. var torque = maxMotorTorque * ratio;
  118. if (speedDif >= .1f) // 0.36 km/h
  119. {
  120. Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
  121. colliders.ForEach(c =>
  122. {
  123. c.brakeTorque = 0;
  124. c.motorTorque = torque;
  125. });
  126. }
  127. else if (speedDif <= -.1f)
  128. {
  129. Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
  130. colliders.ForEach(c =>
  131. {
  132. c.motorTorque = 0;
  133. c.brakeTorque = -torque;
  134. });
  135. }
  136. }
  137. public void SetSpeed(float speed)
  138. {
  139. if (controller == Controller.Keyboard || controller == Controller.KeyboardSpeedPolarSteer) return;
  140. desiredSpeed = speed;
  141. }
  142. public void SetLeaningAngle(float angle)
  143. {
  144. if (controller == Controller.Keyboard) return;t
  145. currentLeaningAngle = angle;
  146. }
  147. public void SetSteeringAngle(float angle)
  148. {
  149. if (controller == Controller.Keyboard || controller == Controller.SpeedSensorOnly) return;
  150. currentSteerAngle = angle;
  151. }
  152. //rotates the meshes
  153. void RotateObject(GameObject obj, float multiplier)
  154. {
  155. obj.transform.Rotate(Time.deltaTime * rb.velocity.magnitude * (360f / oneRotationSpeed) * multiplier, 0, 0);
  156. //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
  157. }
  158. [System.Serializable]
  159. public class AxleInfo
  160. {
  161. public WheelCollider wheel;
  162. public bool motor; // is this wheel attached to motor?
  163. public bool steering; // does this wheel apply steer angle?
  164. }
  165. }*/