Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace Pathfinding {
  5.     /** Simple patrol behavior.
  6.      * This will set the destination on the agent so that it moves through the sequence of objects in the #targets array.
  7.      * Upon reaching a target it will wait for #delay seconds.
  8.      *
  9.      * \see #Pathfinding.AIDestinationSetter
  10.      * \see #Pathfinding.AIPath
  11.      * \see #Pathfinding.RichAI
  12.      * \see #Pathfinding.AILerp
  13.      */
  14.     public class Patrol : MonoBehaviour {
  15.         /** Target points to move to in order */
  16.         public Transform[] targets;
  17.  
  18.         /** Time in seconds to wait at each target */
  19.         public float delay = 0;
  20.  
  21.         /** Current target index */
  22.         int index;
  23.  
  24.         IAstarAI agent;
  25.         float switchTime = float.PositiveInfinity;
  26.  
  27.         void Awake () {
  28.             agent = GetComponent<IAstarAI>();
  29.         }
  30.  
  31.         /** Update is called once per frame */
  32.         void Update () {
  33.             if (targets.Length == 0) return;
  34.  
  35.             bool search = false;
  36.  
  37.             if (agent.reachedEndOfPath && !agent.pathPending && float.IsPositiveInfinity(switchTime)) {
  38.                 switchTime = Time.time + delay;
  39.             }
  40.  
  41.             if (Time.time >= switchTime) {
  42.                 index = index + 1;
  43.                 search = true;
  44.                 switchTime = float.PositiveInfinity;
  45.             }
  46.  
  47.             index = index % targets.Length;
  48.             agent.destination = targets[index].position;
  49.  
  50.             if (search) agent.SearchPath();
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement