Unity Quickies: Zooming, Dragging, and Panning the Camera in 3D Space

Here is a script I wrote which you can apply to your existing 3D camera in order to perform various functions such as zooming, dragging, or panning the main camera. It worked best for me in a chess game I was writing while learning Unity, in which I had to display a chess board to the user and allow him to move around the board inside of the game.

using UnityEngine;

public class Scene : MonoBehaviour
{

    Vector3 lastDragPosition, lastPanPosition;

    void Update()
    {
        UpdateZoom();
        UpdateDrag();
        UpdatePan();
    }

    void UpdateZoom()
    {
        var isMoving = Mathf.Abs(Input.mouseScrollDelta.y) > 0;
        if (!isMoving)
            return;
        var isZoomingIn = Input.mouseScrollDelta.y > 0;
        transform.position = transform.position +
                             transform.forward.normalized * Time.deltaTime * (isZoomingIn ? 15f : -15f);
    }

    void UpdateDrag()
    {
        if (Input.GetMouseButtonDown(2))
            lastDragPosition = Input.mousePosition;
        if (!Input.GetMouseButton(2))
            return;
        var delta = lastDragPosition - Input.mousePosition;
        transform.Translate(delta * Time.deltaTime * 0.5f);
        lastDragPosition = Input.mousePosition;
    }

    void UpdatePan()
    {
        if (Input.GetMouseButtonDown(1))
            lastPanPosition = Input.mousePosition;
        if (!Input.GetMouseButton(1))
            return;
        var delta = lastPanPosition - Input.mousePosition;
        transform.RotateAround(transform.position, Vector3.up, delta.normalized.x * Time.deltaTime * 25f);
        transform.RotateAround(transform.position, transform.right, -delta.normalized.y * Time.deltaTime * 25f);
        lastPanPosition = Input.mousePosition;
    }
}

Alexandru

"To avoid criticism, say nothing, do nothing, be nothing." - Aristotle

"It is wise to direct your anger towards problems - not people; to focus your energies on answers - not excuses." - William Arthur Ward

"Science does not know its debt to imagination." - Ralph Waldo Emerson

"Money was never a big motivation for me, except as a way to keep score. The real excitement is playing the game." - Donald Trump

"All our dreams can come true, if we have the courage to pursue them." - Walt Disney

"Mitch flashes back to a basketball game held in the Brandeis University gymnasium in 1979. The team is doing well and chants, 'We're number one!' Morrie stands and shouts, 'What's wrong with being number two?' The students fall silent." - Tuesdays with Morrie

I'm not entirely sure what makes me successful in general programming or development, but to any newcomers to this blood-sport, my best guess would be that success in programming comes from some strange combination of interest, persistence, patience, instincts (for example, someone might tell you that something can't be done, or that it can't be done a certain way, but you just know that can't be true, or you look at a piece of code and know something doesn't seem right with it at first glance, but you can't quite put your finger on it until you think it through some more), fearlessness of tinkering, and an ability to take advice because you should be humble. Its okay to be wrong or to have a bad approach, realize it, and try to find a better one, and even better to be wrong and find a better approach to solve something than to have had a bad approach to begin with. I hope that whatever fragments of information I sprinkle across here help those who hit the same roadblocks.

Leave a Reply

Your email address will not be published. Required fields are marked *