123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.Serialization;
- using Valve.VR.InteractionSystem;
- public class BicycleController : MonoBehaviour
- {
- #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;
- public float maxMotorTorque = 2000f;
- public float maxBreakTorque = 2000f;
- public float CurrentSteerAngle
- {
- get => currentSteerAngle;
- set => currentSteerAngle = Mathf.Clamp(value, -maxSteeringAngle, maxSteeringAngle);
- }
- public float CurrentMotorTorque
- {
- get => currentMotorTorque;
- set => currentMotorTorque = Mathf.Clamp(value, -maxMotorTorque, maxMotorTorque);
- }
- public float CurrentBrakeTorque
- {
- get => currentBrakeTorque;
- set => currentBrakeTorque = Mathf.Clamp(value, -maxBreakTorque, maxBreakTorque);
- }
- public float CurrentLeaningAngle
- {
- get => currentLeaningAngle;
- set => currentLeaningAngle = Mathf.Clamp(value, -maxLeaningAngle, maxLeaningAngle);
- }
- private WheelCollider[] allWheelColliders;
- private float initialWheelColliderY;
- private float currentSteerAngle = 0f;
- private float currentMotorTorque = 0f;
- private float currentBrakeTorque = 0f;
- private float currentLeaningAngle = 0f;
- #endregion
- // Start is called before the first frame update
- 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();
- //RotateStraight();
- }
- private void ApplyColliderForces()
- {
- //offsetCollidersFromWheel = Mathf.Max(0.05f, 0.45f - rigidBody.velocity.magnitude * 0.07f);
- //wheelConfig.AdjustToGameObjects(frontWheel.transform, rearWheel.transform, offsetCollidersFromWheel);
- ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight});
- ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight});
- }
- private void ControlSteer(IEnumerable<WheelCollider> colliders)
- {
- //float steering = maxSteeringAngle * CurrentSteerAngle * 0.2f;
- //leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
- //rightWheels.localPosition = Vector3.up * (relativeLeanAmount * CurrentSteerAngle * rigidBody.velocity.magnitude * 0.2f);
- colliders.ForEach(c => c.steerAngle = CurrentSteerAngle);
- }
- private void ControlTorque(IEnumerable<WheelCollider> colliders)
- {
- foreach (var c in colliders)
- {
- c.motorTorque = CurrentMotorTorque;
- c.brakeTorque = CurrentBrakeTorque;
- }
- }
- private void Lean()
- {
- //reset all wheels to being centered
- if (CurrentLeaningAngle == 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
- {
- //leaning left, left wheels up, right wheels down
- ApplyOffsetToTransform(wheelConfig.frontLeft.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.rearLeft.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.frontRight.transform, initialWheelColliderY);
- ApplyOffsetToTransform(wheelConfig.rearRight.transform, initialWheelColliderY);
- }
- //CurrentLeaningAngle < 0 -> leaning left, > 0 -> right
- var leaningAbs = Mathf.Abs(CurrentLeaningAngle * Mathf.Deg2Rad);
- //calculate offset for wheels; description Docu folder
- //1.57079633 rad = 90 deg
- var verticalOffset = offsetCollidersFromWheel * Mathf.Sin(leaningAbs) / Mathf.Sin(1.57079633f - leaningAbs);
- var yPlusOffset = initialWheelColliderY + verticalOffset;
- var yMinusOffset = initialWheelColliderY - verticalOffset;
- if (CurrentLeaningAngle < 0) //TODO: maybe add a threshold for leaning, e.g. < -0.05 and > 0.05
- {
- //leaning left, left wheels up, right wheels down
- ApplyOffsetToTransform(wheelConfig.frontLeft.transform, yPlusOffset);
- ApplyOffsetToTransform(wheelConfig.rearLeft.transform, yPlusOffset);
- ApplyOffsetToTransform(wheelConfig.frontRight.transform, yMinusOffset);
- ApplyOffsetToTransform(wheelConfig.rearRight.transform, yMinusOffset);
- }
- else if (CurrentLeaningAngle > 0)
- {
- //leaning right, right wheels up, left wheels down
- 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);
- }
- }
|