41 lines
721 B
C#
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;
|
|
}
|
|
}
|