- Home /
Colliding with the tag of a Child Object
INFO.... I have a Empty with A few Cubes in it as my "Block" GameObject w/rigidbody. The Cube's each have six box collider's on each side that are flat and a tag of Y-,Y+,X-,X+,Z-,Z+ depending where the side is in world space.
Question.... I need to find out if the "Block" I'm controlling has collided with a Child tag of another "Block" I Have in the scene.
So this is what I would like if it is possible.
function OnCollisionEnter(other : Collision)
{
if (""other.child.tag"" == "Y-")
{
DO SOMETHING;
}
}
not sure what to put in the ""other.child.tag"" part
When I use
function OnCollisionEnter(other : Collision)
{
if (other.gameObject.tag == "Y-")
{
Do Something;
}
}
It does nothing, and I'm guessing because its getting the parents tag not the child's.
UPDATE.....I wanted to clarify my set up. I have a empty with a rigidbody. In that empty I have a cube that has six child colliders as the sides. I need my collider code to be with the empty.
no, that doesn't stand to reason.. any child object with a collider will be able to be detected individually, regardless of their parent/hierarchy.. $$anonymous$$uch more likely your function is never being entered at all.. forget checking for the right tag, just try this:
function OnCollisionEnter(other : Collision)
{
Debug.Log("HIT! :" + other.gameObject);
}
my guess is nothing happens.. You might try triggers ins$$anonymous$$d, or otherwise you need to add rigidbodies to the CUBES themselves
here are a few links regarding proper set-up of collisions:
http://unitygems.com/mistakes1/
and here, at the bottom, the collision action matrix:
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
EDIT:
On closer inspection of your described set-up, you may be right, I was misunderstanding the "block v block" dynamic.. I would say, test with a debug like I suggest.. if you get the parent, you may want to simply use 6 rigidbodies, one for each collider..
OR maybe try going to the collider, rather than directly to the gameObject:
function OnCollisionEnter(other : Collision)
{
if(other.collider.tag == "Y-") ... etc
I suspect that may be just crazy enough to work...
Or you can try to loop your object
function OnCollisionEnter(coll : Collision){
for(var c : ContactPoint in coll.contacts){
if(coll.gameObject.name == "Y-")
Debug.Log("name " + coll.gameObject.name);
}
}
thx i will try this when I get home from work and mark it as answered if it does the trick.
if my above answer does not work I will try this after work. it my be a few days before I can mark as answered because of work but if its the right one I will mark it. thx
This did not work for me. $$anonymous$$aybe because of my set up.
Answer by Seth-Bergman · Nov 13, 2012 at 05:17 AM
HERE is my final answer:
first off, to see exactly what collision message is being sent, just try this first:
function OnCollisionEnter(other : Collision)
{
Debug.Log("HIT! :" + other.gameObject);
}
1: Using your setup, provided the collision is entered at all, you will see that the Debug log gives the name of the PARENT, because that is what has the RIGIDBODY.
2: No problem.. now try this:
function OnCollisionEnter(other : Collision)
{
Debug.Log("HIT! :" + other.gameObject);
Debug.Log("HIT! Collider: " + other.collider.gameObject);
}
The second line will give you the name of the ACTUAL OBJECT WITH THE COLLIDER, the child itself.. This works, I just tested it.
If you are not getting any Debug.Log message at all as a result of this test script, you have some other aspect set up wrong of what "OnCollisionEnter" needs to work right.. in which case check out here:
http://unitygems.com/mistakes1/
http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
Thanks! other.collider.gameObject worked great. Now if I throw a spear/knife into a compound collider it becomes the child of the body part, NOT the parent rigidbody like it did before. Just what I wanted!
thank u other.collider.CompareTag() worked like a charm :D
Late, but this is exactly what I needed to make child weapon object actually deliver damage, not receive for the parent. Thank you!
Answer by NoGo86 · Nov 15, 2012 at 06:39 PM
you are right about the not having it set up right. I now have the Main empty with 4 cube objects, each of the cube's have six collision boxes that have rigidebodys with this ontriggerenter
function OnTriggerEnter (col : Collider)
{
if(col.transform.IsChildOf(transform.parent.parent))
{
//Debug.Log (col.gameObject.name);
}
else
{
Debug.Log (col.gameObject.name + col.gameObject.tag);
}
}
Your answer
Follow this Question
Related Questions
Getting a collision's bottom-most collider in a hierarchy 2 Answers
Ammo crate collision 2 Answers
Detect a gameObject's tag via OnCollisionEnter() 5 Answers
Collision with no contact? 2 Answers
Collision with tag 5 Answers