- Home /
Why isn't my enemy pausing in between waypoints? C#
My enemy is using a completely custom AI made using nothing but unity's default tools. It uses a NavMesh and NavMeshAgents.
I'm having problems where I want my enemy to stop for 4 seconds after each waypoint is reached, but they're not stopping.
Edit: It also seems that as time goes on the AI starts doing more and more new paths at one time. It starts out working perfect, after 30 seconds it starts doing two path calculations rapidly, a minute, and we're up to 3.
Here's my code:
if (nav.remainingDistance < 0.1)
{
nav.Stop();
nav.ResetPath();
if(hasMoved && !newDestCalled)
{
Debug.Log ("Trying to call New Destination");
newDestCalled = true;
StartCoroutine(NewDestination ());
}
else
{
waypointRandom = Random.Range(0, waypoints.Length);
Debug.Log (waypointRandom);
nav.SetDestination(waypoints[waypointRandom].position);
hasMoved = true;
}
IEnumerator NewDestination()
{
yield return new WaitForSeconds(4);
waypointRandom = Random.Range(0, waypoints.Length);
Debug.Log (waypointRandom);
nav.SetDestination(waypoints[waypointRandom].position);
newDestCalled = false;
}
Answer by tanoshimi · Jun 27, 2014 at 10:16 PM
I'm assuming that your if (nav.remainingDistance < 0.1)
bit is called in an Update() loop? If so, there's a flaw in your logic.
It's unclear what the ifMoved
variable does, but it actually doesn't matter. Let's assume that ifMoved
is true to start with, and that newDestCalled
is false, in which case the first if
block gets executed and calls your NewDestination coroutine (which is where you're assuming a 4 second pause will be introduced).
But, on the very next frame, newDestCalled
is now true (since it was set in line 8), which means that the else
block now gets executed and immediately sets a new nav destination, without waiting... so there's never a time when the pause introduced by the coroutine would take be noticeable.
It should be the other way around. newDestCalled is BEFORE the coroutine, so shouldn't it be executed first?
Yes it's being called in update. I didn't see any way to clear the destination when it was reached, so this was my logic for doing that. has$$anonymous$$oved is essentially if they've already done the initial movement, I don't know why it's in there, looking at it now....haha.
Guess it's just a relic of the way I had it working to get it to this point.
I think I may see what you're talking about, let me try to fix it real quick.
Okay your first part confused me, that was indeed exactly right! Thank you. To sum it up:
$$anonymous$$y 2nd part of the if statement was a relic and unneeded, and was causing the problems.
Your answer
Follow this Question
Related Questions
How do I limit the path of a NavMeshAgent? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
NavMeshAgent Finding Alternative Route 1 Answer
Enemy Spawn Script Spawning Infinite Enemies [Help] 2 Answers
How do I get my emeny AI to detect if he sees a player, or one of the good AI? (C#) 3 Answers