SwitchColliderOnStanding.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using Controller.Bicycle;
  2. using UnityEngine;
  3. using Valve.VR.InteractionSystem;
  4. namespace Wheels
  5. {
  6. public class SwitchColliderOnStanding : MonoBehaviour
  7. {
  8. public RbBicycleController bikeController;
  9. public Collider whenStanding;
  10. public Collider[] whenDriving;
  11. [Range(0f, 5f)] public float speedThreshold = 0.5f;
  12. private float lastFrameSpeed = -1f;
  13. // Update is called once per frame
  14. void Update()
  15. {
  16. var thisFrameSpeed = bikeController.CurrentSpeed;
  17. if (lastFrameSpeed >= 0f
  18. && (lastFrameSpeed <= speedThreshold && thisFrameSpeed <= speedThreshold
  19. ||
  20. lastFrameSpeed > speedThreshold && thisFrameSpeed > speedThreshold))
  21. {
  22. //no change
  23. return;
  24. }
  25. if (thisFrameSpeed <= speedThreshold)
  26. {
  27. whenStanding.enabled = true;
  28. whenDriving.ForEach(c => c.enabled = false);
  29. }
  30. else
  31. {
  32. whenStanding.enabled = false;
  33. whenDriving.ForEach(c => c.enabled = true);
  34. }
  35. }
  36. }
  37. }