KeyboardBikeController.cs 2.2 KB

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