Giter Club home page Giter Club logo

b-cancel / unity3d_lerpkit_library Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 89 KB

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.

Home Page: https://b-cancel.github.io/Unity3D_LerpKit_Demo/

C# 100.00%
linear-interpolation unity3d unity2d unity-asset unity-scripts unity- distance velocity unity2018 unity2017

unity3d_lerpkit_library's Introduction

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().

unity3d_lerpkit_library's People

Contributors

b-cancel avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.