Smooth Camera Zoom with Mouse wheel

Hi

Iv been looking around for a smooth zoom script for my orbit script but i cant find any.

This is what i have now:

var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

var smoothTime = 0.3;

private var xSmooth = 0.0;
private var ySmooth = 0.0;
private var xVelocity = 0.0;
private var yVelocity = 0.0;

private var posSmooth = Vector3.zero;
private var posVelocity = Vector3.zero;

@script AddComponentMenu(“Camera-Control/Mouse Orbit smoothed”)

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

function LateUpdate () {
if (target) {
x += Input.GetAxis(“Mouse X”) * xSpeed * 0.02;
y -= Input.GetAxis(“Mouse Y”) * ySpeed * 0.02;

xSmooth = Mathf.SmoothDamp(xSmooth, x, xVelocity, smoothTime);
ySmooth = Mathf.SmoothDamp(ySmooth, y, yVelocity, smoothTime);

ySmooth = ClampAngle(ySmooth, yMinLimit, yMaxLimit);

var rotation = Quaternion.Euler(ySmooth, xSmooth, 0);

// posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);

posSmooth = target.position; // no follow smoothing

transform.rotation = rotation;
transform.position = rotation * Vector3(0.0, 0.0, -distance) + posSmooth;
}
}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}

Please help point me in the right direction.

Smoothing is all in using current and desired correctly. This is a piece out of another script that I just put on the forum.

currentZoom=Mathf.Lerp(currentZoom, zoom, 0.05);

This is fairly easy to implement.

Bear in mind, of course, that the Lerp would have to use Time.deltaTime in the call if you want to have it be equally fast and smooth on all framerates.

Won’t currentZoom never actually reach zoom like that, in accordance with Zeno’s paradox? I always feel dirty lerping like that.

Hi just to give you an example what im after check this out… http://www.kolmich.at/kolmich/demo/kvf/omegacity/kvf_omegacity.html
Anyone have a modified orbit cam script with smooth move and zoom on mouse wheel?

    public float zoomStartDistance = 2;
    public float zoomSensitivity = 5;
    public float zoomSmooth = 15;
    public float zoomMin = 1.5f;
    public float zoomMax = 20;
    private float distance;
    private float currentDistance;

    void Start()
    {
         currentDistance = Mathf.Clamp(zoomStartDistance > zoomMin ? zoomMin : zoomStartDistance, zoomMin, zoomMax);
    }
    void LateUpdate()
    {
        // Smooth zoom
        currentDistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity;
        currentDistance = Mathf.Clamp(currentDistance, zoomMin, zoomMax);
        distance = Mathf.Lerp(distance, currentDistance, Time.deltaTime * zoomSmooth);
    }
1 Like