- Home /
Is mousePosition not equal object.transform.position?
I started a new clear project. One plane and one cube on it.
I try now to move the cube to any position where I click with my mouse.
function Update () {
if (Input.GetMouseButtonDown(0))
{
var cube1 = GameObject.Find("Cube1");
cube1.transform.position.x = Input.mousePosition.x;
cube1.transform.position.z = Input.mousePosition.z;
}
}
But I think I miss the link.
How can I establish the relation between the coordinates of the cube and the coordinate of my mouse? Instead to move the cube 1cm e.g. to the right when I clicked next to cube, the cube moved to whatever my screen size is. So in this case instead from x=0 to x=1 the cube moved to x=585.
Which function reads the correct distance or coordinates related to mouse position?
'Input.mousePosition' lives in screen coordinates...2D coordinates represented by the resolution of the screen. Game objects live in world coordinates. One way to convert between the two is to use Camera.ScreenToWorldPoint(). It is very important to set the 'Z' value correctly before making the call. Search for 'Unity mousePosition ScreenToWorldPoint' for some examples.
Input.mousePosition workswith Screen resolution and its coordinated.
Object can move in the background of the camera but the Input.mousePosition rendering in only camera.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105305im_/https://answers.unity.com/themes/thub/images/avi.jpg)