A 💩 tweening library for Unity - only made for the pun, don't actually use it.
Make sure you have using Coroutween;
at the top of your script
After that, you have a few options for how to tween stuff. In the following examples I'll show how to change the FOV of a camera with each approach.
- The easiest way is to use premade tweens like this:
camera.FieldOfViewTo (to: 25f, duration, EaseType.ElasticOut);
- The next way lets you tween any supported value (
int
,float
,Vector2/3/4
,Quaternion
,Color
)
Coroutween.To (from: camera.fieldOfView, to: 25f, duration, EaseType.ElasticOut, x => camera.fieldOfView = x);
- The final way gives you complete control by simply providing a callback with the tween's eased progress.
var from = camera.fieldOfView;
var to = 25f;
Coroutween.CreateInterpolater (duration, EaseType.ElasticOut, t => camera.fieldOfView = Mathf.LerpUnclamped (from, to, t));
All of these examples produce the same result:
Every tween method that has an EaseType
will also have an overload with an EaseMethod
which is a callback that lets you calculate your own easing. If, for example, you wanted to use an animation curve instead of a preset easing type, you could pass curve.Evaluate
instead of some EaseType
.
Here's small example of how you could use it:
public float duration = 1f;
public AnimationCurve positionCurve = new AnimationCurve ();
private void Update ()
{
if (Input.GetKeyDown (KeyCode.Space))
transform.PositionTo (transform.position + Vector3.up, duration, positionCurve.Evaluate);
}
If you are a little clever, you could even use tweens with physics
public Transform target;
public float duration = 1f;
public Rigidbody rb;
private Vector3 targetPosition;
private IEnumerator Start ()
{
while (true)
{
yield return Coroutween.To (transform.position, target.position, duration, EaseType.ElasticOut, x => targetPosition = x);
}
}
private void FixedUpdate ()
{
rb.MovePosition (targetPosition);
}