- Home /
Plane.Raycast() out distance incorrect?
I have a feeling I'm missing something obvious (or not so obvious,) but I've written the following code to find the point on a plane that runs through 0,0,0 perpendicular to the Y axis. A dummy prefab is instantiated at the point and an object instantiated at the same point and attached to the dummy.
void spawnBrick ( int brickNumber ) {
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
float distToPlane = 0.0f;
Plane myPlane = new Plane( Vector3.up, Vector3.zero );
myPlane.Raycast( ray, out distToPlane );
print( distToPlane );
Transform host = Instantiate( selectionHost, Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, distToPlane ) ), Quaternion.identity ) as Transform;
Transform brick = Instantiate( filterBrickList[brickNumber] as Transform, host.position, Quaternion.identity ) as Transform;
host.SendMessage( "AddBrickToSelection", brick );
}
Basically, when I instantiate the host Transform, it is consistently 0.1745186 units above the plane.
I'm wondering if I'm misunderstanding the Plane constructor? The camera is an ortho camera-- near to far plane is only 192 units, the distance tends to be around 84 units (based on a debug report)-- plus, like I said, the vertical offset is consistent... so I'm pretty sure it isn't a precision error.
What gives?? Can anyone see an issue in my code or recognize some undocumented caveat that would result in this offset?
Let me know if you need any clarifications.
Answer by Sinaz20 · Apr 25, 2012 at 08:17 PM
Turns out, distance is correct-- the error lay in using the ScreenToWorldPoint(). Instead, I used the Ray.GetPoint() and saw predicted results.
I'm getting really good at answering my own questions!
Your answer
Follow this Question
Related Questions
Math - calculate position in world space from ray on infinite plane 2 Answers
How to get the distance between two objects in feet/meter? 1 Answer
raycast hit to relative location on collider? 1 Answer
Parenting Bullet Holes? 1 Answer
SOLVED - Raycast to detect GameObject having the shortest hit distance 1 Answer