Tips Tuesday: DOTween

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