Skip to content

Instantly share code, notes, and snippets.

@moon-goon
Created February 29, 2016 03:18
Show Gist options
  • Save moon-goon/e073178cb313fe43a036 to your computer and use it in GitHub Desktop.
Save moon-goon/e073178cb313fe43a036 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class AI_StateMachine : MonoBehaviour {
public Transform[] target;
private int destPoint = 0;
NavMeshAgent agent;
private float distance = 0.0f;
void Start () {
agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement between points
agent.autoBraking = false;
GotoNextPath();
}
void Update () {
if (agent.remainingDistance < 0.5f)
GotoNextPath();
}
void GotoNextPath() {
// Returns if no points have been set up
if (target.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = target[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % target.Length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment