- Home /
How can i make the ai stop at every waypoint for a few seconds?
Making an stealth game where enemy have a cone field of view, and have a set waypoint. Followed the unity doc for the waypoints for enemy, but i cant seems to figure out how to make the enemy stop for a few seconds at every waypoint. This code below is the one from Unity doc.
public Transform[] points;
private int destPoint = 0;
void Start()
{
//other line of codes....
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
void Update()
{
if (!agent.pathPending && agent.remainingDistance < 0.5f)
{
GotoNextPoint();
}
}
I have tried different mention, such as:
private waitTime;
public startWaitTime;
do a Time.deltatime, then put if(waitTime <=0).... it works that he stay at that waypoint for a few seconds, but hes not standing still, but circling around the waypoint then move on to the other.
i have also did agent.speed =0 when at waypoint, and agent.speed = 3 when the waitTime is <=0, but doing it this way, the ai will only follow the player when hes moving to the next waypoint.
Answer by Hellium · Oct 21, 2018 at 02:42 PM
Haven't tested the following script but give it a try
public Transform[] points;
public float waitDuration = 5f;
private int destPoint = 0;
private float waitTimer;
void Start()
{
//other line of codes....
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
if (points.Length == 0)
return;
agent.destination = points[destPoint].position;
destPoint = (destPoint + 1) % points.Length;
}
void Update()
{
if (!agent.pathPending && agent.remainingDistance < agent.stoppingDistance * 1.1f ) // or keep < 0.5f if you want
{
waitTimer += Time.deltaTime;
if( waitTimer > waitDuration )
{
waitTimer = 0 ;
GotoNextPoint();
}
}
}
it have the same result, going into the waypoint, co$$anonymous$$g back out, again and again until the timer reach 0.
Can you indicate the valued you gave to stoppingDistance
and speed
?