using Controller.Bicycle; using UnityEngine; namespace Controller { [RequireComponent(typeof(IBicycleController))] public class KeyboardBikeController : MonoBehaviour { public bool steer = true; public bool lean = true; public bool accelerate = true; public bool breaking = true; public float speedIncreasePerSecond = 3f; public float speedDecreasePerSecond = 0.5f; public float brakeIncreasePerSecond = 5f; public float leaningAngleIncreasePerSecond = 2f; public float steeringAngleIncreasePerSecond = 2.5f; private IBicycleController bicycleController; private float sensedSpeed = 0f; private void Start() { bicycleController = GetComponent(); } private void Update() { if (accelerate) { if (Input.GetKey(KeyCode.W)) { //sensedSpeed += speedIncreasePerSecond * Time.deltaTime; //bicycleController.CurrentSpeed = 25 / 3.6f; bicycleController.CurrentSpeed += speedIncreasePerSecond * Time.deltaTime; } else if (bicycleController.CurrentSpeed > 0) { // get slower over time, when not accelerating //sensedSpeed = Mathf.Max(0, // sensedSpeed - speedDecreasePerSecond * Time.deltaTime); } } if (breaking) { if (Input.GetKey(KeyCode.S)) { //sensedSpeed = Mathf.Max(0,sensedSpeed - brakeIncreasePerSecond * Time.deltaTime); //bicycleController.CurrentSpeed = 0f; bicycleController.CurrentBreakForce += brakeIncreasePerSecond * Time.deltaTime; } else { bicycleController.CurrentBreakForce = 0; } } if (steer) { if (Input.GetKey(KeyCode.A)) bicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime; if (Input.GetKey(KeyCode.D)) bicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime; if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.D)) bicycleController.CurrentSteerAngle = 0f; } if (lean) { if (Input.GetKey(KeyCode.R)) bicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime; if (Input.GetKey(KeyCode.Z)) bicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime; if (Input.GetKeyUp(KeyCode.R) || Input.GetKeyUp(KeyCode.Z)) bicycleController.CurrentLeaningAngle = 0f; } } } }