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!

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.