PolarLeanController.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using Sensors;
  3. using Sensors.Polar;
  4. using UnityEngine;
  5. namespace Controller.Lean
  6. {
  7. [Serializable]
  8. public struct PolarRotationMapping
  9. {
  10. public float maxRight;
  11. public float center;
  12. }
  13. public class PolarLeanController: ILeanController
  14. {
  15. public PolarSensorConfig polarSensorConfig;
  16. public PolarRotationMapping polarRotationMapping;
  17. private float leanFactor;
  18. private BikeSensorData sensorData;
  19. public float LeanAngle => leanAngle;
  20. private float leanAngle = 0f;
  21. private void Start()
  22. {
  23. sensorData = BikeSensorData.Instance;
  24. sensorData.StartListening(polarSensorConfig: polarSensorConfig);
  25. leanFactor = 90f / (polarRotationMapping.maxRight - polarRotationMapping.center);
  26. }
  27. private void Update()
  28. {
  29. var polarData = sensorData.PolarData;
  30. if (polarData != null)
  31. {
  32. leanAngle = (polarData.Value.Acc.y - polarRotationMapping.center) * leanFactor;
  33. }
  34. }
  35. }
  36. }