Collision between sambe objects with same Layer


i am kind of confused as i am trying to make one type of enemy prevent coliding with other enemies of the same type ( its the same enemy instantiated more times). I wanted to use checksphere, but here i get into the problem. If i just use the checksphere it collides with itself. But when i assign layer, which the checksphere should check, it also check itself. And when i invet the layer, then of course it wont find the enemy as all enemies are inverted.

I am realy confused how to check collisions between enemies of the same type. Please note i need them to check close position, not collision. I.e. i need them to evade each other, not collide, thats why i do the check. I tried raycasting, but its too heavy and i would need raycasting in more directions and it just isnt working too good in comparsion as how expensive it is.

Thanks for any advise.


Make sure you have setup the collision matrix as you need it.

You can use Physics.IgnoreCollision to setup an individual behaviour, but layer based collsion is more appropriate in your case.

To get all nearby colliders use Physics.OverlapSphere. It will return an array of all colliders that are "in range". You can also specify a layermask.


I think i still don't get your problem.

Do you :

  1. want no collisions by the Unity physics-system because you want to use your own collision-avoidance-system?
  2. need help creating your own flocking style AI?
  3. just need some help to understand how layerbased collisions or raycasts/OverlapSphere works?

In case of 1. i recommend to ignore collisions for the layer in your collision matrix. That way the physics system will not detect collisions between objects of that layer. Now you have to take care of when two enemies come close and what they should do to avoid collisions.

If you don’t know how collision avoidance works, well, you have to inform yourself. There are a lot of different ways to implement such a behaviour. Collision avoidance is continuous process. Like i mentioned above with OverlapSphere you can get a list of all nearby enemies that are within a certain range. I can’t tell you how your collision response should look like because it depends on how your enemies are moved. If they can turn instantly or not, if they can move sideways/backwards or not.

You just said you have problems but your explanations are not very clear. Maybe you can update your question and post some of your code. I'm trying to read between the lines but i can't spot your actual problem.