Move Camera once Mouse Drag is complete


Here is the script I currently have. The script is attached to the camera.

Right now the moving works, if I left click and drag the mouse upwards it moves upwards. But once I rotate my camera 180 degree (there is a script which manages rotation and zoom), the camera moves downwards when mouse is upwards. And in between 0 and 180 degrees the camera goes in different directions.

Is there anyway to make the camera act like the one within Unity’s Scene Editor? (with middle mouse button). Where if I click and drag downwards it moves upwards? But instead of moving X and Y axis move X and Z axis.

The TargetLookAt object is where the camera is focused on and the object itself doesn’t rotate. It should just move so the camera will follow it. (the following scripts are all done and working).

 private Vector3 mouseOrigin;
    private bool isPanning;
    void Update()
        if (Input.GetMouseButtonDown(0))
            mouseOrigin = Input.mousePosition;
            //right click was pressed    
            isPanning = true;

        // cancel on button release
        if (!Input.GetMouseButton(0))
          //  if(isPanning)
               // MoveTarget(mouseOrigin, Input.mousePosition);
            isPanning = false;

        if (isPanning)
            Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
            Vector3 move = new Vector3(pos.x * MoveSpeed, 0, pos.y * MoveSpeed);

            TargetLookAt.transform.Translate(move, Space.World);

Well the solution was easy. By just rotating the TargetLookAt to the rotation of camera, the movements become aligned.

   TargetLookAt.transform.rotation = Quaternion.Euler(0, (Camera.main.transform.eulerAngles.y),      0);

this script on a camera would move kinda like the unity editor camera. hopefully you can adjust from here:

	float mx;
	float my;
	Vector3 v;
	void Start () {
	// Update is called once per frame
	void Update () {
		mx = Input.GetAxis ("Mouse X");	
		my = Input.GetAxis ("Mouse Y");
		if (Input.GetMouseButton(2)) {
						v = new Vector3 (mx, 0, my);
						transform.position = transform.position + v * .5f;
		if (Input.GetMouseButton(1)) {
						v = new Vector3 (my, mx, 0);
						transform.eulerAngles = transform.eulerAngles + v;

As an alternative, you could also replace the final line of the script with :

TargetLookAt.transform.Translate(move, Camera.main.transform);

This way, the translation is always relative to the camera local coordinates system.