If this project helped you reduce developement time or you just want to help me continue making useful tools
This crudely constructed demo clearly illustrates the differences between using .Lerp(), .MoveTowards(), and .Lerp() + LerpKit
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. 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.
-
using the "lerpKit" namespace
- use the "using lerpKit;" directive → use "lerpHelper.function(...)" to call the function
-
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]
-
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
The only class contained within that namespace is called "lerpHelper"
The Functions within "lerpHelper" are… (all of these are public static float)
- 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)
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)
- calcLerpVelocity(float guideDistance, float timeToTravel_GD, unitOfTime UOT_GD, updateLocation UL)
- 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()