- Home /
How to set up the CollisionIgnoreManager script to ignore a layer (C# question)
I have two kinds of object that are instantiated together throughout the game, and they must occupy the same space and both collide with the player but not each other. Unity seems to have no automatic way to turn on and off collision between objects by layer or tag or anything- and badly needs this feature- but I found a script to do it, (Thanks, Ryan Scott!) It's C# and I just barely learned JavaScript and don't know how to even edit a C# script.
It says in the docs,
"A simple usage case would be putting various objects onto different layers and using the layer masks for the bitmasks."
But how?
The script as it comes does not expose any variables in the Inspector and appears to declare them all inside functions. What do I need to add to it to make the object it is attached to it ignore collisions with all objects on a layer of my choosing?
Answer by Peter G · Apr 20, 2010 at 10:23 PM
Normally, you can just use Physics.IgnoreCollision() to tell Unity to ignore the collision between two objects. The link does that in a more complex way that allows for groups of objects to be ignored.
This link explains how Physics.IgnoreCollision() works. Your code could be something like this.
//This code would go on one of your two spawned objects. //You could insert parts of this into your current code and not have to make twin //variables.
var twin1 : Collider; var twin2 : Collider;
function Start () { twin1 = collider; }
function Update () { Physics.IgnoreCollision(twin1, twin2); }
I thought that would work for already existing objects but not for multiple instances of a yet-to-be-spawned prefab.
Actually, I figured out that I can connect one object to the other with a fixed joint, and this will also stop them from trying to collide; and I can do most of the other stuff I need to with triggers.
It works for spawned objects. That's how the FPS tutorial makes sure the rocket's don't collide with the launcher, but glad you solved it on your own.
Answer by Ryan Scott · Oct 19, 2010 at 01:43 AM
Hi. Sorry I've not been paying attention to Answers.
The script internally calls Physics.IgnoreCollision(). The "Layer" thing I referred to isn't "Layer" in the Unity sense. The integer parameters work as bit masks. Such that each of the 32 bits can be turned on or off to treat objects as groups (this is similar to how unity implements their layers).
(http://en.wikipedia.org/wiki/Mask_(computing) is a fine place to start looking into that if you've not used them before)
To set layer 20, then, you would be able to create a mask by specifying that by: int thisMask = 1 << 20;
To set layers 5 and 20: int thisMask = 1 << 5 | 1 << 20;
In this way the system calls IgnoreCollision on each pair of objects between the groups specified in the masks of each parameter.
There are two primary functions to use: public void addIgnore( Collider newCollider, int thisMask, int mask ) public void addIgnore( Collider newCollider )
The first one takes two masks (specifying one or more groups each) and calls the ignore function appropriately. The second causes the specified collider to ignore all objects in the system.
Hope that answers your question