- Home /
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.
Answer by tanoshimi · Nov 24, 2014 at 06:46 AM
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).
@ tanoshimi:
-I'll try again with pictures, perhaps it will help:
Basically, when the raycast does hit something, I can just check the distance between the hit.point and the ray.origion.
However when no collision occurs, the only way I can check this is to see if hit.point is still some pre-defined value like (1000,1000,1000), a number that is not possible in normal gameplay. Therefore, I can safely use the max distance of the ray knowing that it will be a safe value.
Ideally I would just like to find the ter$$anonymous$$ation point of the ray, without needing another variable to check ins$$anonymous$$d.
It does work as is, and just may not be worth worrying about at this point...
endpoint = ray.origin + (ray.direction * maxRayLength);
@ agosevensixtytwo:
-Right, but when there is a collision, we are right be to where we started, since the ray is less than its max length...
A ray is mathematically defined by a start point and a direction. It means it has no end, it just keeps on going. Unity adds a new factor so that you can limit the length of the ray which should actually be called a segment since it no longer considers the mathematic requirements of a ray.
So either your ray goes on infinitely or it has been given a length, and that length is the end of the ray.
To get the end of the ray, if you got a hit:
Vector3 endOfRay = hit.point;
if you got no hit:
Vector3 endOfRay = ray.origin + ray.direction * length;
Vector3 endOfRay = ray.GetPoint(length);
or
indefine since infinite.
@boddole:
Raycast returns if you hit something or not. So like fafase said you usually have a setup like this:
Vector3 endPoint;
if (Physics.Raycast(yourRay, out hit, yourDistance))
endPoint = hit.point;
else
endPoint = yourRay.GetPoint(yourDistance);
Or if you don't use a Ray it would be:
Vector3 endPoint;
if (Physics.Raycast(yourOrigin, yourDirection, out hit, yourDistance))
endPoint = hit.point;
else
endPoint = yourOrigin + yourDirection.normalized*yourDistance;
Also like fafase said if you haven't specified a length for your raycast if's infinite so there is no endpoint unless you specify what should be your max distance.
Your answer
Follow this Question
Related Questions
C# Check Physics.Raycast Once 0 Answers
C# More Accurate or Larger Raycast 1 Answer
Raycast stops when false, even in update() 1 Answer