GamepadBikeController.cs 1.7 KB

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