12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System;
- using UnityEngine;
- namespace Controller.Bicycle
- {
- public class RbBicycleController : MonoBehaviour, IBicycleController
- {
- #region Variables
- public Rigidbody rigidBody;
- public BicycleControllerMode controllerMode;
- public Transform centerOfMass;
- private Transform rbTransform;
- public BicycleControllerMode ControllerMode
- {
- get => controllerMode;
- set => controllerMode = value;
- }
- public float CurrentSpeed { get; set; }
- public float CurrentLeaningAngle { get; set; }
- public float CurrentSteerAngle { get; set; }
- public Vector3 RigidBodyVelocity => rigidBody.velocity;
- #endregion
- private void Awake()
- {
- rbTransform = rigidBody.transform;
- rigidBody.freezeRotation = true;
- rigidBody.centerOfMass = centerOfMass.position;
- }
- private void Update()
- {
- throw new NotImplementedException();
- }
- private void FixedUpdate()
- {
- ApplyVelocity();
- }
- private void ApplyVelocity()
- {
- var targetVelocity = new Vector3(CalculateVelocityX(), 0, CurrentSpeed);
- targetVelocity = rbTransform.TransformDirection(targetVelocity);
- var velocityChange = targetVelocity - rigidBody.velocity;
- velocityChange.y = 0;
- rigidBody.AddForce(velocityChange, ForceMode.VelocityChange);
- }
- private float CalculateVelocityX()
- {
- var previousRotation = rbTransform.localRotation.eulerAngles;
- rbTransform.localRotation = Quaternion.Euler(previousRotation + new Vector3(0,CurrentSteerAngle,0) * Time.fixedDeltaTime);
- return 0;
- //return CurrentSteerAngle/4; //TODO: something a bit smarter
- }
- private void ApplyLeaningAngle()
- {
- }
- }
- }
|