- Home /
How do I stop an Enemy NavMeshAgent from intersecting the player model?
When I have the player walking and the enemy model static, they collide with each other as expected.
When I make the enemy a Nav Mesh Agent, they stop colliding. If I use Ctrl+D to create multiple Nav Mesh Agent enemies , they collide with each other, but still not with the player model.
Why will they no longer collide with my player model, and how can I get them to? As a top-down shooter, it's quite important. (The player is using the 'character controller' and collides with every other object.)
Answer by asafsitner · May 03, 2012 at 12:56 PM
Just add a NavMeshAgent
component to the player as well. The enemies, being NavMeshAgent
s are driven around using an entirely different system than physics or CharacterControllers
, instead being driven by CrowdManager
(see the profiler). They're only aware of other entities that are driven by the CrowdManager, and only if their avoidance
property is set to something other than None
(obviously the better the avoidance quality the more resources are required).
Adding a NavMeshAgent
component to the player - even if you keep moving it around with the CharacterController
! - will allow the other NavMeshAgent
s to perceive it and avoid collision with it.
A nice side-effect of this is that your player will be able to push enemies aside as it's colliding with them, which could be useful to, say, throw enemies off cliffs and to oblivion.
This answer and explanation is perfect! Thanks very much!
"A nice side-effect of this is that your player will be able to push enemies aside as it's colliding with them, which could be useful to, say, throw enemies off cliffs and to oblivion."
This is the effect I was hoping for also!
I did try to add the Nav$$anonymous$$eshAgent to the player but it effected the movement, and I wasn't sure it was the best approach. Now I know it's the correct thing to do, I'll try to get it working.
Thanks again! This is a great help!
I don't get it. If I attach the Nav$$anonymous$$eshAgent to my character, it won't allow me to rotate the camera, as it always goes back to the original position. I'm confused.
Another useful alternative is to check on script the distance from enemy to character and just stop the agent. Curiously the only way to do this is to use a new path which is the position of the enemy since agent.Stop() is not working for me.
Answer by bubbabruce · Apr 28, 2017 at 06:00 AM
I attach a NavMeshObstacle to the player and that seems to work.
Use it with carving unchecked. It mentions using this for player characters: "This mode is best used in cases where the obstacle is constantly moving (for example, a vehicle or player character)"
Answer by barmhartsam · Mar 03, 2017 at 06:19 PM
@asafsitner I know this is old but I have queries/ Setting the Navmesh on my player that is driven by Characther Controller messes with the ground check of my Charachter controller. Also the Friendly NPC walks underneath my player while pathfinding. If no Navmesh is attached to playe it walks throuh player. Any advice.