2
0

GamepadBikeController.cs 1.6 KB

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