Search Unity

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

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

  1. BES_MROE

    BES_MROE

    Joined:
    Jul 27, 2022
    Posts:
    1
    Hi!

    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;

    //DAMPENING
    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)
    return;

    // 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

    Kurt-Dekker

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

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    If you insist on wrangling your own camera code, you'll need to learn how to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

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

    http://plbm.com/?p=220

    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!!!)
     
    xucian likes this.
  3. xucian

    xucian

    Joined:
    Mar 7, 2016
    Posts:
    846
    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.