ExampleBubble.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Collections;
  2. using UnityEngine;
  3. namespace Chronos.Example
  4. {
  5. // This class is pretty much identical to ExampleNavigator, except
  6. // that it doesn't take into consideration Chronos. It's just used
  7. // as an utility to move the area clock bubbles in the example scene --
  8. // this script is not an example by itself.
  9. [RequireComponent(typeof(UnityEngine.AI.NavMeshAgent))]
  10. public class ExampleBubble : MonoBehaviour
  11. {
  12. public float delay = 5;
  13. public Vector3 areaCenter;
  14. public Vector3 areaSize;
  15. private void Start()
  16. {
  17. StartCoroutine(ChangeDestinationCoroutine());
  18. }
  19. private IEnumerator ChangeDestinationCoroutine()
  20. {
  21. while (true)
  22. {
  23. ChangeDestination();
  24. yield return new WaitForSeconds(delay);
  25. }
  26. }
  27. private void ChangeDestination()
  28. {
  29. Vector3 randomDestination = new Vector3()
  30. {
  31. x = areaCenter.x + Random.Range(-areaSize.x, +areaSize.x) / 2,
  32. y = areaCenter.y + Random.Range(-areaSize.y, +areaSize.y) / 2,
  33. z = areaCenter.z + Random.Range(-areaSize.z, +areaSize.z) / 2,
  34. };
  35. GetComponent<UnityEngine.AI.NavMeshAgent>().SetDestination(randomDestination);
  36. }
  37. private void OnDrawGizmosSelected()
  38. {
  39. Gizmos.color = Color.magenta;
  40. Gizmos.DrawWireCube(areaCenter, areaSize);
  41. }
  42. }
  43. }