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

Question How can I smoothly interpolate between different height and radius values?

Discussion in 'Cinemachine' started by imaginereadingthis, Sep 22, 2022.

  1. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Hello everyone,

    I'm currently developing a prototype where the player becomes bigger (the physical scale increases) after a certain event occurs. I'm using a third-person freelook camera, and I've also made it so that the height and radius of each rig become larger with the player, making a sort of "zoom out" effect. The problem is that this change happens instantly and not smoothly. This is what my code looks like:

    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update()
    3.     {
    4.         CalculateForward();
    5.         RotatePlayerToInput();
    6.         AdjustCameraDistance();
    7.     }
    And this is the function responsible for scaling the heights and radii of each rig:
    Code (CSharp):
    1. void AdjustCameraDistance() {
    2.         if (!radiusUpdated) {
    3.             for (int i = 0; i < 3; i++) {
    4.                 // When the player gets bigger, multiply the heights and radii of the freelook cam.
    5.                 // This will make a "zoom out" effect.
    6.                 freelook.m_Orbits[i].m_Height *= cameraDistanceMultiplier;
    7.                 freelook.m_Orbits[i].m_Radius *= cameraDistanceMultiplier;
    8.             }
    9.             radiusUpdated = true;
    10.         }
    11.     }
    I tried using Mathf.Lerp when assigning the new height and radius values, but it didn't have any effect. For example,
    Code (CSharp):
    1. freelook.m_Orbits[i].m_Height = Mathf.Lerp(freelook.m_Orbits[i].m_Height, freelook.m_Orbits[i].m_Height * cameraDistanceMultiplier, smoothTime * Time.deltaTime);
    This didn't do anything.
    Do you guys have any ideas on how I can get this to work?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You're not using Lerp correctly. The last parameter must be from 0...1. Try using SmoothDamp instead. Note that you must call this over multiple frames, until the target radius is reached.
     
    imaginereadingthis likes this.
  3. imaginereadingthis

    imaginereadingthis

    Joined:
    Jul 25, 2020
    Posts:
    97
    Yep, I asked a similar question about lerping an object's scale in another thread and successfully got it to work using a coroutine. Thank you!