- Home /
ScreenPointtoRay Problem, how does it work!?!
Hello,
I think I'm confused about how ScreenPointoRay works. I have one script giving me the position of my mouse through Screen Point to Ray and then a debug Script.
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10000f, Color.black);
if (_gamePlane.Raycast(ray, out distance))
return ray.GetPoint(distance);
I thought the above code and below code should produce the same ray. But they don't, the second code snippet produces a line that does not freely follow the mouse. Is the "Point" in screen to ray at the center of the screen or something? The black line freely follows the mouse but the red one stays generally in the middle of the screen.
Gizmos.DrawLine(Camera.main.transform.position, Camera.main.ScreenPointToRay(Input.mousePosition).direction);
The red line is providing some kind of interference with my script that causes a game object to copy the location of the Gamecursor. So I'm trying to figure out why these two rays are different and how to get the red line to act like the black line. The video I linked may be able to explain the phenomenon better than my awkward explanation!
Thanks >>so much<< in advance!
Answer by Hellium · Sep 24, 2018 at 04:47 AM
Simply because DrawLine
function needs two positions in space, however, you are giving it a position and a direction.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 start = Camera.main.transform.position;
Vector3 end = ray.GetPoint(10000f);
// OR
// Vector3 end = start + ray.direction * 10000;
Gizmos.DrawLine( start, end );
Ahhh, I see, I was getting the syntax all wrong when i tried to get rid of the direction before, thanks for the info!