BicycleController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BicycleController : MonoBehaviour
  5. {
  6. [Header ("GameObjects")]
  7. //public GameObject rearWheel;
  8. public GameObject frontWheel;
  9. //public GameObject crank;
  10. //public GameObject pedalL;
  11. //public GameObject pedalR;
  12. //public GameObject fork;
  13. public Transform centerOfMass;
  14. [Header("Values")]
  15. public float oneRotationSpeed = 2.7f;
  16. public float crankMultiplier = 2f;
  17. public List<AxleInfo> axleInfos; // the information about each individual axle
  18. public float maxMotorTorque = 12f; // maximum torque the motor can apply to wheel
  19. public float maxSteeringAngle = 5f; // maximum steer angle the wheel can have
  20. [Range(0,1)]
  21. public float relativeLeanAmount = 0.01f;
  22. public Transform leftWheels;
  23. public Transform rightWheels;
  24. private float currentSteerAngle = 0f;
  25. private float currentSpeed = 0f;
  26. private float currentLeaningAngle = 0f;
  27. public float rotSpeed = 10;
  28. private Vector3[] wheelPositions;
  29. public Rigidbody rb;
  30. private Quaternion startForkRot;
  31. private Vector3 upDirection = Vector3.up;
  32. // Start is called before the first frame update
  33. void Start()
  34. {
  35. //rb = GetComponent<Rigidbody>();
  36. rb.centerOfMass = centerOfMass.localPosition;
  37. //startForkRot = fork.transform.localRotation;
  38. wheelPositions = new Vector3[axleInfos.Count];
  39. for (int i = 0; i < axleInfos.Count; i++)
  40. {
  41. wheelPositions[i] = axleInfos[i].wheel.center;
  42. }
  43. }
  44. // Update is called once per frame
  45. void Update()
  46. {
  47. RotateMeshes();
  48. //RotateFork();
  49. Debug.Log("rotation: " + currentSteerAngle);
  50. }
  51. public void FixedUpdate()
  52. {
  53. ApplyWheelForce();
  54. Lean();
  55. //RotateStraight();
  56. }
  57. void RotateMeshes()
  58. {
  59. //RotateObject(crank, 1);
  60. //RotateObject(pedalL, -1);
  61. //RotateObject(pedalR, -1);
  62. //RotateObject(rearWheel, crankMultiplier);
  63. RotateObject(frontWheel, crankMultiplier);
  64. }
  65. /*void RotateFork()
  66. {
  67. fork.transform.localRotation = startForkRot;
  68. fork.transform.RotateAround(fork.transform.position, fork.transform.up, maxSteeringAngle * rotationValue);
  69. }*/
  70. void Lean()
  71. {
  72. upDirection = Vector3.Normalize(Vector3.up + transform.right * (maxSteeringAngle * relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude) / 100);
  73. }
  74. void ApplyWheelForce()
  75. {
  76. float motor = maxMotorTorque;
  77. float steering = maxSteeringAngle * currentSteerAngle;
  78. leftWheels.localPosition = - Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude);
  79. rightWheels.localPosition = Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude);
  80. //rb.velocity = new Vector3(currentSpeed, rb.velocity.y, rb.velocity.z);
  81. foreach (AxleInfo axleInfo in axleInfos)
  82. {
  83. if (axleInfo.steering)
  84. {
  85. axleInfo.wheel.steerAngle = steering;
  86. }
  87. if (axleInfo.motor && rb.velocity.magnitude < currentSpeed)
  88. {
  89. axleInfo.wheel.motorTorque = motor;
  90. }
  91. else if(axleInfo.motor)
  92. {
  93. axleInfo.wheel.motorTorque = 0;
  94. }
  95. }
  96. }
  97. public void SetSpeed(float speed)
  98. {
  99. currentSpeed = speed;
  100. }
  101. public void SetLeaningAngle(float angle)
  102. {
  103. currentLeaningAngle = angle;
  104. }
  105. public void SetSteeringAngle(float angle)
  106. {
  107. currentSteerAngle = angle;
  108. }
  109. //rotates the meshes
  110. void RotateObject(GameObject obj, float multiplier)
  111. {
  112. obj.transform.Rotate(Time.deltaTime * rb.velocity.magnitude * (360f / oneRotationSpeed) * multiplier, 0, 0);
  113. //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
  114. }
  115. }
  116. [System.Serializable]
  117. public class AxleInfo
  118. {
  119. public WheelCollider wheel;
  120. public bool motor; // is this wheel attached to motor?
  121. public bool steering; // does this wheel apply steer angle?
  122. }