3

I have a script that controls an orthographic camera panning movement in Unity, by using middle mouse button. An empty GameObject (e.g. the name is MainCamPivot (please see the picture)) at the center of the world (0,0,0) acts as the parent of the camera. The camera itself has local euler angles of Vector3(45f, 225f, 0f). I can pan/ move the camera nicely in X and Y axes, with this script (attached to the camera):

public float panSpeed = 0.05f;

private Vector3 lastMousePos;

void Start ()
{
    lastCamPos = transform.position;
}

void Update()
{
   if(Input.GetMouseButtonDown(2))
   {
      lastMousePos = Input.mousePosition;
   }
   if(Input.GetMouseButton(2))
   {
      Vector3 touchDeltaPosition = Input.mousePosition - lastMousePos;
      transform.Translate(-touchDeltaPosition.x * panSpeed, -touchDeltaPosition.y * panSpeed, 0);
      lastMousePos = Input.mousePosition;
   }

   // To limit the orthographic camera panning movement:
   if(transform.localPosition.x < 10f) transform.localPosition = new Vector3(10f, transform.localPosition.y, 16.3f);
   if(transform.localPosition.x > 23f) transform.localPosition = new Vector3(23f, transform.localPosition.y, 16.3f);
   if(transform.localPosition.y < 15f) transform.localPosition = new Vector3(transform.localPosition.x, 15f, 16.3f);
   if(transform.localPosition.y > 28f) transform.localPosition = new Vector3(transform.localPosition.x, 28f, 16.3f);
}

To limit the camera panning movement, I set some limits for the X and Y positions of the camera. The problem is the Z position value of the camera always changes, so setting a fixed position for the Z position of the camera will make the camera pans/ moves diagonally like the picture below:

Wrong camera movement

What is the best method to limit an orthographic camera panning movement properly, with scripting? (like the picture below)

Camera panning movement should follow mouse movement

2
  • Remove my answer, I realised you were already playing with local position.
    – Everts
    Dec 22, 2015 at 8:50
  • Have you solved this? Try using two pivots, one that has the script and another to reflect the z change. I did something like this for one of my projects.
    – Rishi
    Oct 7, 2016 at 15:56

1 Answer 1

0

How about instead of using pivots, set the position of the camera based on the difference in mouse position while the player is holding down on the button. What you are trying to do above is move the camera relative to the blocky object. Instead, you should move the camera relative to itself.

Get the mouse position when the user clicks down on the button and in LateUpdate(), while the player still holds the button down, call:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour {

    private Vector3 InitialClickPosition;
    private bool Pan = false;
    private const int MOUSE_BUTTON = 0;

    private void LateUpdate()
    {
        if (Input.GetMouseButtonDown(MOUSE_BUTTON))
        {
            InitialClickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else if (Input.GetMouseButton(MOUSE_BUTTON))
        {
            var difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.transform.position;
            Camera.main.transform.position = InitialClickPosition - difference;
        }
    }
}

That works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.