Switch tortoise movement to NavMeshAgent
Remove turtle animation of patrolling a route and switch to using NavMeshAgent to patrol between points.
This commit is contained in:
1 parent
288d522f6a
commit
d6cb792f25
6 files changed
+316
-740
No files matched your search
@@ -0,0 +1,48 @@
|
||||
// Patrol.cs
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class Patrol : MonoBehaviour
|
||||
{
|
||||
public Transform[] points;
|
||||
private int destPoint = 0;
|
||||
private NavMeshAgent agent;
|
||||
|
||||
void Start ()
|
||||
{
|
||||
agent = GetComponent<NavMeshAgent>();
|
||||
|
||||
// Disabling auto-braking allows for continuous movement
|
||||
// between points (ie, the agent doesn't slow down as it
|
||||
// approaches a destination point).
|
||||
agent.autoBraking = false;
|
||||
|
||||
GotoNextPoint();
|
||||
}
|
||||
|
||||
|
||||
void GotoNextPoint()
|
||||
{
|
||||
// Returns if no points have been set up
|
||||
if (points.Length == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the agent to go to the currently selected destination.
|
||||
agent.destination = points[destPoint].position;
|
||||
|
||||
// Choose the next point in the array as the destination,
|
||||
// cycling to the start if necessary.
|
||||
destPoint = (destPoint + 1) % points.Length;
|
||||
}
|
||||
|
||||
|
||||
void Update ()
|
||||
{
|
||||
// Choose the next destination point when the agent gets
|
||||
// close to the current one.
|
||||
if (!agent.pathPending && agent.remainingDistance < 0.5f)
|
||||
GotoNextPoint();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user