123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System.Collections.Generic;
- using Controller.Bicycle;
- using UnityEngine;
- using UnityEngine.Serialization;
- using Valve.VR.InteractionSystem;
- namespace Controller.Bicycle
- {
- public class WcBicycleController : MonoBehaviour, IBicycleController
- {
- #region Variables
- [Header("Visible Game Objects")] public GameObject rearWheel;
- public GameObject frontWheel;
- [Header("Game Objects for Physics")] public Transform centerOfMass;
- [FormerlySerializedAs("rb")] public Rigidbody rigidBody;
- public WheelConfig wheelConfig;
- [Header("Values")] public float offsetCollidersFromWheel = 0.25f;
- public float maxLeaningAngle = 35f;
- public float maxSteeringAngle = 80f;
- [Range(0,15)]
- [Tooltip("Speed in m/s")]
- public float maxSpeed = 11.111f;
- public float maxBreakTorque = 2000f;
- public float maxMotorTorque = 1000f;
- public BicycleControllerMode ControllerMode { get; set; }
- public float CurrentSpeed
- {
- get => currentSpeed;
- set => currentSpeed = Mathf.Clamp(value, 0, maxSpeed);
- }
- public float CurrentSteerAngle
- {
- get => currentSteerAngle;
- set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
- }
- public float CurrentLeaningAngle
- {
- get => currentLeaningAngle;
- set => currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle);
- }
- private WheelCollider[] allWheelColliders;
- private float initialWheelColliderY;
- private float currentSteerAngle = 0f;
- private float currentLeaningAngle = 0f;
- private float currentSpeed;
- #endregion
-
- void Start()
- {
- rigidBody.centerOfMass = centerOfMass.localPosition;
- allWheelColliders = wheelConfig.AllWheels;
- wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
- initialWheelColliderY = allWheelColliders[0].transform.localPosition.y;
- }
- void FixedUpdate()
- {
- ApplyColliderForces();
- Lean();
-
- }
- private void ApplyColliderForces()
- {
-
-
- ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight});
- ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight});
- }
- private void ControlSteer(IEnumerable<WheelCollider> colliders)
- {
-
-
-
- colliders.ForEach(c => c.steerAngle = CurrentSteerAngle);
- }
- private void ControlTorque(IEnumerable<WheelCollider> colliders)
- {
- var rbSpeed = rigidBody.velocity.magnitude;
- var speedDif = CurrentSpeed - rbSpeed;
- var ratio = speedDif / maxSpeed;
-
- var brakeTorque = 0f;
- var motorTorque = 0f;
-
- if (speedDif >= .1f)
- {
- var torque = maxMotorTorque * ratio;
-
- brakeTorque = 0;
- motorTorque = torque;
- }
- else if (speedDif <= -.1f)
- {
- var torque = -maxBreakTorque * ratio;
-
- motorTorque = 0;
- brakeTorque = torque;
- }
- foreach (var c in colliders)
- {
- c.motorTorque = motorTorque;
- c.brakeTorque = brakeTorque;
- }
- }
- private void Lean()
- {
-
- if (CurrentLeaningAngle == 0)
- {
-
- ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
- }
-
- var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
-
-
- var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
- var yPlusOffset = initialWheelColliderY + verticalOffset;
- var yMinusOffset = initialWheelColliderY - verticalOffset;
- if (CurrentLeaningAngle < 0)
- {
-
- ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yPlusOffset);
- ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
- ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
- ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
- }
- else if (CurrentLeaningAngle > 0)
- {
-
- ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yMinusOffset);
- ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yMinusOffset);
- ApplyOffsetToTransform(wheelConfig.frontRight.transform, yPlusOffset);
- ApplyOffsetToTransform(wheelConfig.rearRight.transform, yPlusOffset);
- }
- }
- private void ApplyOffsetToTransform(Transform t, float newY)
- {
- var oldPos = t.localPosition;
- t.localPosition = new Vector3(oldPos.x, newY, oldPos.z);
- }
- }
- }
|