Calculate distance between mesh edges

I have an origin point readily accessible where my mouse is on the screen, for example

            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

      

Now imagine a cube. Wherever you click this cube, the line is taken from the edge that clicked on the object and stops at the other end. The orientation, vertical or horizontal, determines which side to click on, one of the four sides, top or bottom.

How do I determine the distance (from one edge of the grid to the other) and orientation (vertical or horizontal)?

Thoughts?

The only idea I have so far is to use collision detection and use CollisionEnter as the starting point and somehow draw a line that reaches the opposite end of the mesh and use CollisionExit to determine the destination (or exit) point. Then do some calculations to determine the distance between the Enter and Exit methods.

+3


source to share


1 answer


The only way I can think of approaching this is to throw the beam back in the other direction ....

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
    //offset the ray, keeping it along the XZ plane of the hit
    Vector3 offsetDirection = -hit.normal;
    offsetDirection.y = 0;
    //offset a long way, minimum thickness of the object
    ray.origin = hit.point  + offsetDirection * 100;
    //point the ray back at the first hit point
    ray.direction = (hit.point - ray.origin).normalized;
    //raycast all, because there might be other objects in the way
    RaycastHit[] hits = Physics.RaycastAll(ray);
    foreach (RaycastHit h in hits)
    {
        if (h.collider == hit.collider)
        {
            h.point; //this is the point you're interested in
        }
    }
}

      

This shifts the beam to a new location so that it retains the same XZ coordinates of the original hit, so the resulting endpoints form a line perpendicular to the world / scene Y axis. For this we use the direction of the camera Forward

(since we want to get a point further from the point of view). If we wanted to get a point for a line perpendicular to the hitting surface (parallel to the surface normal), we could create an offset using instead hit.normal

.



You probably want to put the layermask or maxdist parameter in the two raycast methods (so it checks fewer things and is faster), but that's up to you.

Source Code: which detects the two endpoints of a "single" ray passed through the object.

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
    //offset the ray along its own direction by A LOT
    //at a minimum, this would be the maximum thickness of any object we care about,
    //PLUS the distance away from the camera that it is
    ray.origin += ray.direction * 100;
    //reverse the direction of the ray so it points towards the camera
    ray.direction *= -1;
    //raycast all, because there might be other objects in the way
    RaycastHit[] hits = Physics.RaycastAll(ray);
    foreach(RaycastHit h in hits)
    {
        if(h.collider == hit.collider)
        {
            h.point; //this is the point you're interested in
        }
    }
}

      

+3


source







All Articles