- Home /
Physics.IgnoreCollision by Tag question
hello & thanks for looking at my question. should be a simple one. I'm trying to make an tagged gameobject not collide with another tagged gameobject but Physics.IgnoreCollision is telling me they are both the same object. can't wrap my head around the concept yet. script is attached to LeftStopper gameobject. i know how i would do it via javascript based just can' get the same idea to work in C#. or am i doing it completely wrong? tried it so many different ways with no luck.
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.tag == "RightMarble" && collider.gameObject.tag == "LeftStopper")
{
Debug.Log("Collided RightMarble with _LeftStopper");
// would like a Physics.IgnoreCollision(a,b) here
}
}
You simply need not mention the one you want to ignore. Except if both object have rigidbody, if you don't want anything to happen don't say so.
If they have rigidbody, I guess you need http://unity3d.com/support/documentation/ScriptReference/Physics.IgnoreLayerCollision.html
Answer by Kleptomaniac · Mar 18, 2012 at 04:50 AM
You are using OnCollisionEnter in the wrong way. OnCollisionEnter is referencing collision with the colliders attached to the object which the script is attached to (your LeftStopper gameObject). Therefore, you only need to check for a collision with RightMarble. So like this:
if(collision.gameObject.tag == "RightMarble" ) {
I believe that should work. Hope it helps! Klep
And actually, now that I think about it, it should be void OnCollisionEnter(Collider collision) { :P
thanks $$anonymous$$lepto for getting back to me. appriciated. cheers for the tip , i seemed to be double checking for nothing in that bit. i already had the debug trace working so seems like i wasn't having problems there. i also tried your second tip to use 'OnCollisionEnter(Collider collision)' doesn't seem to achieve any different results. my main concern is how to use Physics.IgnoreCollision if you or anyone else can help.
@$$anonymous$$leptomaniac: your comment isn't true. The trigger functions just have a Collider parameter. All collision functions (Enter / Stay / Exit) have a Collision parameter.
@$$anonymous$$UX: Your idea doesn't work. OnCollisionEnter is called after a collision has occurred. Physics.IgnoreCollision can only be used with two concrete colliders to disable the collisions between those two object. Unity doesn't support tag-sensitive collisions. The collsions can only be enabled / disabled per layer or per object pair.
If you have a group of objects that shouldn't collide with another group, place them on seperate layers. See layer-based collisions
@Bunny83 cheers, thanks for getting back to me & pointing that out.. i will look into layer-based solution then & try to figure it out that way ins$$anonymous$$d
Answer by MUX · Mar 18, 2012 at 02:13 PM
yes a layer-baised solution was exactly the thing i needed. how nifty is that feature! just when i almost gave up, i solve it without code! thanks @Bunny83 (& @fafase above) for the tips.
ps. sorry, im new to this question form, so how do mark question as solved?
On you rpost you have 3 or 4 tags like edit and else (don't remember) Click the close tag, it opens a window that asks the reason of the closing. Choose answered.
Your answer
![](https://koobas.hobune.stream/wayback/20220613065236im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trouble with Physics.IgnoreCollision 0 Answers
2D box colliders not touching but are colliding, how to fix? 0 Answers
How to hit objects in VR 1 Answer
rigidbody2D.addforce in collider 1 Answer
How to move an object over a distance in a direction? 2 Answers