- Home /
How do I find the normal of the closest surface (in a specific radius)?
Hello! So in my first attempt I used multiple Physics.Raycast in various directions and the one that had the shortest hit.distance, I used it's hit.normal to get the normal of that surface. This method could work but it requires a lot of raycasts in every direction to get a spherical distance check of all the objects in radius and it feels like this wouldn't be a very optimized solution.
So my next idea was to use Physics.SphereCast to cast a spherical ray, with a radius and 0 maxDistance. This method felt like it should've done the job since it is basically a spherical raycast in every direction, but the major issue with this is that the SphereCast will not detect any hits if it is already inside of an object. This means that only when an object is further away than the radius of the SphereCast will it detect a hit.
So now I'm kinda stuck on where to go next. I have looked at Physics.OverlapSphere but there doesn't seem to exist an easy way of getting the normal of the closest hit position using this method. Thank you for your time!
EDIT: After some testing it seems that OverlapSphere and ClosestPoint are very performance heavy, so in my case it's probably better to just use raycasts in multiple directions.
Answer by kylecat6330 · Jul 04, 2020 at 03:12 PM
I'm not exactly sure what you are trying to do, so it is hard to say which is the best way to go about this, so I'll just go over doing it with OverlapSphere.
To use Physics.OverlapSphere here you'll need to use a for loop that'll check the distance of all colliders and stores the closest one.
if (Vector3.Distance(collider[i].transform.position, SphereCenter) < Vector3.Distance(nearestObjPoint, SphereCenter)
{
collider[i].transform.position = nearestObjPoint;
}
After the for loop you can take the nearest point and calculate the direction to it using Vector3 direction = (SphereCenter - nearestObjPoint).normalized
Then you can shoot a raycast at it to get the normal.
Answer by Jabbasmakt · Jul 04, 2020 at 07:34 PM
@kylecat6330 Thanks for your help! I managed to get it working but I don't think it will work for my case. This because I need to check how close I am to surfaces, such as the terrain and map geometry. This means that when the sphere collides with something on the terrain it returns the centre position of the entire terrain, meaning that the ray will be angled towards the middle of the terrain, which might be very far away, and so the normal will not be the one that is closest position on the surface to the sphere.
This hopefully explains the issue I get: https://i.imgur.com/eAuEVHr.png
(I'm working in 3D, just a 2D representation)
EDIT: I used ClosestPoint() to find the closest point on the collider surface, and apparently ClosestPoint() doesn't work unless you have convex MeshColliders.
Answer by kylecat6330 · Jul 04, 2020 at 08:10 PM
If you need the actual point of the collider which is closest to the collider then instead of using a for loop to check the nearest transform.position point use Collider.ClosestPoint, tho check for the closest point in each collider like this;
if (Vector3.Distance(collider[i].ClosestPoint(SphereCenter), SphereCenter) < Vector3.Distance(nearestPoint, SphereCenter)
{
collider[i].ClosestPoint(SphereCenter) = nearestPoint;
}
Then after that you'd need to calculate the direction to the nearest closest point of all the colliders with Vector3 direction = (SphereCenter - nearestPoint).normalized;
Once that is done you shoot a raycast at the point and you can get your normal from that.
Thank you again. So I used ClosestPoint() and it works as intended, only issue is that ClosestPoint() doesn't work unless you have convex mesh colliders, so I gotta have to figure out how to turn my terrain meshes into that ..
You could try a ClosestPointOnBounds, but I've never used anything with bounds and I'm not completely sure how they work. That's kind of all I've got in terms of OverlapSphere. You could also try a for loop with an array of directions which you can pass through a raycast, so that you can use a single raycast to check multiple direction. However, I'm not sure how much more efficient that is than just using multiple raycast.
Doesn't seem to work with ClosestPointOnBounds, but still a huge thank you for your help. I think I will just create all my terrain collision meshes with a certain thickness to allow the ClosestPoint script to work.
EDIT: This will also fix an issue were some fast falling objects can fall through the map.
EDIT2: ClosestPoint still doesn't work even if you have a collision mesh with a thickness. It needs to be completely convex, so that makes it unusable in my case ...
Your answer
Follow this Question
Related Questions
Obstacle Avoidance 0 Answers
Collision between sambe objects with same Layer 1 Answer
Jumping with raycasts causing issues? 2 Answers
RayCast to all directions 4 Answers