[Solved] Move camera with mouse dragging (2D)

Hi,

I want to create a map for my 3D scene. So I have placed a camera in the sky looking down. When M is pressed, I’m switching from the character controller camera to this camera. Now I want to move this camera in up, down, left and right directions with mouse’s movement and change height of camera with scrolling middle mouse button.
I have this script

var camera1 : Camera; 
var camera2 : Camera; 

function FixedUpdate() 
{

	if (Input.GetKeyDown (KeyCode.M))
	{
	    GameObject.Find("First Person Controller").GetComponent("SmoothMouseLook").enabled = false;
		GameObject.Find("First Person Controller").GetComponent("FPSWalker").enabled = false;
	    
	    camera1.enabled=true;//2D camera
	    camera2.enabled = false;//first person camera
		
		moveDirection = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
		camera1.transform.position += moveDirection;
	}
}

But 2d camera (camera1) doesn’t move when I drag the mouse. What’s the problem?

Note this is not a good way to approach dragging with the mouse. Drag and drop code of various sorts have been posted to Unity Answers a number of times, so I suggest you seek out those answers as a starting point.

As for your code, you are using Input.GetKeyDown(). GetKeyDown() fires for only a single frame, so your code above will only move the camera if you happen to be moving the mouse at the very same time the key goes down, and it will only move it once per key press.

As other have mentioned, execute this code in Update(). In particular you don’t want to exectue GetKeyDown() in FixedUpdate() since the difference in frame rates between Update() and FixedUpdate() can cause you to lose key presses.

Note you are using GetAxis() return values to move world objects. There is no match between the two coordinate systems.