- Home /
Smooth 2D camera zoom
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?
Where are you running this code, if it is not in LateUpdate() then try it here and see if the result is the same.
Have you tried moving the camera ins$$anonymous$$d of changing its size?
No this is 2d game and size it's ortographic I can move it kilometer from objects t hg eye will always be the same size
Answer by SarperS · Nov 09, 2014 at 11:16 AM
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.
Answer by Tap2Continue · Dec 20, 2014 at 11:22 PM
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);
}
}
Idk how the duck u fixed my code somehow but thanks man :)
Your answer
Follow this Question
Related Questions
Camera scripting references 0 Answers
Problems with second Camera zoom and display 1 Answer
Sniper Scope with zoom 1 Answer
How to centre a 3D pinch zoom 1 Answer