using System; using UnityEngine; namespace Controller.Bicycle { public class RbBicycleController : BicycleControllerBaseBehaviour, IBicycleController { #region Variables private Transform rbTransform; private float currentSteerAngle; private float currentLeaningAngle; private float currentSpeed; public BicycleControllerMode ControllerMode { get => controllerMode; set => controllerMode = value; } public float CurrentSpeed { get => currentSpeed; set => currentSpeed = Mathf.Clamp(value, 0, maxSpeed); } public float CurrentSteerAngle { get => currentSteerAngle; set { //don't lean while standing / walking to bike if (rigidBody.velocity.magnitude < .5f) return; currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle); } } public float CurrentLeaningAngle { get => currentLeaningAngle; set => currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle); } 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() { } } }