SwitchColliderOnStanding.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. public bool rbToKinematicWhenStanding = true;
  13. private float lastFrameSpeed = -1f;
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. var thisFrameSpeed = bikeController.CurrentSpeed;
  18. if (lastFrameSpeed >= 0f
  19. && (lastFrameSpeed <= speedThreshold && thisFrameSpeed <= speedThreshold
  20. ||
  21. lastFrameSpeed > speedThreshold && thisFrameSpeed > speedThreshold))
  22. {
  23. //no change
  24. return;
  25. }
  26. if (thisFrameSpeed <= speedThreshold)
  27. {
  28. if (rbToKinematicWhenStanding) bikeController.rigidBody.isKinematic = true;
  29. whenStanding.enabled = true;
  30. whenDriving.ForEach(c => c.enabled = false);
  31. }
  32. else
  33. {
  34. if (rbToKinematicWhenStanding) bikeController.rigidBody.isKinematic = false;
  35. whenStanding.enabled = false;
  36. whenDriving.ForEach(c => c.enabled = true);
  37. }
  38. }
  39. }
  40. }