Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Camera movement script in Unity3D. Supports rotating, panning, and zooming. Attach to the main camera. Tutorial explaining code: http://jibransyed.wordpress.com/2013/02/22/rotating-panning-and-zooming-a-camera-in-unity/
// Credit to damien_oconnell from http://forum.unity3d.com/threads/39513-Click-drag-camera-movement
// for using the mouse displacement for calculating the amount of camera movement and panning code.
using UnityEngine;
using System.Collections;
public class MoveCamera : MonoBehaviour
{
//
// VARIABLES
//
public float turnSpeed = 4.0f; // Speed of camera turning when mouse moves in along an axis
public float panSpeed = 4.0f; // Speed of the camera when being panned
public float zoomSpeed = 4.0f; // Speed of the camera going back and forth
private Vector3 mouseOrigin; // Position of cursor when mouse dragging starts
private bool isPanning; // Is the camera being panned?
private bool isRotating; // Is the camera being rotated?
private bool isZooming; // Is the camera zooming?
//
// UPDATE
//
void Update ()
{
// Get the left mouse button
if(Input.GetMouseButtonDown(0))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isRotating = true;
}
// Get the right mouse button
if(Input.GetMouseButtonDown(1))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isPanning = true;
}
// Get the middle mouse button
if(Input.GetMouseButtonDown(2))
{
// Get mouse origin
mouseOrigin = Input.mousePosition;
isZooming = true;
}
// Disable movements on button release
if (!Input.GetMouseButton(0)) isRotating=false;
if (!Input.GetMouseButton(1)) isPanning=false;
if (!Input.GetMouseButton(2)) isZooming=false;
// Rotate camera along X and Y axis
if (isRotating)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);
}
// Move the camera on it's XY plane
if (isPanning)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
transform.Translate(move, Space.Self);
}
// Move the camera linearly along Z axis
if (isZooming)
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);
Vector3 move = pos.y * zoomSpeed * transform.forward;
transform.Translate(move, Space.World);
}
}
}
@nabeelsaleem

This comment has been minimized.

Copy link

commented Jul 7, 2014

@2ksp

This comment has been minimized.

Copy link

commented Jul 9, 2014

Unity did not reconignize "transform" when i tried this code

@SanketRJDave

This comment has been minimized.

Copy link

commented May 7, 2015

Camera is not rotating up and down. please help!

@jBachalo

This comment has been minimized.

Copy link

commented Jun 15, 2015

2 things needed to improve this

  • a way to define a pivot point or center of rotation
  • an inertia toggle
@malonso11

This comment has been minimized.

Copy link

commented Apr 22, 2016

Thanks man, work very cool

@tomatohorse

This comment has been minimized.

Copy link

commented May 19, 2016

I'm a unity newb... can anyone tell me how to get this script to run each time I load Unity, so it's the default behavior? And thanks.

@illes63

This comment has been minimized.

Copy link

commented Jul 22, 2016

Thanks, very helpful. I´ll credit the source on my project.

@junwatu

This comment has been minimized.

Copy link

commented Apr 11, 2017

Thanks for the script.

@jlfeitosa3

This comment has been minimized.

Copy link

commented Jun 29, 2017

Hi, very useful script!, i include phisics colision in this.

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

[RequireComponent(typeof(CharacterController))]
public class KeyboardOrbit : MonoBehaviour
{
//
// VARIABLES
//

public float turnSpeed = 4.0f;      // Speed of camera turning when mouse moves in along an axis
public float panSpeed = 4.0f;       // Speed of the camera when being panned
public float zoomSpeed = 4.0f;      // Speed of the camera going back and forth

private Vector3 mouseOrigin;    // Position of cursor when mouse dragging starts
private bool isPanning;     // Is the camera being panned?
private bool isRotating;    // Is the camera being rotated?
private bool isZooming;     // Is the camera zooming?
private CharacterController controller;
private CollisionFlags flags;

//
// UPDATE
//

void Update()
{
    controller = GetComponent<CharacterController>();

    // Get the left mouse button
    if (Input.GetMouseButtonDown(0))
    {
        // Get mouse origin
        mouseOrigin = Input.mousePosition;
        isRotating = true;
    }

    // Get the right mouse button
    if (Input.GetMouseButtonDown(1))
    {
        // Get mouse origin
        mouseOrigin = Input.mousePosition;
        isPanning = true;
    }

    // Get the middle mouse button
    if (Input.GetMouseButtonDown(2))
    {
        // Get mouse origin
        mouseOrigin = Input.mousePosition;
        isZooming = true;
    }

    // Disable movements on button release
    if (!Input.GetMouseButton(0)) isRotating = false;
    if (!Input.GetMouseButton(1)) isPanning = false;
    if (!Input.GetMouseButton(2)) isZooming = false;

    // Rotate camera along X and Y axis
    if (isRotating)
    {
        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

        transform.RotateAround(transform.position, transform.right, -pos.y * turnSpeed);
        transform.RotateAround(transform.position, Vector3.up, pos.x * turnSpeed);

        flags = controller.Move(pos * Time.deltaTime);
    }

    // Move the camera on it's XY plane
    if (isPanning)
    {
        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

        Vector3 move = new Vector3(pos.x * panSpeed, pos.y * panSpeed, 0);
        transform.Translate(move, Space.Self);

        flags = controller.Move(move * Time.deltaTime);
    }

    // Move the camera linearly along Z axis
    if (isZooming)
    {
        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - mouseOrigin);

        Vector3 move = pos.y * zoomSpeed * transform.forward;
        transform.Translate(move, Space.World);

        flags = controller.Move(move * Time.deltaTime);
    }
}

}

@farzadhallaji

This comment has been minimized.

Copy link

commented Jul 7, 2017

thanks
its very very useful!!!

@jlfeitosa3 too

@q23345

This comment has been minimized.

Copy link

commented Jul 23, 2017

Thanks a lot for the inspiration, very useful to sort out the last few bugs in my camera code.

@maazirfan

This comment has been minimized.

Copy link

commented Sep 21, 2017

`public class swipebutton : MonoBehaviour
{
public Camera cam;

void Update()
{
   
    cam.transform.Rotate(Vector3.up, 20.0f * Time.deltaTime);
}

}`

@MNwAtThs

This comment has been minimized.

Copy link

commented Aug 10, 2018

Hello Guys is there a way to restrict the camera on the z axis if so I need help

@SaadAli2016

This comment has been minimized.

Copy link

commented Dec 15, 2018

not working

@eric2296

This comment has been minimized.

Copy link

commented Jan 13, 2019

Hi guys, when I launch the script and I try to zoom or rotate or pan the camera;I've the same error:
-System.NullReferenceException: Object reference not set to an instance of an object at MoveCamera.Update () [0x00193]
why?

@grayrhino

This comment has been minimized.

Copy link

commented Apr 30, 2019

eric2296, since the script is using Camera.main... you have to change your Main Camera tag to MainCamera on Inspector. That will fix the error

@fkiran

This comment has been minimized.

Copy link

commented May 8, 2019

Can We add a path so camera will move on the path when swipe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.