Wrymnn
1
Have 2D Top Down game.
I need some Smooth Ortographic Camera zoom in it.
So far this is not working, it`s pretty fast and not smooth.
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, camera.orthographicSize + Input.GetAxis("Mouse ScrollWheel") * 100, Time.deltaTime * zoomSpeed);
Any Ideas please?
Here’s an awesome script. It works perfectly
using UnityEngine;
using System.Collections;
public class Zoom : MonoBehaviour {
public float zoomSpeed = 1;
public float targetOrtho;
public float smoothSpeed = 2.0f;
public float minOrtho = 1.0f;
public float maxOrtho = 20.0f;
void Start() {
targetOrtho = Camera.main.orthographicSize;
}
void Update () {
float scroll = Input.GetAxis ("Mouse ScrollWheel");
if (scroll != 0.0f) {
targetOrtho -= scroll * zoomSpeed;
targetOrtho = Mathf.Clamp (targetOrtho, minOrtho, maxOrtho);
}
Camera.main.orthographicSize = Mathf.MoveTowards (Camera.main.orthographicSize, targetOrtho, smoothSpeed * Time.deltaTime);
}
}
SarperS
2
It is fast because the usage of the Lerp method is wrong. For the third parameter your argument is
Time.deltaTime * zoomSpeed
The way the lerp works is, if this parameter is 0 it returns your first argument camera.orthographicSize. If it’s 1 it return your second argument. The values in-between interpolate between your both arguments. Hope it makes sense. So what you want to do is, map the zoom value between 0 and 1, modify that value with the scroll wheel and then use that as an argument for the lerp. Let me know if it doesn’t make any sense and I can provide a code example in my free time.