The title sums up my question. How do I find a point along a raycast. I don’t mean the start or the end or even where it collides with an object. Say if the distance from the one object to another was 10. How can I find out what the position is halfway between those objects using a raycast. Thanks in advanced!
Basic maths is how you do it. What is a ray? It’s a line starting from a defined point, travelling in one direction. If you define your ray with start and end points, then this direction vector is just (end-start), which you then normalise to make it direction only.
To find the point at any distance, d, along a ray is very simple. Take the start point, S and the direction vector V, and the point you’re looking for is just:
S + Vd
As long as the direction vector is normalised (has magnitude 1). This is very basic vector maths, and if you’re doing anything at all with raycasts, I really strongly suggest you read into it more. You should have a thorough understanding of vector maths whatever you do. You’ll need it.
You can also get the point using the ray itself.
Ray r = new Ray( Vector3.zero, Vector3.forward + Vector3.up);
print(r.GetPoint(10)); //gets a point in space 10 meters along the ray
getting the distance required is another matter, one that looks like was solved 8 years ago, but im posting this here for posterity. You can raycast with a ray or with a raw position and direction. They’re basically the same.