0
\$\begingroup\$

I have multiple SphereColliders attached to GameObject, one bigger to detect collision danger zone and the other, smaller, for actual collision (both triggers!). When the OnTriggerEnter() how do I find which one of those two caused trigger enter event?

private void OnTriggerEnter(Collider other)
{
    foreach (var collider in GetComponents<SphereCollider>())
    {
        if(isColliding(collider)) //how to find if this particular trigger isColliding ?
        {
            if(collider.radius > 0.5)//smaller or bigger one
               GetComponent<MeshRenderer>().material.color = _dangerColor;
            else handleCollision();
        }
    }

}

Because the accepted answer provides two solutions, it would be in order to specify how I solved the problem. In the end I went for keeping outter collider and doing "manual" overlap test for inner one (since I did not need physics response, it was the lesser evil). It also turned into new feature, it allows me to lerp danger color.

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

As far as I know, there is no good way to accomplish this when having two triggers on the same GameObject. You could do sphere overlap tests, but then you would just be duplicating what the physics engine has already done!

What I normally do is put each trigger on a separate GameObject (perhaps one parent, one child) each with a script with its own OnTriggerEnter. Using that, one script could handle your danger zone, and the other could handle your collision event.

\$\endgroup\$
1
  • \$\begingroup\$ Creating child object is one of options I considered - but like you said, it is duplicating existing functionality which is not the best from design point of view. \$\endgroup\$
    – wondra
    Commented Apr 26, 2017 at 14:02
1
\$\begingroup\$

As detecting the danger zone and handling the actual collisions is something different, I think you should have two gameobjects, one with the big collider and a DetectDangerZone script, another gameobject with the smaller collider which handles your collision.

\$\endgroup\$
2
  • \$\begingroup\$ One of them should be on a child object. Which one is a matter of preference. \$\endgroup\$ Commented Apr 26, 2017 at 15:03
  • \$\begingroup\$ This option more general, and probably better for most cases. In the end, however, I went for simpler sphere overlaps calculation(I did not need physics collision response). It also turned into new feature - now I can lerp danger color. \$\endgroup\$
    – wondra
    Commented Apr 26, 2017 at 15:21
0
\$\begingroup\$

You could check the distance between the object and the object colliding with it, in this way you could have multiple checks to see if the object has passed certain distances.

//Distances are equal to the radii of the colliders they replace
float hitZone = 1f;
float closeZone = 2f;
float dangerZone = 3f;

private void OnTriggerEnter(Collider other)
{
    float distance = Vector3.Distance(transform.position,other.position);
    if(distance < dangerZone){
        //Object is in the danger zone
    }
    else if (distance < closeZone){
        //Object is in the close zone
    }
    else if (distance < hitZone){
        //Object is in the hit zone
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .