ScreenToWorldPoint() not working


I’m currently experimenting with ScreenToWorldPoint(), and something just doesn’t seem to be working right…

Whenever I use this code:

function Update() {

It just shows (0.0 ,125.0, 0.0) all the time in the Debug console, regardless of how much I move the cursor around the screen.

How can I make my code display the actual world coordinates my mouse is currently passing over?

Thanks in advance. :slight_smile:

ScreenToWorldPoint receives a Vector3 argument where x and y are the screen coordinates, and z is the distance from the camera. Since Input.mousePosition.z is always 0, what you’re getting is the camera position.

The mouse position in the 2D screen corresponds to a line in the 3D world passing through the camera center and the mouse pointer, thus you must somehow select which point in this line you’re interested in - that’s why you must pass the distance from the camera in z.

If you try something like this:

function Update() {
  var mousePos = Input.mousePosition;
  mousePos.z = 10; // select distance = 10 units from the camera

you will get the world point at 10 units from the camera.

You have to specify a Z position, like the code example in the docs does.