KeyboardBikeController.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using UnityEngine;
  3. public class KeyboardBikeController : MonoBehaviour
  4. {
  5. private BicycleController bicycleController;
  6. public bool steer = true;
  7. public bool lean = true;
  8. public bool accelerate = true;
  9. public float torqueIncreasePerSecond = 10f;
  10. public float brakeTorqueIncreasePerSecond = 20f;
  11. public float leaningAngleIncreasePerSecond = 2f;
  12. public float steeringAngleIncreasePerSecond = 2.5f;
  13. public float maxMotorTorque = 400f;
  14. public float maxBrakeTorque = 600f;
  15. public float maxLeaningAngle = 35f;
  16. public float maxSteeringAngle = 70f;
  17. private void Start()
  18. {
  19. bicycleController = GetComponent<BicycleController>();
  20. }
  21. private void Update()
  22. {
  23. if (accelerate)
  24. {
  25. if (Input.GetKey(KeyCode.T))
  26. {
  27. bicycleController.CurrentBrakeTorque = 0f;
  28. bicycleController.CurrentMotorTorque += torqueIncreasePerSecond * Time.deltaTime;
  29. }
  30. else if (Input.GetKeyUp(KeyCode.T))
  31. {
  32. bicycleController.CurrentMotorTorque = 0f;
  33. }
  34. if (Input.GetKey(KeyCode.G))
  35. {
  36. bicycleController.CurrentMotorTorque = 0f;
  37. bicycleController.CurrentBrakeTorque += brakeTorqueIncreasePerSecond * Time.deltaTime;
  38. }
  39. else if (Input.GetKeyUp(KeyCode.G))
  40. {
  41. bicycleController.CurrentBrakeTorque = 0f;
  42. }
  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. }