123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- /*using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using Valve.VR.InteractionSystem;
- public enum Controller
- {
- Sensors,
- SpeedSensorOnly,
- Keyboard,
- KeyboardSpeedPolarSteer
- }
- public class BicycleController : MonoBehaviour
- {
- [Header("GameObjects")] public GameObject rearWheel;
- public GameObject frontWheel;
- public GameObject crank;
- public GameObject bike;
- //public GameObject pedalL;
- //public GameObject pedalR;
- public GameObject fork;
- public Transform centerOfMass;
- [Header("Configuration")] public Controller controller = Controller.Sensors;
- [Header("Values")] public float oneRotationSpeed = 2.7f;
- public float crankMultiplier = 2f;
- public List<AxleInfo> axleInfos;
- public float maxMotorTorque = 1000;
- public float maxSteeringAngle = 5f;
- [Range(0, 1)] public float relativeLeanAmount = 0.01f;
- public Transform leftWheels;
- public Transform rightWheels;
- private float currentSteerAngle = 0f;
- private float desiredSpeed = 0f;
- private float currentLeaningAngle = 0f;
- public float rotSpeed = 10;
- private Vector3[] wheelPositions;
- public Rigidbody rb;
- private Quaternion startForkRot;
- private Vector3 upDirection = Vector3.up;
- private float calculatedWheelSpeed;
- private float currentMotorTorque;
- private readonly float maxSpeed = 11.111f;
- // Start is called before the first frame update
- void Start()
- {
- //rb = GetComponent<Rigidbody>();
- rb.centerOfMass = centerOfMass.localPosition;
- //startForkRot = fork.transform.localRotation;
- wheelPositions = new Vector3[axleInfos.Count];
- for (int i = 0; i < axleInfos.Count; i++)
- {
- wheelPositions[i] = axleInfos[i].wheel.center;
- }
- }
- private void OnGUI()
- {
- GUI.TextField(new Rect(114, 10, 280, 20),
- $"Wanted speed {(desiredSpeed * 3.6):n2} km/h; Current Speed {(rb.velocity.magnitude * 3.6):n2}");
- }
- // Update is called once per frame
- void Update()
- {
- if (controller == Controller.Keyboard || controller == Controller.KeyboardSpeedPolarSteer)
- {
- desiredSpeed = Input.GetAxis("Vertical") * 4.3333f;
- }
- if (controller == Controller.Keyboard)
- {
- currentSteerAngle = Input.GetAxis("Horizontal") * maxSteeringAngle;
- }
- RotateMeshes();
- //RotateFork();
- Debug.Log("rotation: " + currentSteerAngle);
- }
- public void FixedUpdate()
- {
- ApplyWheelForce();
- Lean();
- //RotateStraight();
- }
- void RotateMeshes()
- {
- //RotateObject(crank, 1);
- //RotateObject(pedalL, -1);
- //RotateObject(pedalR, -1);
- RotateObject(rearWheel, crankMultiplier);
- RotateObject(frontWheel, crankMultiplier);
- }
- void RotateFork()
- {
- fork.transform.localRotation = startForkRot;
- fork.transform.RotateAround(fork.transform.position, fork.transform.up, maxSteeringAngle * currentSteerAngle);
- }
- void Lean()
- {
- upDirection = Vector3.Normalize(Vector3.up + transform.right *
- (maxSteeringAngle * relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude) / 100);
- }
- void ApplyWheelForce()
- {
- ControlSteer(axleInfos.Where(a => a.steering).Select(a => a.wheel));
- ControlTorque(axleInfos.Where(a => a.motor).Select(a => a.wheel));
- }
- private void ControlSteer(IEnumerable<WheelCollider> colliders)
- {
- float steering = maxSteeringAngle * currentSteerAngle * 0.2f;
- leftWheels.localPosition = -Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude * 0.2f);
- rightWheels.localPosition = Vector3.up * (relativeLeanAmount * currentSteerAngle * rb.velocity.magnitude * 0.2f);
- colliders.ForEach(c => c.steerAngle = steering);
- }
- private void ControlTorque(IEnumerable<WheelCollider> colliders)
- {
- var currentSpeed = rb.velocity.magnitude;
- var speedDif = desiredSpeed - currentSpeed;
- var ratio = speedDif / maxSpeed;
- var torque = maxMotorTorque * ratio;
- if (speedDif >= .1f) // 0.36 km/h
- {
- Debug.Log($"SpeedDif = {speedDif} -> applying Torque {torque} (Ratio: {ratio})");
- colliders.ForEach(c =>
- {
- c.brakeTorque = 0;
- c.motorTorque = torque;
- });
- }
- else if (speedDif <= -.1f)
- {
- Debug.Log($"SpeedDif = {speedDif} -> applying brake Torque {torque} (Ratio: {ratio})");
- colliders.ForEach(c =>
- {
- c.motorTorque = 0;
- c.brakeTorque = -torque;
- });
- }
- }
- public void SetSpeed(float speed)
- {
- if (controller == Controller.Keyboard || controller == Controller.KeyboardSpeedPolarSteer) return;
- desiredSpeed = speed;
- }
- public void SetLeaningAngle(float angle)
- {
- if (controller == Controller.Keyboard) return;t
- currentLeaningAngle = angle;
- }
- public void SetSteeringAngle(float angle)
- {
- if (controller == Controller.Keyboard || controller == Controller.SpeedSensorOnly) return;
- currentSteerAngle = angle;
- }
- //rotates the meshes
- void RotateObject(GameObject obj, float multiplier)
- {
- obj.transform.Rotate(Time.deltaTime * rb.velocity.magnitude * (360f / oneRotationSpeed) * multiplier, 0, 0);
- //obj.transform.Rotate(Time.deltaTime * rotSpeed * (360f / oneRotationSpeed) * multiplier, 0, 0);
- }
- [System.Serializable]
- public class AxleInfo
- {
- public WheelCollider wheel;
- public bool motor; // is this wheel attached to motor?
- public bool steering; // does this wheel apply steer angle?
- }
- }*/
|