KeyboardBikeController.cs 2.7 KB

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