check if point is inside a collider

Hi,

is there a way to Check if a point (Vector3) is hitting any collider and maybe get back one or all collider its in?

i need this for a raycast. my problem with raycasts is that they dont get a hit if they are starting inside a collider. i need to know if the starting point is inside a collider and if not cast the ray.

anyone got an idea for that?

Can you explain the context?

check out this:

1 Like

the “Bounds.Contains” is not working in this case. i dont know the collider, only the point.

The Context is that i have a 2D space shooter game. there is no collision for the ships themselfes so it is possible for a laser gun to be inside the sphere collider of an enemy ship. the problem is that when make the raycast from the gun to a short distance i hit the enemy ships hull and not the shield.

Have you looked at Collision Layers?

1 Like

Look CheckSphere and OverlapSphere from docs: http://docs.unity3d.com/ScriptReference/Physics.html

Not sure if you are using 2d or 3d physics, so you might need to check 2d section from docs…

3 Likes

With sphere colliders it’s actually really easy. Just check if the distance between the laser turret and the enemy ship is less than the radius of the sphere collider.

This is an annoyance i have with unity’s colliders and physics. The don’t have many functions at your disposal to do certain things like this.

Sure, I can get the bounds of a collider and calculate if a point is in that. That’s fine. But that’s also not as accurate.

Sure I can overlap a couple geometries and get the colliders inside that area.

But like what OP wants, there isn’t much.

I actually wrote my own little ‘geom’ library of representation of geometry. I have a factory that can create the geom objects from colliders. And then with that I can calculate various things. Such as if the geometry contains a point.

This means you have to implement the test per geom type. Spheres are cake walk (distance between center of sphere and point is less than radius). Boxes a bit easier. Capsules not too complicated either. Complex colliders (a rigidbody with multiple colliders) is really just if any one of the colliders contains, so easy too. The mesh collider is the only hard one.

1 Like

Thanks for the help.

I Solved my problem with OverlapSphere. if you set the radius of OverlapSphere to Zero its realy just the origin point of the Sphere and a big Plus is that you get an array of all the Colliders at that point.

5 Likes

You can get the Vector3 location of the point and get the Vector3 location of the object and see if they are within 0.1 of each other? Or check if they are “point < 0.001”. So basically just checking how far each xyz cordinate in one vector is from the other xyz cordinate in the other vector.

As i said before this isnt possible if you dont know the other collider :wink:

Assuming you have convex colliders:
A way would be to cast a ray from the point P0 to the center of the collider.
IF the point P0 is inside the collider the hitpoint of the raycast would be further away from the center than P0.
ELSE the hitpoint on the collider must be between P0 and the collider center. This can be checked using squared distances.

1 Like

It’s good to help but please don’t necro such old threads (8 years old now) that are way beyond being relevant to those who posted.

1 Like