ForcedCooperation.cs 656 B

123456789101112131415161718192021222324252627282930313233
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace ZenFulcrum.EmbeddedBrowser {
  4. /** Forces whoWillComply to be behind our z when Comply() is called. */
  5. public class ForcedCooperation : MonoBehaviour {
  6. public Transform whoWillComply;
  7. public float howLongWillTheyComply;
  8. public void Comply() {
  9. StartCoroutine(_Comply());
  10. }
  11. protected IEnumerator _Comply() {
  12. var t0 = Time.time;
  13. do {
  14. var pos = transform.InverseTransformPoint(whoWillComply.position);
  15. if (pos.z > 0) {
  16. pos.z = 0;
  17. whoWillComply.position = transform.TransformPoint(pos);
  18. }
  19. yield return null;
  20. } while (Time.time - t0 < howLongWillTheyComply);
  21. }
  22. }
  23. }