n4m31
February 14, 2013, 2:15am
1
i have create gameObject with position Vector3(0,0,-1)
and Camera with position Vector3(0,0,-10)
but i can not drag my Object properly

```
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -1))
this.gameObject.transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
```

With ScreenToWorldPoint, the Z parameter is a distance in front of the camera. -1 does not make sense. Because of perspective, no fixed number will work here. Try using the distance between the camera and the object.

If you change the camera euler to Euler(90,0,0), the Y parameter will become distance between camera and object.

```
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, (gameObject.transform.position.y - Camera.main.transform.position.y)))
this.gameObject.transform.position = new Vector3(mousePos.x, mousePos.y, mousePos.z);
```

and you should calculate the distance first (if the distance will never change) in this case -1-(-10) = 9 for better performance.

```
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 9);
```