Pan and Rotate controls with mouse

I am working on an orthographic game, and I have not gotten to the controls because I have not been sure how to do them. Most the tutorials and help I find online, involve using touch controls. I want to be able to rotate the camera in a hemisphere by holding the middle wheel button like most 3D software use (except Unity for some odd reason), and pan around using left click dragging. I have made the guess that it would be best to place the script on an empty gameobject, and have the camera in that. Then have the script move the empty gameobject around to move the camera with it. With C#, I have had no success with any of the attempts I made (I trashed them to remove the waste of clutter).

You can use Transform.RotateAround for the orbit effect.

For the panning you could probably do something like

var mouseX = Input.GetAxis("MouseX");
var mouseY = Input.GetAxis("MouseY");
var sensitivity = 0.2f;

var cameraPos = transform.position;
cameraPos += transform.right * mouseX * sensitivity;
cameraPos += transform.up * mouseY * sensitivity;
transform.position = cameraPos;

I haven’t tested this code though.

With a tad bit of work to that (since it was written in Javascript, and panning was going in wrong direction), I was able to get panning to work. Though I am not sure how I can get orbiting to work. I was able to get Input.MouseButtonDown(2)/Middle Mouse button to run the “rotate around”. But I am not able to figure out how to get mouseX/mouseY to tell the camera to rotatearound in both directions.

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    private Manager manager;

    //===Panning===
    float mouseX;
    float mouseY;
    float sensitivity;
    Vector3 cameraPos;

    void Start () {
        if (GameObject.Find ("Manager") != null) {
            manager = GameObject.Find ("Manager").GetComponent<Manager>() ;
        } else {
            Debug.Log ("ERROR: Manager NOT FOUND");
        }

        //===Panning===

        sensitivity = 0.5f;
        cameraPos = transform.position;


    }
   

    void Update () {
        if (manager.gameActive) {
            Panning ();
            Zoom ();
            Rotate ();
        }
    }

    public void Rotate(){
        if (Input.GetMouseButton (2)) {
            transform.RotateAround (Vector3.zero, Vector3.up , 20 * Time.deltaTime);
        }
    }

    public void Panning(){
        if (Input.GetMouseButton (0)) {
            mouseX = Input.GetAxis ("MouseX");
            mouseY = Input.GetAxis ("MouseY");
            cameraPos += transform.right * (mouseX * -1) * sensitivity;
            cameraPos += transform.up * (mouseY * -1) * sensitivity;
            transform.position = cameraPos;
        }
    }

    public void Zoom(){
        if (Input.GetKeyDown (KeyCode.Keypad1) || Input.GetKeyDown (KeyCode.Alpha1)) {
            Camera.main.orthographicSize = 7;
        } else if (Input.GetKeyDown (KeyCode.Keypad2) || Input.GetKeyDown (KeyCode.Alpha2)) {
            Camera.main.orthographicSize = 17;
        } else if (Input.GetKeyDown (KeyCode.Keypad3) || Input.GetKeyDown (KeyCode.Alpha3)) {
            Camera.main.orthographicSize = 27;
        }
    }
}
2 Likes

The way I am setting up my game, I would really like to use an orbiting camera. I know I should put it onto an empty gameobject, and use the controls on that, but I just cannot seem to figure out how. RotateAround is not a function that will be useful, and I kind of suck at euler.angles.

This image from my game so far show how things will end up covering other things (which is why I want orbiting, despite using an orthographic camera) http://tinypic.com/r/dgtr1u/9