RVOAgentPlacer.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace Pathfinding.Examples {
  4. /// <summary>
  5. /// Places ROV agents in circles.
  6. /// Used in a example scene
  7. /// </summary>
  8. [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_examples_1_1_r_v_o_agent_placer.php")]
  9. public class RVOAgentPlacer : MonoBehaviour {
  10. public int agents = 100;
  11. public float ringSize = 100;
  12. public LayerMask mask;
  13. public GameObject prefab;
  14. public Vector3 goalOffset;
  15. public float repathRate = 1;
  16. // Use this for initialization
  17. IEnumerator Start () {
  18. yield return null;
  19. for (int i = 0; i < agents; i++) {
  20. float angle = ((float)i / agents)*(float)System.Math.PI*2;
  21. Vector3 pos = new Vector3((float)System.Math.Cos(angle), 0, (float)System.Math.Sin(angle))*ringSize;
  22. Vector3 antipodal = -pos + goalOffset;
  23. GameObject go = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.Euler(0, angle+180, 0)) as GameObject;
  24. RVOExampleAgent ag = go.GetComponent<RVOExampleAgent>();
  25. if (ag == null) {
  26. Debug.LogError("Prefab does not have an RVOExampleAgent component attached");
  27. yield break;
  28. }
  29. //ag.radius = radius;
  30. go.transform.parent = transform;
  31. go.transform.position = pos;
  32. ag.repathRate = repathRate;
  33. ag.SetTarget(antipodal);
  34. ag.SetColor(GetColor(angle));
  35. }
  36. }
  37. const float rad2Deg = 360.0f/ ((float)System.Math.PI*2);
  38. public Color GetColor (float angle) {
  39. return AstarMath.HSVToRGB(angle * rad2Deg, 0.8f, 0.6f);
  40. }
  41. }
  42. }