Getting the Furthest Point on a Ray

Hello everyone, I’m trying to find a way of directly getting the furthest point along a Ray.

What I’m doing:

-I’m making a raycast with a max distance, checking to make sure a hit.point is far enough away from the origin, then instantiating a prefab within the ray distance points (randomized min/max use of ray.GetPoint). I’ve seen that if there is no “hit” the hit.point value will remain as it was, and I can set it to some obviously fake value and check against that (therefore max distance must have been achieved if no object blocked it).

This works okay, but I’d rather just find the furthest point along the ray if possible (and I’ve seen that GetPoint() will return any value along the ray’s direction, so it isn’t reliable to check against).

Any ideas are appreciated, and if its not possible or does not make sense, telling me that works to. Thanks.

Your question is a little unclear…

  • The furthest point along a Ray is determined by the distance property specified in its constructor.

  • Or, do you mean the point at which the furthest collision occurs? You could do that with RayCastAll, sorting the results and picking the furthest hit point based on Vector3.Distance.

  • Or, do you mean the furthest the ray travels before it hits something? For that just use regular RayCast, and check the Vector3.Distance of the hit.point (if no collider was hit, RayCast will return false).