Tweening with DOTween

There are several really good tweening libraries for Unity although I must say DOTween is my favorite since it has a wealth of features and performs well even on mobile. DOTween is open source although there is a DOTween Pro version which includes extra features such as visual editors but most importantly allows you to show some support to the fantastic developer of this asset.

DOTween augments various types with extension methods making it easy to tween a large variety of properties. The available tweening methods are grouped together in the IntelliSense popup since they all begin with the prefix “DO”. DOTween provides a fluid style API which I feel provides a clean way of defining animations.

For example, an object can be transformed on it’s Y-axis with:

    .DOMoveY(targetY, 0.8f)

DOTween offers a lot of control over how the tweening will be processed; for instance, you can pick between absolute and relative transformations. DOTween provides a rich selection of easing functions; I find the website useful for previewing how each of the easing functions will behave.

One of my favorite features of DOTween is it’s sequences feature which allows you to define a sequence of tweens which can occur in parallel or in series. Sequences are composed from a number of tweens, intervals, callbacks, etc. This allows you to compose complex animations but interestingly can also be used to schedule tasks without having to manage them manually:

    .AppendCallback(() => {
        // do something...

Since sequences can include callbacks it is possible to initiate a sequence of tweens followed by callbacks to perform any sort of logic including cleanup or state transitions. Consider an example where a projectile collides with an explosive barrel; a sequence of actions can occur:

  1. Spawn explosion special effect; such as a particle system.
  2. Start animating the barrel breaking down into fragments.
  3. Despawn the explosion special effect.

For example,

Transform effectInstance = prefabPool.Spawn(explosionEffectPrefab.transform);
effectInstance.position = barrelGameObject.transform.position;

            .DOScale(0f, shrinkDurationInSeconds)
    .OnKill(() => {

When starting a tween any value can be provided as an identifier for that tween. I find it useful to provide an identifier of the owning object so that the tween can be canceled if the object is destroyed before the tween is completed with DOTween.Kill(id).

DOTween will invoke the OnKill callback when the tween either completes or when it is killed prematurely. This is a useful place to cleanup unwanted objects such as a temporary particle system special effect.

DOTween also works really well with Unity’s coroutine support; it provides methods which return a YieldInstruction which can be yielded by a coroutine:

public override IEnumerator DestroyCoroutine()
    yield return this.transform
        .DOScale(0f, this.durationInSeconds)