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 { [Header("Visible Game Objects")] public GameObject rearWheel; public GameObject frontWheel; public GameObject crank; public GameObject pedalL; public GameObject pedalR; public GameObject fork; public GameObject bike; [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 oneRotationSpeed = 2.7f; public float crankMultiplier = 2f; public float maxMotorTorque = 1000; public float maxSteeringAngle = 5f; [Range(0, 1)] public float relativeLeanAmount = 0.01f; private WheelCollider[] allWheelColliders; 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 Quaternion startForkRot; private Vector3 upDirection = Vector3.up; private float calculatedWheelSpeed; private float initialWheelColliderY; private void OnGUI() { GUI.TextField(new Rect(300, 10, 700, 40), $"Motor Torque = {CurrentMotorTorque}; Brake Torque = {CurrentBrakeTorque}; Leaning Angle = {CurrentLeaningAngle}; Steering Angle = {CurrentSteerAngle}"); } // 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; //startForkRot = fork.transform.localRotation; wheelPositions = new Vector3[wheelColliders.Count]; } void Update() { RotateMeshes(); //RotateFork(); Debug.Log("rotation: " + CurrentSteerAngle); } 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); } private void RotateMeshes() { //RotateObject(crank, 1); //RotateObject(pedalL, -1); //RotateObject(pedalR, -1); RotateObject(rearWheel, crankMultiplier); RotateObject(frontWheel, crankMultiplier); } private void RotateFork() { fork.transform.localRotation = startForkRot; fork.transform.RotateAround(fork.transform.position, fork.transform.up, maxSteeringAngle * CurrentSteerAngle); } //rotates the meshes void RotateObject(GameObject obj, float multiplier) { obj.transform.Rotate(Time.deltaTime * rigidBody.velocity.magnitude * (360f / oneRotationSpeed) * multiplier, 0, 0); //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0); } }