1
0

KeyboardBikeController.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 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. {
  38. // sensedSpeed = Mathf.Max(0,
  39. // sensedSpeed - brakeIncreasePerSecond * Time.deltaTime);
  40. bicycleController.CurrentSpeed = 0f;
  41. }
  42. //bicycleController.CurrentSpeed = sensedSpeed;
  43. }
  44. if (steer)
  45. {
  46. if (Input.GetKey(KeyCode.F))
  47. {
  48. bicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime;
  49. }
  50. if (Input.GetKey(KeyCode.H))
  51. {
  52. bicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime;
  53. }
  54. if (Input.GetKeyUp(KeyCode.F) || Input.GetKeyUp(KeyCode.H))
  55. {
  56. bicycleController.CurrentSteerAngle = 0f;
  57. }
  58. }
  59. if (lean)
  60. {
  61. if (Input.GetKey(KeyCode.R))
  62. {
  63. bicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime;
  64. }
  65. if (Input.GetKey(KeyCode.Z))
  66. {
  67. bicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime;
  68. }
  69. if (Input.GetKeyUp(KeyCode.R) || Input.GetKeyUp(KeyCode.Z))
  70. {
  71. bicycleController.CurrentLeaningAngle = 0f;
  72. }
  73. }
  74. }
  75. }
  76. }