# How to prevent collisions between instantiated prefabs

I'm creating a virtual creature evolution system like here. This type of project requires to define creatures, which are made of some 3d boxes and simulate them over and over again, making little changes along the way, until the point where they are good enough at a certain task. Basically, I'm using Genetic algorithms. I'm going to be doing a lot of simulation (a few hundred creatures each generation, times X generations, where X could be in the thousands). In order to save time, I want to simulate multiple creatures at once, so I must run collision checks efficiently. Each creature exists on its own and therefore should not be bothered by other colliders except himself and the ground.

I have a prefab of a creature. Each creature is made of multiple 3d boxes with colliders. The nodes in the same creature should collide with themselves but should not collide with nodes that are in other creatures (because it should learn to perform based on the environment as it is). What is the most efficient way I could do this?

I thought about checking the root of the collision object but that just prevents the collision and doesn't eliminate the useless checks that are being made. I've also looked at solutions involving the "Layer Collision Matrix" but still could not find a way to make it work.

• If these objects are positioned far enough apart, then the physics engine's broadphase pass should solve this for you, only considering interactions between colliders that are nearby. Jan 29, 2020 at 16:24
• @DMGregory how could i know what distance between creatures is large enough? Is there some sort of documentation? Jan 29, 2020 at 20:34
• Jan 27, 2021 at 13:40

I ended up using the suggestion of @allnodcoms to use layers. I queue up a full generation to be simulated on 16 separate layers using a SimulationManager. Although this isn't the large amount I was looking for, it allows me to increase the speed of the simulation more than if I were to simulate 100 creatures at once.

I don't know about its efficiency but you can use ignore collision function: Physics.IgnoreCollision

It enables you to ignore collisions of two specific collider.

• The problem with Physics.IgnoreCollision is that it allows you to blacklist collider pairs, but what OP needs is a way to whitelist them. I didn't test it, but using that method to forbid every collider with every other collider could cause performance problems. OP has about 1000 colliders, so there would be about one million blacklist entries in the collision system. That can not be performant. Jan 29, 2020 at 11:33

This is a pretty specific requirement, and without knowing more about why you need this functionality it's difficult to suggest a tailored solution.

I would initially suggest removing the collisions entirely and adding a script to the main creature container which checks the relationship between the various child objects, but again, without knowing why you need to check for internal collisions and not external ones, it is hard to suggest a more definitive alternative.

Danny

• Did the changes I made clear why I need this functionality? Jan 30, 2020 at 13:13
• Sort of... I would still look into a collisionless system if it is at all possible, but if you are going to be bound by the limits of the physics system. The collision matrix allows you to specify which objects collide with each other, and this is done through layering. Unity only supports 31 (8 of which are predefined), which doesn't suit your needs, but for use in a 'proof of concept' you might be able to restrict the number of creatures per generation. Assigning each creature a layer (plus one for 'environment') and setting each layer to only collide with itself would answer the question. Jan 30, 2020 at 14:00

Well I know this is answered already, but I did a little research of my own for a personal project and I believe this should work for any prefab looking to avoid contact with any of the same item. If you give the prefab object (if it's a group this applies to all objects with colliders) a unique tag, like "ObjectTag" for example then you can simply use:

Physics.IgnoreCollision(prefabObject.GetComponent<Collider>(), GameObject.FindGameObjectWithTag("ObjectTag").GetComponent<Collider>());


with of course "prefabObject" and "ObjectTag" being replaced by whatever names you wish to use. As prefab objects keep their tags this essentially allows you to bypass the rule of not being allowed to refer to the same object twice in Physics.IgnoreCollision.

I am very new to coding and Unity so if I missed anything I apologize.

• This seems to be the same suggestion made by Arlandria last year, though you've added a better code example. Jan 27, 2021 at 13:37