Math - calculate position in world space from ray on infinite plane

Hey everyone,

I'm currently using the Unity Plane object to create an infinite plane, and to cast several rays against it. Like so:


Plane.Raycast returns

1) whether it has hit the plane or not, and

2) the distance, in case it has a hit

So I cast a ray from my camera, and it hits my plane. However, with just the distance information I cannot calculate where in 3D space each ray hits.

What mathematical calculations can I use in order to calculate the 3D intersectionpoints of each ray?

Basically if you cast a ray you normally have a starting point and a direction vector. if you have the distance you hit something just add the normalized direction vector multiplied by the distance to your starting point. That's it. But the Ray class in Unity have a special function that will do that for you: Ray.GetPoint()

   Ray myRay = new Ray(startpoint, direction); // or use any other ray
   float dist;
   if (myPlane.Raycast(myRay,out dist))
      Vector3 hitPoint = myRay.GetPoint(dist);

Use `Plane.Raycast()` then `ray.GetPoint()`

var ray : Ray;
var dist : float;

if( Plane.Raycast(ray, out dist) ) {
     var hitPoint : Vector3 = ray.GetPoint(dist);