ScreenToViewportPoint Bug?

I paste in the following lines from the example and attach this script to a game object.

function Update () {
		transform.position = Camera.main.ScreenToViewportPoint(Input.mousePosition);
	}

But when the game runs my object to which this script is attached is way behind the camera near to 0,0,0. It also does not seem to move or rotate with the camera but moves slightly as I move the camera viewpoint. I would expect the object to be drawn at the mouse position no?

Any idea?

?

The first problem is that you want ScreenToWorldPoint(), not ScreenToViewportPoint() (unless this is a GUITexture or GUIText).

Second issue: In dealing with ScreenToWoprldPoint(), the ā€˜zā€™ of the passed value is the distance in front of the camera. For a perspective camera, if the ā€˜zā€™ is 0, then ScreenToWorldPoint() will return the location of the camera. Say you wanted to place your object on a plane 10 units from the camera plane. You could do:

var pos = Input.mousePosition;
pos.z = 10.0;
transform.position = Camera.main.ScreenToWorldPoint(pos);