CMZTECH.NET
  • Blog
  • Graphics
  • Virtual Reality
  • 3D Printing
  • Fun Stuff
    • furiosus tangeret solum vitrum >
      • A new friend
      • Chorazin
      • Curious1
      • MRGAMERESOURCES
  • Contact

non-linear lerp in unity

3/30/2015

0 Comments

 
A LERP (linear interpolation) returns you a value based on start and end key values and a fraction of the interpolation. 
E.g:
  • When the fraction is 0 you are at the start
  • When the fraction is at 1 you are at the end
  • When the fraction is at 0.5 you are halfway


But if we replace the fraction with something non linear as long as its a function that returns a result in the range 0..1 we can get some more interesting results.   Trig curves like Sine are a good candidate and allow the creation of movement that has smooth acceleration and deceleration.

Picture
For my example I wanted to interpolate from key A to key B and then back to key A, so the part of the sin curve I should use goes from 0..PI.  So I take my input fraction, scale it by PI and then generate the Sin and I get a lovely smooth interpolation and back again.  If you simply wanted to go from Key A to Key B you would use 0..1/2PI.


//When something scary happens momentarily zoom the camera
float inter = (1.0f*fearCounter)/FEARCOUNTER;  //linear
inter*= 3.1415f; 
inter = Mathf.Sin(inter);
float fov = Mathf.Lerp(cameraFOV,cameraFOV/2.0f, inter);
localCamera.fieldOfView =fov;
0 Comments



Leave a Reply.

    David Coombes

    Making stuff

    Categories

    All
    3D Graphics
    3D Printing
    3D Scanning
    AI
    Blender
    Cycling
    Development
    Environment
    Futurism

    Archives

    February 2024
    January 2024
    November 2023
    September 2023
    August 2023
    February 2023
    November 2022
    April 2022
    February 2022
    January 2022
    February 2021
    January 2021
    November 2020
    May 2020
    April 2020
    March 2020
    February 2020
    December 2019
    November 2019
    September 2019
    August 2019
    July 2019
    January 2019
    December 2018
    October 2018
    August 2018
    June 2018
    April 2018
    March 2018
    February 2018
    January 2018
    July 2017
    June 2017
    May 2017
    April 2017
    March 2017
    January 2017
    September 2016
    August 2016
    July 2016
    May 2016
    April 2016
    March 2016
    December 2015
    November 2015
    August 2015
    July 2015
    June 2015
    April 2015
    March 2015
    February 2015
    January 2015
    December 2014
    November 2014
    October 2014

    RSS Feed