- Home /
How to make a camera zoom in on a specific point
I have a 2D game with a camera that when the user clicks on an object the camera would move to that object (still dont have that working yet either) the camera would need to smoothly zoom using the orthographicsize method to the desired point. since it is 2D i noticed the camera's z access cant be 0 or else everything disappears but i cant make the orthographicsize change in a smooth fashion. i can make it move to the desired size but never zoom it just pops into place.
Please help
Answer by unity_ek98vnTRplGj8Q · Jan 30, 2020 at 11:53 PM
If you know the desired size, you can use a Lerp function (linear interpolation) to smoothly transition the size. Here's a script that will smoothly transition the camera zoom for you when you call ZoomCamera()
public float transitionSpeed = 0.3f;
private float desiredCameraSize;
private Camera cam;
void Start(){
cam = Camera.main;
desiredCameraSize = cam.orthographicSize;
}
void Update(){
cam.orthographicSize = Mathf.Lerp(Camera.main.orthographicSize, desiredCameraSize, transitionSpeed);
}
public void ZoomCamera(float size){
desiredCameraSize = size;
}
I tried to use this code and it didnt do anything. I changed the transition speed as well and still nothing happened.
I just tested it and it seems to work fine. You can try making the desiredCamSize public and playing with it in the inspector, and you can also try making the cam variable public and giving it the reference yourself as maybe the camera you are using isn't the main camera. If you do this remember to take out the line in the start function that sets the camera variable to the main camera. Also looking back I would multiply transitionSpeed by Time.deltaTime which is a good practice for any transition during Update()