RbBicycleController.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using UnityEngine;
  3. namespace Controller.Bicycle
  4. {
  5. public class RbBicycleController : MonoBehaviour, IBicycleController
  6. {
  7. #region Variables
  8. public Rigidbody rigidBody;
  9. public BicycleControllerMode controllerMode;
  10. public Transform centerOfMass;
  11. private Transform rbTransform;
  12. public BicycleControllerMode ControllerMode
  13. {
  14. get => controllerMode;
  15. set => controllerMode = value;
  16. }
  17. public float CurrentSpeed { get; set; }
  18. public float CurrentLeaningAngle { get; set; }
  19. public float CurrentSteerAngle { get; set; }
  20. public Vector3 RigidBodyVelocity => rigidBody.velocity;
  21. #endregion
  22. private void Awake()
  23. {
  24. rbTransform = rigidBody.transform;
  25. rigidBody.freezeRotation = true;
  26. rigidBody.centerOfMass = centerOfMass.position;
  27. }
  28. private void Update()
  29. {
  30. throw new NotImplementedException();
  31. }
  32. private void FixedUpdate()
  33. {
  34. ApplyVelocity();
  35. }
  36. private void ApplyVelocity()
  37. {
  38. var targetVelocity = new Vector3(CalculateVelocityX(), 0, CurrentSpeed);
  39. targetVelocity = rbTransform.TransformDirection(targetVelocity);
  40. var velocityChange = targetVelocity - rigidBody.velocity;
  41. velocityChange.y = 0;
  42. rigidBody.AddForce(velocityChange, ForceMode.VelocityChange);
  43. }
  44. private float CalculateVelocityX()
  45. {
  46. var previousRotation = rbTransform.localRotation.eulerAngles;
  47. rbTransform.localRotation = Quaternion.Euler(previousRotation + new Vector3(0,CurrentSteerAngle,0) * Time.fixedDeltaTime);
  48. return 0;
  49. //return CurrentSteerAngle/4; //TODO: something a bit smarter
  50. }
  51. private void ApplyLeaningAngle()
  52. {
  53. }
  54. }
  55. }