Home > OS >  how to make all NPCs with NavMeshAgent Compoent Avoid Player's gameobject witout NavMeshAgent c
how to make all NPCs with NavMeshAgent Compoent Avoid Player's gameobject witout NavMeshAgent c

Time:01-09

In my project player's gameobject has it's own script to control motion. I notice that if I attach the NavMeshAgent to player's gameobject ,I always get some unexpected motions. So I disabled it. Is there any way to make all NPCs with NavMeshAgent compoent avoid Player's gameobject witout NavMeshAgent compoent? Or prevent any unexpected motion when I attach NavMeshAgent to player's gameobject?

I had tried disabled the NavMeshAgent on my player gameobject,and the NPC seem to be blind,they went through player directly.

CodePudding user response:

  1. One option is to use a NavMeshObstacle component on the player's gameobject. This will cause NPCs with NavMeshAgents to avoid the player's gameobject as if it were an obstacle.
  2. You can also use raycasts or spherecasts to detect when an NPC is approaching the player's gameobject and manually adjust the NPC's movement to avoid the player.
  3. Finally, if you are using Unity's built-in physics system (e.g. Rigidbody and Colliders), you can use Physics.IgnoreCollision to prevent the player's colliders from interacting with the NPCs' colliders. This can help to prevent unexpected collisions and motion but that might not be what you want as a behaviour.
  • Related