Skip to content

Instantly share code, notes, and snippets.

@pointcache
Last active December 31, 2016 19:44
Show Gist options
  • Save pointcache/e0463dbe255d7158fff9cec2546d40ba to your computer and use it in GitHub Desktop.
Save pointcache/e0463dbe255d7158fff9cec2546d40ba to your computer and use it in GitHub Desktop.
/// <summary>
/// Object that performs interpolation, works in tandem with Interpolator component
/// </summary>
public class TransformInterpolator
{
/// <summary>
/// target transform
/// </summary>
Transform transform;
/// <summary>
/// Set this to set next target position to interpolate to
/// </summary>
public Vector3 position { get { return transform.position; } set { was_posUpdated = true; lastPos = posUpdate; posUpdate = value; } }
bool was_posUpdated;
Vector3 posUpdate, lastPos;
/// <summary>
/// holds time since last pos_update
/// </summary>
float interpolationDriver;
public TransformInterpolator(Transform target)
{
transform = target;
}
public void Update()
{
//if received new position reset timer
if (was_posUpdated )
{
was_posUpdated = false;
interpolationDriver = 0f;
}
interpolationDriver += Time.deltaTime;
//remap time to 0-1
float lerp = interpolationDriver.Remap(0f, Time.fixedDeltaTime, 0f, 1f);
//set target position
transform.position = Vector3.Lerp(lastPos, posUpdate,lerp );
}
}
@pointcache
Copy link
Author

remap

public static float Remap(this float value, float from1, float to1, float from2, float to2)
    {
        return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment