- Home /
Check if the center of an object is inside a trigger Collider?
Hi guys!
I would like to know if there is a way to check if the center of an object is inside a Trigger Collider.
I have a simili Weather System, where the temperature varies in different Weather Zones. These zones are trigger Colliders.
I want to be able to tell in which zone an Island Is, based on its center, because I don't want to have an Island included in different zones. Is there a way to check that? Or do I have to put a child empty object with a small collider in the middle of the parent object and check by that collider?
Thanks a lot!
Compare world positions if its equal its in center of collider
I don't want to see if the center of my object is at the center of the trigger, I want to see if the center of ly object is somewhere inside the trigger. If the center is close to the left border, I want to tell that it is inside the same way it would be at the right Border. It's juste to be sure that a single object is not inside 2 triggers next to each others at the same time.
Answer by yummy81 · Feb 12, 2018 at 03:49 PM
When it comes to 3D, I would use Physics.OverlapSphere. You can supply it with two parameters: position and radius. But the most important is that if you set the radius to 0.0f, you will get the point. And that's want you want. So, when the method detects that it is inside the collider (or colliders), it will simply return the array of colliders. At that moment, you only have to get the collider you are interested in from this array, but in your case the simplest solution would be to check only for the first element. I wrote the code for you. Just attach the script to your Island gameobject. The Island does not have to have Rigidbody or any sort of collider for Physics.OverlapSphere to work, but gameobjects against which you want to perform a check (weather zones) must have their own colliders. Here's the code:
public Collider[] colliders;
private void Update()
{
colliders = Physics.OverlapSphere(transform.position, 0.0f);
if (colliders.Length > 0)
{
Debug.Log(colliders[0].name);
}
else
{
Debug.Log("I'm outside the weather zone");
}
}
The same goes for 2D. Just change:
public Collider[] colliders;
to this:
public Collider2D[] colliders;
and replace this:
colliders = Physics.OverlapSphere(transform.position, 0.0f);
with this:
colliders = Physics2D.OverlapCircleAll(transform.position, 0.0f);
Still for 2D, you can also use Physics2D.OverlapCircle, which returns just one collider (the one you want) without all this mess with arrays. And there is Collider2D.OverlapPoint, which implementation requires delegates in your case.
Very interesting! It is in 3D indeed, and your answer seems to make sense. I've never used OverlapSphere before, so I'll check this out after university tonight to be sure it fits what I want, but thanks in advance!
Did not have much time to test it recently, but I finally just did, and it works perfectly. Thanks a lot, I'll add the OverlapSphere (and circle for 2D) to the usefull things I know.
Thanks!!