This repository has been archived on 2024-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
snakeinabox/Assets/Scripts/Tools.cs
2020-12-05 19:18:40 -08:00

41 lines
721 B
C#

using System;
using UnityEngine;
// Put in Tools.cs (no Tool class tho)
[Serializable]
public class Lerper
{
public float t = 0;
public float spring = 1;
public float dampen = 1;
float vel;
public void Update(float to = 1, bool bounce = false)
{
float dir = to - t;
vel += dir * spring * Time.deltaTime;
if (Mathf.Sign(vel) != Mathf.Sign(dir))
{
vel *= 1 - (dampen * Time.deltaTime);
}
else
{
vel *= 1 - (dampen * 0.33f * Time.deltaTime);
}
float newt = t + vel * Time.deltaTime;
if (bounce && (newt < 0 || newt > 1))
{
vel *= -0.5f;
newt = Mathf.Clamp01(newt);
}
t = newt;
}
public void Reset()
{
t = vel = 0;
}
}