- Home /
Stop a Navmesh agent mid path
i have a third person hack 'n slash i'm working on and no, it is not a button masher. The combat needs skill to execute attacks plus no auto targeting. Anyhoo, I've hot the player side of things set up and now comes the AI. I'm using Navmesh and mecanim to move the agents. The agent has a rigidbody with a custom third person controler attached. I can get him to patrol nicely. Then the player shows up and he has to stop and come towards the player. However my agent is determined to finish his patrol route.
void Start (){
navmeshAgent.updateRotation = false;
navmeshAgent.updatePosition = true;
target = GameObject.FindGameObjectWithTag("waypoint2").transform;
//find game object will change to specific transforms later
}
//then later in update:
navmeshAgent.SetDestination(target.position);
if (navmeshAgent.remainingDistance > navmeshAgent.stoppingDistance)
enemyChar.move(navmeshAgent.desiredVelocity);
else
enemyChar.move(Vector3.zero);
//much later on I pass the Vector3 values to mecanim and animate the mesh.
//this is in another class though
Now go ahead and try to enemyChar.move(Vector3.zero); He wont stop. navmeshAgent.ResetPath()? No thanks. Set him a new destination? He likes his old one. I did manage to het him to stop by miving the waypoint but that's not the way to do it I think. Help.
Answer by M-Hanssen · Apr 22, 2016 at 10:10 AM
navmeshAgent.Stop();
navmeshAgent.ResetPath();
Should do the trick
Sir/ma'am you under estimate the deter$$anonymous$$ation of my enemy
This did the trick for me - but without the navmeshAgent.Stop(); since this have been deprecated. Thanks
Answer by carlqwe · Apr 22, 2016 at 09:59 AM
If you want to stop the moving object when its for instance half way to its destination, then u can set the destination to the objects current position.
MyNavMeshAgent.destination = gameObject.localPosition;
Or even easier set the target to the gameObjects local position
target = gameObject.transform.localPosition;
Doesn't work? Check out this unity documentation
I tried transform.gameObject.transform.position. The enemy (yes he's become my enemy) just ignores me. But if I move the transform of the waypoint he responds. It's like i can't modify anything related to the NavmeshAgent at all. Setting the move Vector to zero just makes him jerk a bit then stop. The code is modified from the unity's examples
Answer by noGrip · Aug 30, 2021 at 08:28 AM
It has been some years since the last reply. But this works for me now:
nav.velocity = Vector3.zero;
nav.isStopped = true;
It keeps complaining for me, whenever enemy gets in range and keeps on moving on, even though it should stop.
Here's my error: "Stop" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.StackTraceUtility:ExtractStackTrace ()
Can you help with this, please?
Don't know if I can help much, without seeing a code snippet. But the error description seems to indicate that your agent is not on the NavMesh.
I would check NavMesh.SamplePosition: [link text][1] [1]: https://docs.unity3d.com/ScriptReference/AI.NavMesh.SamplePosition.html
I decided not to make it stop, but to try and approach the enemy if it gets in range. It works a lot better, than stopping would have, so I'm happy with it.
The lines that make it work basically are as follows:
public void AttackEnemy()
{
if (Target == null)
{
Attack(false);
Move();
}
else
{
float maxDistance = Vector3.Distance(transform.position, Target.transform.position);
stance.LookAt(Target.transform);
if (maxDistance < range)
{
Attack(true);
Approach();
}
else
{
Attack(false);
Move();
}
}
}
public void Attack(bool isActive)
{
var emissionModule = projectileParticles.emission;
emissionModule.enabled = isActive;
}
public void Move()
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(goal.transform.position);
}
public void Approach()
{
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.SetDestination(Target.transform.position);
}
Your answer
Follow this Question
Related Questions
Rotate animation at waypoint 1 Answer
NavMeshAgent: Keep Enemy x Distance from Player? 1 Answer
Teddy Bear Mecanim NavMesh (my animations don't work) 0 Answers
NavMeshAgent animation - sliding 2 Answers
Navmesh stuck on obstacle 0 Answers