- Home /
Child Collider Question Cant seem to get it right
So, I have read many things on using colliders on Children Objects and yet I can't seem to wrap my head around it. My last attempt was with a gameObject which was a Circular/Sphere model and on the outside it has cubes for lights. I want the player's bullets to collide with the cubes and decrement their health so that when they are all destroyed the parent object is destroyed. One attempt was on the Parent GameObject:
var light1 : Collider; var light2 : Collider;
function OnCollisionEnter(info : Collision){ Debug.Log(info.gameObject.tag); }
This returns my Bullet tag and switch the information is all the Bullet information not the Lights. So, my thought was ok I'll just check in the Bullets script:
function OnCollisionEnter(info : Collision){
Debug.Log(info.collider.name);
}
Now at first this appeared to work, however only on the last Collider. So with two it was saying the Collider Light2 was hit when I am shooting Collider Light1. If I remove the second collider it says Light1 and if I have 3 then Light3 collider is always hit.
Eventually it will do
function OnCollisionEnter(info : Collision){ if(info.collider.name == "Light1" && info.gameObject.tag == "Bullet"){ light1health = light1health--; if(light1health == 0){ Destroy(Light1);} }
if(info.collider.name == "Light2" && info.gameObject.tag == "Bullet"){ light2health = light2health--; if(light2health == 0){ Destroy(Light2);} }
function Update(){ if(light1health == 0 && light2health == 0) { Destroy(Sphere); } } }
Of course for the Destroy I will have to specify GameOjects and the like, but I just can't seem to get the effect I need. My Bullet has a rigidbody and the sphere has a rigidbody. If the info.collider.name is incorrect forgive me, but I am going off memory cause the code isn't here, but the Debug did output the info needed. And yes I see the last code probably won't work like that, but I thought it might better show exactly what I am going for. Any ideas? May instead of the info.collider.name == "Light1" it would be getCompentinChildren() or something, but it's like I am dancing around the answer and just need pointed in the right direction.
An alternative is putting empty gameObjects with the colliders updating to the exact position/rotation of where the lights are, but that seems really more work than what needs to happen.
Answer by Adam 10 · Feb 24, 2011 at 04:45 PM
Ok, I had read this before and I guess I just didn't use it right the first time.
function OnCollisionEnter(collisionInfo : Collision)
{
if(collisionInfo.gameObject.tag == "Bullet"){
for(ContactPoints c in collisionInfo.contacts){
if(c.thisCollider.name == "MyColliderName"){
lightHealth--;
}
}
}
}
Again I am going off memory, but I will correct this when I get home in case anyone else sees this.