- Home /
Desperate need for help with Trigger/Collider functions.
Hi Unity,
I'm having a progress-stopping problem with my simple combat system. I'm trying to make it so that if a projectile hits a tag, it explodes and the transform is disabled. Later, an IEnumerator takes care of the destruction, but here's the code (See below for explanation of the issue):
if(Physics.Raycast(myTransform.position,myTransform.up, out hit, range) &&
expended == false)
{
//If the collider has the tag of Floor then..
if(hit.transform.tag == "Floor")
{
expended = true;
//Instantiate an explosion effect.
Instantiate(blasterExplosion, hit.point, Quaternion.identity);
//Make the projectile become invisible.
myTransform.renderer.enabled = false;
//Turn off the light. The halo will also disappear.
myTransform.light.enabled = false;
//Turn off the particle emission
myTransform.particleEmitter.emit = false;
}
if(hit.transform.tag == "BlueTeamTrigger" ||
hit.transform.tag == "RedTeamTrigger")
{
expended = true;
//Instantiate an explosion effect.
Instantiate(blasterExplosion, hit.point, Quaternion.identity);
//Make the projectile become invisible.
myTransform.renderer.enabled = false;
//Turn off the light. The halo will also disappear.
myTransform.light.enabled = false;
The problem is, the projectile ignores the collider "RedTeamTrigger" and "BlueTeamTrigger" completely, as if it wasn't there. However, it explodes and disables properly when it hits the "Floor" tag. What am I doing wrong here? I've checked the documentation and even completely rebuilt the game to make sure this was the real issue.
Thanks!
Addyarb
So I figured out what my problem may be, as when I create a cube with the same tag it does collide properly. The object (called "Trigger" and with the tag "Red/BlueTeamTrigger") is a child object of the player motor, which has a rigidbody attached. From some of the documentation I can asses that sometimes a child of a rigidbody doesn't collide with triggers? Can anyone elaborate and/or offer a better solution?
Answer by Addyarb · Apr 28, 2014 at 09:38 AM
For those curious, I figured out the answer here: http://answers.unity3d.com/questions/241844/child-objects-trigger-collider-not-working-when-pa.html
I used hit.transform.collider.tag instead of hit.transform.tag. hit.transform.tag seems to ignore the fact that there's a collider if the parent has a rigidbody attached.