Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weird Zoom In Issue

Discussion in 'Cinemachine' started by UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396, Dec 31, 2019.

  1. UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    UDN_5c806b49-d8a0-4f67-a296-c12c91aa7396

    Joined:
    Jan 9, 2017
    Posts:
    151
    I'm having a weird zooming in issue with my vcam controller script. I'm using a Virtual Camera.

    Normal zooming in (near to far distance zoom) works fine.
    https://imgur.com/HyR1wo1

    Zooming in with camera tilting is making the camera very jerky while it is rotating. Why is this happening?
    https://imgur.com/CHNrZLP

    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3.  
    4. public class VCamController : MonoBehaviour
    5. {
    6.     // Important stuff here.
    7.     public CinemachineVirtualCamera vcam;
    8.  
    9.     // Variables used in calculations to make the script logic work.
    10.     //private float defaultZoom = 20f;
    11.     private float zoom = 20f;
    12.     private float zoomChangeAmount = 50f;
    13.  
    14.     private float tilt = 70f;
    15.     private float tiltChangeAmount = 100f;
    16.     public bool camTilt = false;
    17.  
    18.     private void Update()
    19.     {
    20.         ScrollZoom();
    21.     }
    22.  
    23.     void ScrollZoom()
    24.     {
    25.         if (Input.mouseScrollDelta.y > 0)
    26.         {
    27.             zoom -= zoomChangeAmount * Time.deltaTime;
    28.             zoom = Mathf.Clamp(zoom, 10f, 30f);
    29.             vcam.GetCinemachineComponent<CinemachineFramingTransposer>().m_CameraDistance = zoom;
    30.  
    31.             if (camTilt == true)
    32.             {
    33.                 tilt -= tiltChangeAmount * Time.deltaTime;
    34.                 tilt = Mathf.Clamp(tilt, 40f, 80f);
    35.                 vcam.transform.rotation = Quaternion.Euler(tilt, vcam.transform.rotation.y, vcam.transform.rotation.z);
    36.             }
    37.         }
    38.         if (Input.mouseScrollDelta.y < 0)
    39.         {
    40.             zoom += zoomChangeAmount * Time.deltaTime;
    41.             zoom = Mathf.Clamp(zoom, 10f, 30f);
    42.             vcam.GetCinemachineComponent<CinemachineFramingTransposer>().m_CameraDistance = zoom;
    43.  
    44.             if (camTilt == true)
    45.             {
    46.                 tilt += tiltChangeAmount * Time.deltaTime;
    47.                 tilt = Mathf.Clamp(tilt, 40f, 80f);
    48.                 vcam.transform.rotation = Quaternion.Euler(tilt, vcam.transform.rotation.y, vcam.transform.rotation.z);
    49.             }
    50.         }
    51.     }
    52. }
    53.  
     
  2. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    Fair warning: I'm the Product Manager, not tech support nor one of the engineers (most folks are still on holiday). I think I've got a decent answer for you, but take it with an appropriate grain of salt.

    I think the issue here is that you're trying to do in code what Cinemachine already does for you for free. I've set up an analogous scene.

    https://imgur.com/a/9CFyhMZ

    You'll notice that I'm using two virtual cameras instead of one. (
    CM upper
    represents my zoomed out, looking down position,
    CM lower
    represents my lower, looking forward position. I'm using a Cinemachine Mixing Camera to blend between them. With that done, the only code I need is something to adjust the weights of the two cameras.

    Code (CSharp):
    1. using UnityEngine;
    2. using Cinemachine;
    3. public class MixingCamController : MonoBehaviour
    4. {
    5.     // Important stuff here.
    6.     public CinemachineMixingCamera mixCam;
    7.     // Variables used in calculations to make the script logic work.
    8.     [Range(0,1f)]
    9.     private float zoom = .5f;
    10.     public float zoomChangeAmount = 1f;
    11.     private void Update()
    12.     {
    13.         ScrollZoom();
    14.     }
    15.     void ScrollZoom()
    16.     {
    17.         if (Input.mouseScrollDelta.y > 0)
    18.         {
    19.             zoom -= zoomChangeAmount * Time.deltaTime;
    20.         }
    21.         if (Input.mouseScrollDelta.y < 0)
    22.         {
    23.             zoom += zoomChangeAmount * Time.deltaTime;
    24.         }
    25.  
    26.         mixCam.SetWeight(0, zoom);
    27.         mixCam.SetWeight(1, 1f - zoom);
    28.     }
    29. }
    Note that Virtual Cameras are just that: virtual. Think of them like bookmarks. Adding an extra vcam is negligible in terms of performance impact and greatly simplifies this problem.

    Hope that helps.
     
    Last edited: Jan 3, 2020
    Gregoryl likes this.