KeyboardBikeController.cs 2.7 KB

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