Orthographic Camera Panning

(I’m relatively new to Unity) I’m making a scene where I want the camera to move in the opposite direction of the mouse movement when I hold down the right mouse button. The scene is isotropic, which is why the camera is orthographic.

[SerializeField] private Camera cam;
private Vector3 dragOrigin = Vector3.zero;
public float speed = 10;
    
void Update(){
    if(Input.GetMouseButtonDown(1)){
        dragOrigin = cam.ScreenToWorldPoint(Input.mousePosition);
    }
    if(Input.GetMouseButton(1)){
        Vector3 difference = cam.ScreenToWorldPoint(Input.mousePosition) - cam.transform.position;
        Vector3 targetPosition = dragOrigin - difference;
        //targetPosition.y = cam.transform.position.y;
            
        cam.transform.position = Vector3.Lerp(cam.transform.position, targetPosition, speed * Time.deltaTime);
    }
}

This works, but there is the problem that the y-value of the camera changes as well. It should always stay at the same height. If I use targetPosition.y = cam.transform.position.y; the camera stays on the same y-position, but it moves slower when I move it up and down.

How can I rewrite the code so that only the x and z values change and that the camera moves with the same speed in all directions?

Thanks in advance.

The reason the y-value of your camera’s position changes comes from how work Camera.ScreenToWorldPoint. When passing Input.mousePosition as argument, this means that the depth used for converting point from screen space to world space is zero. As such, except if your camera-plane is parallel to the XZ (world coordinate) plane (that is, horizontal), then two different values of Input.mousePos will give world coordinates with different y-values (because all points in your camera near plane have different y-values).


A solution is to set the same y-value for both dragOrigin and difference. This value should be the same as the y-value yout want your camera to have. Doing so, your targetPosition should have the same y-value as your camera already has.