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 CurrentSteerAngle { get; set; } = 0f; public float CurrentMotorTorque { get; set; } = 0f; public float CurrentBrakeTorque { get; set; } = 0f; public float CurrentLeaningAngle { get; set; } = 0f; private WheelCollider[] allWheelColliders; private float initialWheelColliderY; #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() { ControlSteer(new[] {wheelConfig.frontLeft, wheelConfig.frontRight}); ControlTorque(new[] {wheelConfig.rearLeft, wheelConfig.rearRight}); } private void ControlSteer(IEnumerable 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 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); } }