- Home /
Can you have different collision box type with one gameobject.
I am doing a 2D game with Unity.
I have a character that is a plane with a collision box around. I tried to make it contain an empty object (drag and drop a gamobject on another one it appear inside, do not know how it is called) with another collision box.
But the new collision box trigger the OnCollisionEnter from the root gameobject. I wanted to be able to differentiate colission depending wich box was hit.
Is it possible to have collision elements triggereing different responses for the same logical object?
Answer by pyro · May 25, 2011 at 04:06 PM
You could do something like this:
//added to child
function OnCollisionEnter(c : Collision)
{
if (c.gameObject.transform == transform.parent) return;
//rest of code
}
or
//added to parent
function OnCollisionEnter(c : Collision)
{
if (c.gameObject.transform.parent == transform) return;
//rest of code
}
gameobject return the object that collided with me, not the collider itself. Beside, the OnCollisionObject of the children object is never triggered, it seems the parent has some kind of priority.
the code above is checking "is the object that collided with me my parent? if so exit out early" and you would add that version to the child; the second code checks "am I the parent of the gameobject I collided with? if so exit the function early" and you would add that version to the parent, it's either or you would not use both. Also you can use physics layers in Edit->Project Settings->Physics
Ok, but it is a "did i collide with myself" situation. Here i am colliding another object, and i want to know if it is the parent or the child that has been touched by another object non related to my gameobject.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
support not detected collision and Leasehold 0 Answers
How to add to a 3D object's length through code? 1 Answer
Simple collision? 2 Answers