- Home /
Click and Drag Camera only works in Orthographic camera
I was working on a click and drag script to move the Camera around. It works perfectly with an Orthographic Camera, but when I try to change it back to a Perspective Camera, it doesn't work at all and I don't understand why.
Here's my script attached to the camera:
function Update () {
if ( Input.GetMouseButtonDown(0)){
dragOrigin = Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
dragOrigin = camera.ScreenToWorldPoint(dragOrigin);
}
if ( Input.GetMouseButton(0)){
var currentPos : Vector3 = Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
currentPos = camera.ScreenToWorldPoint(currentPos);
var movePos : Vector3 = dragOrigin - currentPos;
transform.position = transform.position + movePos;
}
}
Answer by Wolfram · Jun 25, 2012 at 04:18 PM
Just read the docs. For a perspective cam, you must provide a z distance for Camera.ScreenToWorldPoint, at which you want to place your object.
I didn't see anything in the Script Reference that said a Perspective Camera worked differently than an Orthographic Camera for ScreenToWorldPoint. But I'll play around with that. Thanks.
Well, it isn't explicitly mentioned, but for an orthographic camera, the distance is irrelevant for object size/pixel placement (it's only relevant for visibility/render order due to the zBuffer).
What is mentioned is that the third parameter deter$$anonymous$$es the distance from the camera. So if you set this to 0, you will simply place the object at the camera position.