1
0

GamepadBikeController.cs 1.8 KB

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