lerping camera field of view not quite working for me

Hi,

I want to lerp the players cameras field of view when he accelerates with a booster and have it come back slowly to 60.

The first line of code is how I lerp from the value 60 to 62.7 which is supposed to happen faster than when it comes back from 62.7f to 60 (line two is in a coroutine and is activated after 1 second of the initial lerp) But the problem is that the lerping happens almost instantly on both, it’s as though the Mathf.Lerp is obsolete or not there at all, does anyone know why or how to control the speed of the lerp better?

camera.fieldOfView = Mathf.Lerp(60f, 62.7, Time.deltaTime * 2 );


camera.fieldOfView = Mathf.Lerp(62.7f, 60, Time.deltaTime );

This should not really do what you want it to do. The third argument is the progress between 0 and 1 (0 means returning argument one, 1 means returning argument two, every other value is linear interpolation between the two). Instead you will need a variable that holds the current progress and is increased by Time.deltaTime and use that as the third variable.

Check out this post, it also has cool types of progress:
How to Lerp like a pro | Chico Unity3D (wordpress.com)

This fits my standard “smoothly move between quantities” pattern.

Smoothing movement between any two particular values:

https://discussions.unity.com/t/812925/5

You have currentQuantity and desiredQuantity.

  • only set desiredQuantity
  • the code always moves currentQuantity towards desiredQuantity
  • read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4

Thank you for your help but it’s the same result as my line of code. The movement is instant for some reason, it doesnt change based on the value of “MovementPerSecond”.

    void AcceptUserInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            desiredQuantity = 62.7f;
            StartCoroutine(camBack());
        }
    
    }
    IEnumerator camBack()
    {

        yield return new WaitForSeconds(1f);

        desiredQuantity = 60;
    }

    void ProcessMovement()
    {
      
        currentQuantity = Mathf.MoveTowards(currentQuantity,desiredQuantity, MovementPerSecond * Time.deltaTime);

    }
    private void Update()
    {
     
            AcceptUserInput();

            ProcessMovement();
            camera.fieldOfView = desiredQuantity;
}

camera.fieldOfView = currentQuantity is what you want. Desired is the target, so that is why it is instant and not over time.

1 Like

So you want to have a kinda visual boost effect by increasing / decreasing the Camera FOV with a smooth transition between the two states.
My humble advice is that the use of a Coroutine could be a bit complicate since you dont know the time that will take to run from 60.0f to 62.7f … Or you may be have to calculate it…

Use instead a simple boolean that says : I have to increase / decrease the FOV. And use Mathf.Approximately() in the increase mode to know if you have reached 62.7f so you can switch to the decrease mode.

In the he vid below OnTriggerEnter() is used to “PingPong” the FOV. But i think it can work with other events.