Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. static var waypoints = Array();
  2. var connected = Array();
  3. static var kLineOfSightCapsuleRadius = 0.25;
  4.  
  5. static function FindClosest (pos : Vector3) : AutoWayPoint {
  6. // The closer two vectors, the larger the dot product will be.
  7. var closest : AutoWayPoint;
  8. var closestDistance = 100000.0;
  9. for (var cur : AutoWayPoint in waypoints) {
  10. var distance = Vector3.Distance(cur.transform.position, pos);
  11. if (distance < closestDistance)
  12. {
  13. closestDistance = distance;
  14. closest = cur;
  15. }
  16. }
  17.  
  18. return closest;
  19. }
  20.  
  21. @ContextMenu ("Update Waypoints")
  22. function UpdateWaypoints () {
  23. RebuildWaypointList();
  24. }
  25.  
  26. function Awake () {
  27. RebuildWaypointList();
  28. }
  29.  
  30.  
  31. // Draw the waypoint pickable gizmo
  32. function OnDrawGizmos () {
  33. Gizmos.DrawIcon (transform.position, "Waypoint.tif");
  34. }
  35.  
  36. // Draw the waypoint lines only when you select one of the waypoints
  37. function OnDrawGizmosSelected () {
  38. if (waypoints.length == 0)
  39. RebuildWaypointList();
  40. for (var p : AutoWayPoint in connected) {
  41. if (Physics.Linecast(transform.position, p.transform.position)) {
  42. Gizmos.color = Color.red;
  43. Gizmos.DrawLine (transform.position, p.transform.position);
  44. } else {
  45. Gizmos.color = Color.green;
  46. Gizmos.DrawLine (transform.position, p.transform.position);
  47. }
  48. }
  49. }
  50.  
  51. function RebuildWaypointList () {
  52. var objects : Object[] = FindObjectsOfType(AutoWayPoint);
  53. waypoints = Array(objects);
  54.  
  55. for (var point : AutoWayPoint in waypoints) {
  56. point.RecalculateConnectedWaypoints();
  57. }
  58. }
  59.  
  60. function RecalculateConnectedWaypoints ()
  61. {
  62. connected = Array();
  63.  
  64. for (var other : AutoWayPoint in waypoints) {
  65. // Don't connect to ourselves
  66. if (other == this)
  67. continue;
  68.  
  69. // Do we have a clear line of sight?
  70. if (!Physics.CheckCapsule(transform.position, other.transform.position, kLineOfSightCapsuleRadius)) {
  71. connected.Add(other);
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement