Tips Tuesday: DOTween

Hey everyone,

One of the reason I really like Unity, as opposed to other game engines, is the community and vast wealth of information and resources made available to it. In that respect, I thought I’d take the time to highlight some such tips, that in my view simply do not get enough exposure.

All my tips involve tools that are free or freely accessible, and so I really encourage you to have a go at them and thus expand your game dev arsenal and knowledge.

Subject this week: DOTween. You can get it free from the asset store

DOTween is a Brackeys top 5 Unity editor[1] - one he calls feeling dumb for not using it earlier - and is used to make simple animations.

To showcase it how much it would simplify your life, I’ll use the Fader class from the RPG course

RPG course

public IEnumerator FadeOut(float time)
{
  while(canvasGroup.alpha < 1)
  {
    canvasGroup.alpha += Time.DeltaTime / time;
    yield return null;
  }
}

public IEnumerator FadeIn(float time)
{
  while(canvasGroup.alpha > 0)
  {
    canvasGroup.alpha -= Time.DeltaTime / time;
    yield return null;
  }
}

and now with DOTween, this is how it would look like:

public void FadeOut(float time) { canvasGroup.DOFade(0, time); }

public void FadeIn(float time) { canvasGroup.DOFade(1, time); }

As you can see, with DOTween you are given extra methods to use out of the box - called extension methods - and all you need to do is just call these methods… in this case, the DOFade() method on the CanvasGroup. It doesn’t even need to be a co-routine as DOTween will manage it for you (making sure the fading happens across the desired number of frames)

Now, the keener of you may have the following question: “but the RPG course version does other things before and after the fading, like loading a new scene during the whole co-routine between the fades…”. Well that wouldn’t be a problem thanks to DOTween sequences, but this post is long enough and I’ll cover that as a future Tips Tuesday post!

Hope you found this useful and interesting…

[1] Top 5 Editor Extensions - YouTube

1 Like

Might have forgotten to give a better idea of what DOTween does. Essentially, having it in your project gives you access to some extra methods, the majority of which are on an object’s transform.

Consider the following example (that you’ve probably done a million times before): you want to move an object from its current position to a new position, in a fixed amount of time. This might be a elevator you are moving up, or a spell ability that smoothly move your player backwards for 2 seconds, or a platform that moves left and right every 5 seconds or so…

What you’re probably doing right now is:

// moving the gameObject along the x-axis a distance of 10 for 5 seconds

[SerializeField] float duration = 5;
[SerializeField] float distance = 10;

private float timeElapsed = 0;

private void Update()
{
  timeElapsed += time.DeltaTime;
  
  if (timeElapsed < duration)
  {
    float distanceToMoveThisFrame = distance * time.DeltaTime / duration;
    transform.position += new Vector3(distanceToMoveThisFrame , 0, 0);
  }
}

whereas with the DOMove() method provided by DOTween, all this becomes as followed (and it only needs to be called once)

Vector3 newPosition = transform.position + new Vector3(distance, 0, 0);
transform.DOMove(newPosition, duration);

Then DOTween will move the transform smoothly by 10 along the x over a 5 seconds period.

1 Like