#[SOLVED] Raycast hit.point is wrong

1 messages · Page 1 of 1 (latest)

floral skiff
#

Hey guys. I'm doing a raycast in a shape of the camera frustum, and trying to get the point where it intersects with the plane(ground). As you can see in the image, I got the rays working properly, but somehow the hit.point is returning a wrong position(the gizmos spheres represent the hit.point that it returns). The red lines are the rays. Any reason why this might be happening?? Any help would be appreciated

arctic fossil
#

Are you certain that the visualisation accurately represents the data?
How are you drawing the rays and the points, vs how are you doing the raycast itself

floral skiff
#
//FUNCTION DOING RAYCAST
//0 - topLeft. 1 - topRight, 2 - bottomLeft, 3 - bottomRight
    public Vector3[] GetFrustumCornersIntersectingGround(Camera cam, LayerMask groundLayer)
    {
        Vector3[] nearCorners = GetFrustumNearCorners(cam);
        Vector3[] farCorners = GetFrustumFarCorners(cam);
        Vector3[] intersectingPoints = new Vector3[4];
        for(int i = 0; i < intersectingPoints.Length; i++)
        {
            RaycastHit hit;
            if (Physics.Raycast(nearCorners[i], farCorners[i], out hit, Mathf.Infinity, groundLayer))
            {
                intersectingPoints[i] = hit.point;
            }
        }

        this.nearCorners = nearCorners;
        this.farCorners = farCorners;
        return intersectingPoints;
    }

//GIZMOS
private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;

        if(nearCorners!=null && nearCorners.Length >= 4)
        {
            for (int i = 0; i < nearCorners.Length; i++)
            {
                Gizmos.DrawLine(nearCorners[i], farCorners[i]);
            }
        }

        Vector3[] corners = GetFrustumCornersIntersectingGround(camera, groundLayer);
        for (int i = 0; i < corners.Length; i++)
        {
            Gizmos.DrawSphere(corners[i], 2f);
        }
    }
#

Here are the two functions doing the raycast and the gizmos
Both are done from the same points

devout thistle
#

You know, in my experience when something doesn't line up like you think it should alot of times its because of something stupid in the inspector.

#

is it parented to any other objects that could be warping its alignment?

floral skiff
#

I have fixed it 😃
Not sure what was the issue exactly, but as @arctic fossil said, the rays drawn by the gizmos are different from the actual raycasts.
Solution: I changed the raycasting method from:
if (Physics.Raycast(nearCorners[i], farCorners[i], out hit, Mathf.Infinity, groundLayer))
to

rays[i] = new Ray(nearCorners[i], farCorners[i] - nearCorners[i]);
RaycastHit hit;
if (Physics.Raycast(rays[i], out hit, Mathf.Infinity, groundLayer))
#

Thanks guys @devout thistle @arctic fossil

#

[SOLVED] Raycast hit.point is wrong

arctic fossil
#

You treated it as Physics.Raycast(start, end) when that's wrong

#

It's supposed to be Physics.Raycast(start, direction)