- Home /
Unity 2D How can I detect collision with a Vector3?
Title (some what). What I'm trying to achieve is to have a Vector3, that's randomly setup, check if it's position is inside a Tilemap Collider2D. I already have a way that the Vector3 is randomly generated, I just really need to figure out a way for that created position to know it's inside this specific collider so that it can keep generating until it's not in the collider.
Answer by xxmariofer · Apr 24, 2019 at 08:22 AM
the most common way of doing this is using the overlapsphere method
Collider[] hitColliders = Physics.OverlapSphere(center, radius);
the center will be the vector3, it must have a radius but you can create a really little one like 0.1f and if hitcolliders is null will mean no objects collided
Did a few modifications to that but the base you gave me is what I needed. Thank you.
Answer by Eyes-Wide-Shut · Apr 24, 2019 at 03:43 AM
Vector3 represents a position in 3D space, and has no context of collisions. You need to attach a collider for that.
There's multiple ways to achieve what you're asking, but most likely the easiest solution is to attach a collider to the gameobject that has the Vector3 and detect collisions.