Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question C#: How to add dampening to Pan, zoom, orbit camera?

Discussion in 'Scripting' started by BES_MROE, Sep 27, 2022.



    Jul 27, 2022

    I'm very new to Unity and C# and I'm trying to create a camera that can pan, zoom, and orbit around the scene.

    - I managed to get all the functions working with the script below, but only the zoom is nicely dampened. The pan and orbit are very abrupt...

    - How can I add dampening to the pan and orbit part of the script?

    Thanks in advance!

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

    public class MainCam : MonoBehaviour
    public Transform target;

    public Vector3 targetOffset;
    public float distance = 5.0f;
    public float orbit = 0f;

    public float maxDistance = 200f;
    public float minDistance = 100f;

    public float xSpeed = 200.0f;
    public float ySpeed = 200.0f;

    public int yMinLimit = -80;
    public int yMaxLimit = 80;

    public int zoomRate = 40;

    public float panSpeed = 5f;

    public float MouseSensitivity = 4f;
    public float zoomDampening = 5.0f;
    public float ScrollSensitvity = 2f;

    private float xDeg = 0.0f;
    private float yDeg = 0.0f;
    private float currentDistance;
    private float desiredDistance;

    void Start()
    Vector3 angles = transform.eulerAngles;
    xDeg = angles.x;
    yDeg = angles.y;

    currentDistance = distance;
    desiredDistance = distance;

    // Camera logic on LateUpdate to only update after all character movement logic has been handled.

    void LateUpdate()
    // Don't do anything if target is not defined
    if (!target)

    // Left mouse down ORBIT
    else if (Input.GetMouseButton(0))
    // xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    // yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

    xDeg += Input.GetAxis("Mouse X") * MouseSensitivity;
    yDeg -= Input.GetAxis("Mouse Y") * MouseSensitivity;

    // Middle mouse down PAN
    else if (Input.GetMouseButton(2))
    //grab the rotation of the camera
    target.rotation = transform.rotation;
    target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
    target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);

    // affect the desired Zoom distance if we roll the scrollwheel
    desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);

    //clamp the zoom min/max
    desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);

    //Clamp the vertical axis for the orbit
    yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);

    // set camera rotation
    Quaternion rotation = Quaternion.Euler(yDeg, xDeg, 0);

    // For smoothing of the zoom, lerp distance
    currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);

    // keep within legal limits
    currentDistance = Mathf.Clamp(currentDistance, minDistance, maxDistance);

    // calculate position based on the new currentDistance
    Vector3 position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);

    transform.rotation = rotation;
    transform.position = position;

    private static float ClampAngle(float angle, float min, float max)
    if (angle < -360)
    angle += 360;
    if (angle > 360)
    angle -= 360;
    return Mathf.Clamp(angle, min, max);
  2. Kurt-Dekker


    Mar 16, 2013
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum:

    If you insist on wrangling your own camera code, you'll need to learn how to use code tags:

    and how to report your problem productively in the Unity3D forums:

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)
    thefallengamesstudio likes this.
  3. thefallengamesstudio


    Mar 7, 2016
    Never use camera manual manipulation if you're doing anything even slightly more complicated than simple translation. Even then Cinemachine should be the go-to solution. I discovered it very recently and was blown away by how many dev hours I realized it could've spared me from, should I've discovered it sooner (9y dev here)
    Kurt-Dekker likes this.