- Home /
Question by
itscoolbro · Sep 28, 2013 at 01:44 PM ·
cameramousepositionscreentoworldpoint
screenToWorld not working
My camera is rotated 90 deg around the X axis. (Rotation : 90,0,0) and is at (Pos 0,10000,0);
When I run the game, the log stays on : 0, 10000, 0.
If I use a new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0) as parameter for the screenToWorldPoint function, the log updates but the coordonates are not right.
public Vector3 mousePos;
void Update(){
mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Debug.Log (mousePos);
}
Please help.
Comment
Answer by robertbu · Sep 28, 2013 at 01:45 PM
The 'Z' parameter of the position needs to be the distance in front of the camera. You are mapping a 2D coordinate onto a 3D space, so the distance is necessary to figure out a position. So if you wanted a something 10 units in front of the camera:
mousePos = Input.mousePosition;
mousePos.z = 10;
mousePos = Camera.main.ScreenToWorldPoint (mousePos);