GamepadBikeController.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 accelerationLoss = 0.5f;
  18. private float acceleration;
  19. private float lean;
  20. private float steer;
  21. private IBicycleController bicycleController;
  22. private void Start()
  23. {
  24. bicycleController = GetComponent<IBicycleController>();
  25. }
  26. private void Update()
  27. {
  28. if (useSteer) bicycleController.CurrentSteerAngle = steer;
  29. if (useLean) bicycleController.CurrentLeaningAngle = lean;
  30. if (useSpeed) bicycleController.CurrentSpeed += acceleration * Time.deltaTime;
  31. }
  32. [UsedImplicitly]
  33. public void OnSpeed(InputValue value)
  34. {
  35. acceleration = value.Get<float>() * speedMultiplier;
  36. if (acceleration < 0.1f && acceleration >= 0) acceleration = -accelerationLoss;
  37. }
  38. [UsedImplicitly]
  39. public void OnLean(InputValue value)
  40. {
  41. lean = value.Get<float>() * leanMultiplier;
  42. }
  43. [UsedImplicitly]
  44. public void OnSteer(InputValue value)
  45. {
  46. steer = value.Get<float>() * steerMultiplier;
  47. }
  48. }
  49. }