GamepadBikeController.cs 1.5 KB

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