Skip to content

Make Linear Interpolation Easier. Lerp a certain distance in a given unity of time. Pass the functions a distance and a time... or simply a velocity... and it will do the rest for you.

Notifications You must be signed in to change notification settings

b-cancel/Unity3D_LerpKit_Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

Unity3D_LerpKit_Library

If this project helped you reduce developement time or you just want to help me continue making useful tools
Feel free to buy me a cup of coffee! :)
PayPal Donate Button

Demo

This crudely constructed demo clearly illustrates the differences between using .Lerp(), .MoveTowards(), and .Lerp() + LerpKit
https://b-cancel.github.io/Unity3D_LerpKit_Demo/

Purpose

   This tool was created so that it was possible to travel from point A to point B with a constant intuitive velocity without physics.
   Unity does have .Lerp() for Color, Mathf, Color32, Vector2, Vector3, Vector4, Material, and Quaternion... but to be able to do what is described above we would have to change the "t" value every frame. Unity also has .MoveTowards() for Mathf, Vector2, Vector3, and Vector4 but its hard to convert the "maxDistanceDelta" into some intuitive velocity.
   This is why the lerpKit was created.

How It's Done

   As explained above to do this properly with .Lerp() we would have to update the "t" value every frame. If we want to travel said distance with a constant velocity then we simply need to calculate this "t" value every frame bassed on some velocity. A velocity required some guideDistane, and some guideTime.

How To Use Lerp Kit

There are 2 ways to use Lerp Kit; each with their own pros and cons...
  1. using the "lerpKit" namespace
    • use the "using lerpKit;" directive → use "lerpHelper.function(...)" to call the function
  2. using extension methods
    • create a "dummy instance" of (Mathf, Vector2, Vector3, Vector4, or Color) → use "dummyInstance.function(...)" to call the function
    • create an "instance" of (Mathf, Vector2, Vector3, Vector4, or Color) → use "instance.function(..)" to call the function with "instance" replacing the first parameter [of the dummy instance method]

Enumerables

  • updateLocation { fixedUpdate, Update };
    • it makes a difference where you place your "Type.lerp(...)" and you must indicate where you place it to properly calculate your lerp velocity
    • NOTE: this can be read in by reading the call stack but its too expensive to be worth it
  • unitOfTime { frames, seconds };
    • this allows for easy conversion of your guide time into frames if you pass it seconds
  • guideDistance { distBetween_Other, distBetween_StartAndEnd, distBetween_CurrAndEnd, distBetween_StartAndCurr };
    • it makes sense to use one of these often. So you can simply chose an enumerable and the functions will calculate the distance you chose for you
    • NOTE: distBetween_Other should only be used for using the Kit with Color in which case distBetween_Other will point to the largest possible distance between 2 points in the RGBA color space.
    • To pass a custom distance you do not need the guideDistance enum

Lerp Kit Documentation

The namespace that contains the implementation of every class used is called "lerpKit"
The only class contained within that namespace is called "lerpHelper"
The Functions within "lerpHelper" are… (all of these are public static float)

For Guide Distance Calculation

Check the enumerables section to understand the guideDistance type
  • calcGuideDistance(float startValue, float currValue, float endValue, guideDistance GD)
  • calcGuideDistance(Vector2 startVect2, Vector2 currVector2, Vector2 endVector2, guideDistance GD)
  • calcGuideDistance(Vector3 startVect3, Vector3 currVector3, Vector3 endVector3, guideDistance GD)
  • calcGuideDistance(Vector4 startVect4, Vector4 currVector4, Vector4 endVector4, guideDistance GD)
  • calcGuideDistance(float[] startValues, float[] currValues, float[] endValues, guideDistance GD)

For Lerp Value Calculation

LerpVelocity should be passed in distance per frame (either update or fixed update, depending on where your .Lerp() function is placed)
  • calcLerpValue(float currValue, float endValue, float lerpVelocity_DperF)
  • calcLerpValue(Vector2 currVector2, Vector2 endVector2, float lerpVelocity_DperF)
  • calcLerpValue(Vector3 currVector3, Vector3 endVector3, float lerpVelocity_DperF)
  • calcLerpValue(Vector4 currVector4, Vector4 endVector4, float lerpVelocity_DperF)
  • calcLerpValue(float[] currValues, float[] endValues, float lerpVelocity_DperF)
  • calcLerpValue(Color currColor, Color endColor, float lerpVelocity_DperF)

For Lerp Velocity Calculation

This will calculate Lerp Velocity in Distance per frame for you with more intuitive parameters
  • calcLerpVelocity(float guideDistance, float timeToTravel_GD, unitOfTime UOT_GD, updateLocation UL)

Extra

  • distance(Color color1, Color color2)
    • This was Created because Color has no definition of Distance. So I simply Imagined every possible color as a value in a 3D color cube and then I was able to use Vector3.Distance()

Limitations

There are no lerpKit functions that will help you with Material.Lerp(), Quaternion.Lerp(), or Mathf.LerpAngle().

About

Make Linear Interpolation Easier. Lerp a certain distance in a given unity of time. Pass the functions a distance and a time... or simply a velocity... and it will do the rest for you.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages