ScreenToWorldPoint not working

I’m making a really simple 2D game in Unity 2018.1.
My camera is in Perspective mode.
Here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		Debug.Log(Input.mousePosition);
		Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition));
	}
}

The value of Input.mousePosition is normal, but Camera.main.ScreenToWorldPoint(Input.mousePosition) is always (0.0 , 0.0 , -10.0).
What’s the problem?

Your problem is a combination of the following:

  • you use a perspective camera
  • You pass 0 as z distance to ScreenToWorldPoint

Since a perspective camera has perspective, all rays actually meet at the origin of the camera. Since you pass 0 as distance form the camera origin it will always return the camera origin.

You need to specify a distance.

Vector3 pos = Input.mousePosition;
pos.z = 10;
Debug.Log("World point: " + Camera.main.ScreenToWorldPoint( pos ));