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.
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.
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.
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.
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.
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.