Search Unity

Smoothly change float when adding another float

Discussion in 'Scripting' started by SomerenV, Dec 23, 2018.

  1. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    I want to change the distance from the player car when the car speeds up and slows down. I got it working but the transition between positions goes kinda fast. This is the current code for changing the distance:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (CarSpeed.zoomOut == true)
    4.         {
    5.             height = startheight + addheight;
    6.         }
    7.         else
    8.         {
    9.             height = startheight;
    10.         }
    11.     }
    How to I smoothly change those numbers? So height is startheight + addheight but the change needs to be gradual.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    For that you should use Lerp function.
    See
    Mathf.Lerp doc
    https://docs.unity3d.com/ScriptReference/Mathf.Lerp.html
    Is relatively nice explained.

    So in you r case for floats
    Code (CSharp):
    1. changeSpeed = 0.5f ; // play with this value. Typically 0 to 1.0f But you can check beyond ( 0 to 10.0f ).
    2. height = Mathf.Lerp ( height, startheight + addheight, Time.deltaTime * changeSpeed ) ;
    Just to note, you probably wan to keep it outside CarSpeed.zoomOut == true loop. But you will need test it, specific for your case.
     
  3. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Shouldn't MoveTowards be best suited for this ?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    To be honest, don't know. Never used it.
    Someone else is probably better, to answer this question.
     
  5. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    I'm gonna try this solution in a minute. Thanks for answering though! I already looked at Mathf.Lerp but found it a bit too confusing. Any reason why I should keep it outside CarSpeed.zoomOut?

    Yeah, that could be used too but the camera position is relative to the car so it doesn't have it's own defined position. I use a distance and height parameter to define where the camera needs to be positioned relative to the car.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    The reason I mentioned that, As I don't know, if is one of shot, or you keep true value, for multiple iterations.
    If second, then shouldn't be an issue. But pretty same result I think you can achieve if you keep outside of that, if loop.
    This is only problem specific. So either situation may work. Try both.

    Regarding lerp (example for transform.position as Vector3), is simply
    Code (CSharp):
    1. currentPosition = Lerp ( currentPosition, desiredPosition, howFastGetToDesiredPosition ) ;
    Code (CSharp):
    1. this.transfrom.position = Lerp ( this.transfrom.position, targetPosition, howFastGetToDesiredPosition ) ;
    Or as I posted earlier (for float), regarding your height application. Hope that makes sense :p
     
  7. SomerenV

    SomerenV

    Joined:
    Dec 20, 2011
    Posts:
    83
    I've changed the script a bit. It now changes the FOV and the lookat offset to the vehicle. Works like a charm with the code you provided. Also, thanks for the explanation on how Lerp works :)

    Happy holidays!
     
    Antypodish likes this.
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,770
    Thx, all best.