Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Lerping different values at the same time

Discussion in 'Scripting' started by Rangerz132, Sep 15, 2023.

  1. Rangerz132

    Rangerz132

    Joined:
    Feb 3, 2019
    Posts:
    38
    Hello!

    My problem is as follows :

    I would like my camera to zoom and rotate according to the mouseScrollDelta. However, the min/max zoom variables and the min/max angle variable are not the same values. At the moment, when I scroll with my mouse, the angle is reaching the min or max values faster then the zoom value because it is not the same threshold. How can I map the angle value to match the zoom threshold?

    Here's my code :


    Code (CSharp):
    1. private void HandleZoom()
    2.     {
    3.         // Define zoom value based on the mouse scroll delta
    4.         if (Input.mouseScrollDelta.y > 0)
    5.         {
    6.             zoom -= Time.deltaTime * damping;
    7.             angle -= Time.deltaTime * damping;
    8.  
    9.  
    10.         }
    11.         else if (Input.mouseScrollDelta.y < 0)
    12.         {
    13.             zoom += Time.deltaTime * damping;
    14.             angle += Time.deltaTime * damping;
    15.         }
    16.  
    17.         // Set zoom boundaries
    18.         if (zoom <= minZoom)
    19.         {
    20.             zoom = minZoom;
    21.         }
    22.         else if (zoom >= maxZoom)
    23.         {
    24.             zoom = maxZoom;
    25.         }
    26.  
    27.         // Set angle boundaries
    28.         if (angle <= minZoomAngle)
    29.         {
    30.             angle = minZoomAngle;
    31.         }
    32.         else if (angle >= maxZoomAngle)
    33.         {
    34.             angle = maxZoomAngle;
    35.         }
    36.  
    37.         cinemachineVirtual.m_Lens.FieldOfView = Mathf.Lerp(cinemachineVirtual.m_Lens.FieldOfView, zoom, Time.deltaTime * smoothSpeed);
    38.         transform.eulerAngles = new Vector3(Mathf.Lerp(transform.eulerAngles.x, angle, Time.deltaTime * smoothSpeed), 0, 0);
    39.     }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Have a variable, lets call it 't'. You move that value based on the mouseScrollDelta and damping between 0 and 1 (your damping value is going to probably need to be tweaked to feel good).

    Code (csharp):
    1. t = Mathf.Clamp01(t - System.Math.Sign(Input.mouseScrollDelta.y) * Time.deltaTime * damping);
    note - I simplified your if statement using Clamp and System.Math.Sign which returns 1 for positive, -1 for negative, and 0 for 0. This is different than Mathf.Sign where returns 1 for both positive and 0.

    From this t you can calculate your zoom and angle.

    Code (csharp):
    1. zoom = Mathf.Lerp(minZoom, maxZoom, t);
    2. angle = Mathf.Lerp(minZoomAngle, maxZoomAngle, t);
    And then you just set your fieldofview and rotation the same way you currently are.
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,527
    This wouldn't work because
    Input.mouseScrollDelta
    is already a frame-delta value which most of the time is 0. You only get an output when the mousewheel did an actual "jump" up or down. So you should construct a "targetT" value where you simply add the mouse delta and have "t" be smoothed towards targetT.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I just recycled their existing logic.

    They adjusted zoom and angle if mouseScrollDelta was non-zero by Time.deltaTime * damping. This does precisely that as well.

    With large enough values of damping, and the fact they smooth it out with the lerp at the end, you'd effectively get the same thing you're describing. You're creating a target 't', and the fieldofview and rotation smooths out towards it in the last lines.

    ...

    Just tested:


    Looks exactly like I expected. Not pretty... but basically what OPs original code did just with angle/fov tethered.

    Code (csharp):
    1.     public class zTest01 : SPComponent
    2.     {
    3.  
    4.         public float minZoom = 30f;
    5.         public float maxZoom = 90f;
    6.         public float minZoomAngle = 0f;
    7.         public float maxZoomAngle = 45f;
    8.         public float damping = 10f;
    9.         public float smoothSpeed = 1f;
    10.  
    11.         public float t = 0.5f;
    12.         public float zoom;
    13.         public float angle;
    14.  
    15.         private void Update()
    16.         {
    17.             t = Mathf.Clamp01(t - System.Math.Sign(Input.mouseScrollDelta.y) * Time.deltaTime * damping);
    18.             zoom = Mathf.Lerp(minZoom, maxZoom, t);
    19.             angle = Mathf.Lerp(minZoomAngle, maxZoomAngle, t);
    20.  
    21.             this.GetComponent<Camera>().fieldOfView = Mathf.Lerp(this.GetComponent<Camera>().fieldOfView, zoom, Time.deltaTime * smoothSpeed);
    22.             transform.eulerAngles = new Vector3(Mathf.Lerp(transform.eulerAngles.x, angle, Time.deltaTime * smoothSpeed), 0, 0);
    23.         }
    24.  
    25.     }
    I even copied their original code in and yep, it looked the same... if only that the angle and zoom were untethered from one another (so I could get it zoomed in but angle away) and I had to crank the 'damping' up to like 500 since the value range is much larger than 0->1.

    If OP wants a different approach though with values that are more predictable as to what they're doing (currently the behviour is reliant completely on framerate meaning different machines will feel distinctly different due to performance). OP can ask for that. But completely rewriting their code to some completely unrelated wouldn't help them learn what had to be done to their own code to make it work. And I just slapped in a little extra that shows how you can simplify their if statements using clamp and sign.
     
    Last edited: Sep 15, 2023
    Rangerz132 likes this.
  5. Rangerz132

    Rangerz132

    Joined:
    Feb 3, 2019
    Posts:
    38