- Home /
 
Camera zoom smoothing
So I have a rather basic camera setup and I would like to implement zoom smoothing in it but I'm not exactly sure how. I believe Mathf.Lerp should be used but my attempts to implement it have failed so far. Here is the code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraControl : MonoBehaviour
 {
     public Transform target;
     public Vector3 offset;
     public float currentZoom = 2f;
     public float pitch = 1.75f;
     public float zoomSpeed = 0.85f;
     public float minZoom = 1.5f;
     public float maxZoom = 3f;
 
     public float yawSpeed = 100f;
 
     private float currentYaw = 0f;
 
     private void Update()
     {
         currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
         currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
         
         if (Input.GetButton("Mouse MiddleClick"))
         {
             currentYaw += Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
         }
         
     }
     private void LateUpdate()
     {
         transform.position = target.position - offset * currentZoom;
         transform.LookAt(target.position + Vector3.up * pitch);
 
         transform.RotateAround(target.position, Vector3.up, currentYaw);
     }
 }
 
 
              
               Comment
              
 
               
              After your RotateAround, you can try to move forward your camera, with something like that : Vector.Lerp(transform.position, transform.position + transform.forward * deltaBetweenCurrentZoomAndLastZoom, Time.deltaTime);
 
               Best Answer 
              
 
              Answer by Hellium · Mar 11, 2019 at 08:42 AM
 public class CameraControl : MonoBehaviour
  {
      public Transform target;
      public Vector3 offset;
      private float desiredZoom = 2f;
      private float currentZoom = 2f;
      public float pitch = 1.75f;
      public float zoomSpeed = 0.85f;
      public float zoomLerpSpeed = 0.85f;
      public float minZoom = 1.5f;
      public float maxZoom = 3f;
  
      public float yawSpeed = 100f;
  
      private float currentYaw = 0f;
  
      private void Update()
      {
          desiredZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
          desiredZoom = Mathf.Clamp(desiredZoom, minZoom, maxZoom);
          currentZoom = Mathf.Lerp(currentZoom, desiredZoom, zoomLerpSpeed);
          
          if (Input.GetButton("Mouse MiddleClick"))
          {
              currentYaw += Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
          }
          
      }
      private void LateUpdate()
      {
          transform.position = target.position - offset * currentZoom;
          transform.LookAt(target.position + Vector3.up * pitch);
  
          transform.RotateAround(target.position, Vector3.up, currentYaw);
      }
  }
 
              Your answer
 
             Follow this Question
Related Questions
Camera not slowly moving to object 0 Answers
How to smooth my camera (Vector3.Lerp) 0 Answers
Problems with Lerp. 1 Answer
Mouse wheel zoom 1 Answer