GamepadBikeController.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using JetBrains.Annotations;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. namespace Controller
  5. {
  6. [RequireComponent(typeof(PlayerInput))]
  7. [RequireComponent(typeof(BicycleController))]
  8. public class GamepadBikeController : MonoBehaviour
  9. {
  10. public bool useSpeed;
  11. public bool useSteer;
  12. public bool useLean;
  13. public float speedMultiplier = 200f;
  14. public float leanMultiplier = 20f;
  15. public float steerMultiplier = 15f;
  16. private float speed;
  17. private float lean;
  18. private float steer;
  19. private BicycleController bicycleController;
  20. private void Start()
  21. {
  22. bicycleController = GetComponent<BicycleController>();
  23. }
  24. private void Update()
  25. {
  26. if(useSteer) bicycleController.CurrentSteerAngle = steer;
  27. if(useLean) bicycleController.CurrentLeaningAngle = lean;
  28. if (useSpeed)
  29. {
  30. if (speed < 0)
  31. {
  32. bicycleController.CurrentMotorTorque = 0;
  33. bicycleController.CurrentBrakeTorque = -speed;
  34. }
  35. else
  36. {
  37. bicycleController.CurrentBrakeTorque = 0;
  38. bicycleController.CurrentMotorTorque = speed;
  39. }
  40. }
  41. }
  42. [UsedImplicitly]
  43. public void OnSpeed(InputValue value)
  44. {
  45. speed = value.Get<float>() * speedMultiplier;
  46. }
  47. [UsedImplicitly]
  48. public void OnLean(InputValue value)
  49. {
  50. lean = value.Get<float>() * leanMultiplier;
  51. }
  52. [UsedImplicitly]
  53. public void OnSteer(InputValue value)
  54. {
  55. steer = value.Get<float>() * steerMultiplier;
  56. }
  57. }
  58. }