- Home /
World mouse position not depending on Screen mouse position
I made a very simple script showing both the Screen and World coordinates of the mouse in the inspector :
var mousePos : Vector3; var worldPos : Vector3;
function Update () { mousePos = Input.mousePosition; worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); }
However, worldPos does not depend on the actual position of the mouse at all. It's always just above my character's head (what looks like the center of the camera). The script above is attached to an empty GameObject with no parents/children. I also hid the cursor to replace it with a GUI Label.
Thanks for any help.
Answer by Mike 3 · May 26, 2010 at 08:04 PM
That function will always return the position of the camera when the vector you pass in has a z value of 0
The function basically gives you the world position depending on what distance from the camera you ask for - if you want to know what world point your mouse is selecting, you either need to decide a distance, or use raycasting instead
Answer by Eric5h5 · May 26, 2010 at 08:03 PM
You need to define some kind of Z position to get useful data from ScreenToWorldPoint. Try this:
mousePos = Input.mousePosition;
mousePos.z = 1.0;
worldPos = Camera.main.ScreenToWorldPoint(mousePos);
Your answer
Follow this Question
Related Questions
Mouse.position from center of player 1 Answer
how to make the object position related to the screen 2 Answers
click on screen to get the coordinate on the ground(x,z plane) 1 Answer
Move to mouse position 1 Answer
How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers