2
0

KeyboardBikeController.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using Controller.Bicycle;
  2. using UnityEngine;
  3. namespace Controller
  4. {
  5. [RequireComponent(typeof(IBicycleController))]
  6. public class KeyboardBikeController : MonoBehaviour
  7. {
  8. public bool steer = true;
  9. public bool lean = true;
  10. public bool accelerate = true;
  11. public float speedIncreasePerSecond = 3f;
  12. public float speedDecreasePerSecond = 0.5f;
  13. public float brakeIncreasePerSecond = 5f;
  14. public float leaningAngleIncreasePerSecond = 2f;
  15. public float steeringAngleIncreasePerSecond = 2.5f;
  16. private IBicycleController bicycleController;
  17. private float sensedSpeed;
  18. private void Start()
  19. {
  20. bicycleController = GetComponent<IBicycleController>();
  21. }
  22. private void Update()
  23. {
  24. if (accelerate)
  25. {
  26. if (Input.GetKey(KeyCode.T))
  27. {
  28. //sensedSpeed += speedIncreasePerSecond * Time.deltaTime;
  29. bicycleController.CurrentSpeed = 25 / 3.6f;
  30. }
  31. else if (bicycleController.CurrentSpeed > 0)
  32. {
  33. //sensedSpeed = Mathf.Max(0,
  34. // sensedSpeed - speedDecreasePerSecond * Time.deltaTime);
  35. }
  36. if (Input.GetKey(KeyCode.G))
  37. // sensedSpeed = Mathf.Max(0,
  38. // sensedSpeed - brakeIncreasePerSecond * Time.deltaTime);
  39. bicycleController.CurrentSpeed = 0f;
  40. //bicycleController.CurrentSpeed = sensedSpeed;
  41. }
  42. if (steer)
  43. {
  44. if (Input.GetKey(KeyCode.F))
  45. bicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime;
  46. if (Input.GetKey(KeyCode.H))
  47. bicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime;
  48. if (Input.GetKeyUp(KeyCode.F) || Input.GetKeyUp(KeyCode.H)) bicycleController.CurrentSteerAngle = 0f;
  49. }
  50. if (lean)
  51. {
  52. if (Input.GetKey(KeyCode.R))
  53. bicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime;
  54. if (Input.GetKey(KeyCode.Z))
  55. bicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime;
  56. if (Input.GetKeyUp(KeyCode.R) || Input.GetKeyUp(KeyCode.Z)) bicycleController.CurrentLeaningAngle = 0f;
  57. }
  58. }
  59. }
  60. }