123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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()
- {
- }
- }
- }
|