KeyboardBikeController.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. namespace Controller
  3. {
  4. public class KeyboardBikeController : MonoBehaviour
  5. {
  6. private BicycleController bicycleController;
  7. public bool steer = true;
  8. public bool lean = true;
  9. public bool accelerate = true;
  10. public float torqueIncreasePerSecond = 10f;
  11. public float brakeTorqueIncreasePerSecond = 20f;
  12. public float leaningAngleIncreasePerSecond = 2f;
  13. public float steeringAngleIncreasePerSecond = 2.5f;
  14. public float maxMotorTorque = 400f;
  15. public float maxBrakeTorque = 600f;
  16. public float maxLeaningAngle = 35f;
  17. public float maxSteeringAngle = 70f;
  18. private void Start()
  19. {
  20. bicycleController = GetComponent<BicycleController>();
  21. }
  22. private void Update()
  23. {
  24. if (accelerate)
  25. {
  26. if (Input.GetKey(KeyCode.T))
  27. {
  28. bicycleController.CurrentBrakeTorque = 0f;
  29. bicycleController.CurrentMotorTorque += torqueIncreasePerSecond * Time.deltaTime;
  30. }
  31. else if (Input.GetKeyUp(KeyCode.T))
  32. {
  33. bicycleController.CurrentMotorTorque = 0f;
  34. }
  35. if (Input.GetKey(KeyCode.G))
  36. {
  37. bicycleController.CurrentMotorTorque = 0f;
  38. bicycleController.CurrentBrakeTorque += brakeTorqueIncreasePerSecond * Time.deltaTime;
  39. }
  40. else if (Input.GetKeyUp(KeyCode.G))
  41. {
  42. bicycleController.CurrentBrakeTorque = 0f;
  43. }
  44. }
  45. if (steer)
  46. {
  47. if (Input.GetKey(KeyCode.F))
  48. {
  49. bicycleController.CurrentSteerAngle -= steeringAngleIncreasePerSecond * Time.deltaTime;
  50. }
  51. if (Input.GetKey(KeyCode.H))
  52. {
  53. bicycleController.CurrentSteerAngle += steeringAngleIncreasePerSecond * Time.deltaTime;
  54. }
  55. if (Input.GetKeyUp(KeyCode.F) || Input.GetKeyUp(KeyCode.H))
  56. {
  57. bicycleController.CurrentSteerAngle = 0f;
  58. }
  59. }
  60. if (lean)
  61. {
  62. if (Input.GetKey(KeyCode.R))
  63. {
  64. bicycleController.CurrentLeaningAngle -= leaningAngleIncreasePerSecond * Time.deltaTime;
  65. }
  66. if (Input.GetKey(KeyCode.Z))
  67. {
  68. bicycleController.CurrentLeaningAngle += leaningAngleIncreasePerSecond * Time.deltaTime;
  69. }
  70. if (Input.GetKeyUp(KeyCode.R) || Input.GetKeyUp(KeyCode.Z))
  71. {
  72. bicycleController.CurrentLeaningAngle = 0f;
  73. }
  74. }
  75. }
  76. }
  77. }