- Home /
Collision between sambe objects with same Layer
Hi,
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.
Luke
Can't you use Vector3.Distance to see how close they are and make them move away from eachother when they are too close?
thanks for reply. I was thinking about this solution, but i am sure it would be very expensive as there can be up to 10 instances of same object in scene, so you would be comparing one object distance to 9 other objects each frame, then doing another calculations for translating position. This happing for each of these object.
Also i dont know how i would find all those objects, because they all have same names and tags, so how the object would know wich one is which to move in opposite direction.
Iam sure there must be way with something similar to checkSphere, but i dont know about it :(
Answer by Bunny83 · Apr 28, 2011 at 05:20 PM
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.
edit
I think i still don't get your problem.
Do you :
- want no collisions by the Unity physics-system because you want to use your own collision-avoidance-system?
- need help creating your own flocking style AI?
- 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.
thanks for reply, but i think this wouldnt help as it utilize layer masks again or i guess i dont understand it well. How does this help with the problem of the same instantiated objects checking for each other. As i said, i am not looking for collision, but more for something like checksphere where objects checks if other is in close range.
I am sorrry if i wasnt clear. I am looking for something like flocking AI (i.e. enemies are aware of each other, they are moving, but they dont collide with each other). I will try to look up more on the flocking AI, but if you can direct me to some good resource, i would be very thankful. ( Also i dont want them to have any collisisons, also another thing is all the enemies have same tags, same layers as they are one instantiated type, thats why i cant use something like checksphere )
Your answer
Follow this Question
Related Questions
Obstacle Avoidance 0 Answers
BoxCollider2D is not working as expected 1 Answer
How does Raycast layer mask work? 1 Answer
How do I find the normal of the closest surface (in a specific radius)? 3 Answers
Align to grid in runtime problem 1 Answer