SwitchColliderOnStanding.cs 1.4 KB

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