- Home /
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?
Sorry just read the Plane.Raycast :D. wait, i'll add an example
Answer by Bunny83 · Jan 25, 2011 at 11:13 PM
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);
}
That doesn't work, that is RaycastHit which is information extracted from a normal Raycast. I cannot use normal Raycasts since they only can be done on meshcolliders. $$anonymous$$y infinite plane is no mesh, it's a theoretical plane. Check Plane: http://unity3d.com/support/documentation/ScriptReference/Plane.html
Ah your answer has been editted :D So my last reply doesn't fit the bill. I will try this tomorrow Bunny. Using the Ray object was new to me, hope it'll work :) Thanks for adding the math too, I want to understand that part too!
Answer by Peter G · Jan 25, 2011 at 11:26 PM
Use Plane.Raycast()
then ray.GetPoint()
var ray : Ray; var dist : float;
if( Plane.Raycast(ray, out dist) ) { var hitPoint : Vector3 = ray.GetPoint(dist); }
Thank you sir, although Bunny pointed me in the right direction, your help is much appreciated
Your answer
Follow this Question
Related Questions
Raycast refuses to cast down 1 Answer
Help with some C# 1 Answer
How can i tell the distance from a raycast? 1 Answer
Bounds IntersectRay distance isnt correct 1 Answer
Unreliability of Physics.Raycast.Distance for player movement? 0 Answers