- Home /
How to calculate the distance between specific side of an object and the closest to it surface's edge
I have some object which is located on the surface of free shape. Which is the best way to get the distance between A and B points? Let's take A1-B1 as world X axis and A2-B2 as world Z axis.
I want to know how much (by N meters) I can move the object by X axis (for example) until it reaches the edge of the surface.
See the picture below:
I guess I need to compare object’s and surface’s bounds somehow, but I have no idea how to get the farthest point from the center of each in the specific direction.
Another idea is to do raycasting from the object towards the surface edge, but it's flat, so there is no "wall" to get the hit.
Thank you in advance for the help!
Answer by The-Peaceful · Apr 16, 2021 at 07:02 AM
If you have a freeform surface, checking it's bounds wouldn't be enough, because the bounds are like a cube which includes the whole gameobject, meaning it would be inaccurate in some cases. The easiest way would probably be giving the surface mesh a meshcollider and raycasting down towards the surface step by step untill the raycast doesn't hit the surface anymore.
Thank you for the answer. I had this idea too, however I'll need to do raycasting every frame, so, I'm afraid of low performance... :(
Your answer
Follow this Question
Related Questions
How to get the center point of an object without any renderers? 2 Answers
How can I Instantiate a prefab (projectile) consistently from the character? 0 Answers
Moving object with transform.position ignore other objects even if they collided 1 Answer
2D RPG boomerang instantiation help. 2 Answers
Transform position changes multiplies value by 7.61 1 Answer