- Home /
How to avoid collision between two navmesh agent?
In my 3d game I have multiple enemy which follow player using navmesh but when they follow player I want to maintain some distance between them so they doesnot collide or intersect with each other. So how can I achieve this?
Answer by GreasySam · Apr 24 at 02:43 AM
you could use a rayCast to sense weather another enemy is in front of it and stop it if there is. public LayerMask layer;// set this to the enemy layer float senseDistance = 50;// distance till other enemy sensed
var ray = new Ray(this.transform.position, this.transform.forward);//creates raycast if(Physics.Raycast(ray, out hit, senseDistance, layer)) /checks weather the ray hits up to the senseDistance distance, and if the layer it hit is an enemy/ { //stop moving(); } else { //move(); {
But in my game enemy can be in right or left of other enemy so I have to use raycast in all four direction?
that would be a good solution, you could move the enemy away from the other when it senses one, or use a sphereCast (Physics.SphereCast()) https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
Is there any way that my all enemies follow player and also does not interact with each other? If I use physics.sphere than all enemy will use it and when two enemy come closer than both will stop each other.
Your answer
