Tweening
1. Understanding the naming convention
Prefix | Behavior | Animation curve |
|---|---|---|
| Starts slowly, then accelerates | Slow → Fast |
| Starts quickly, then decelerates | Fast → Slow |
| Starts slowly, accelerates, then slows down | Slow → Fast → Slow |
Easing family |
|
|
| Description |
|---|---|---|---|---|
Linear | — | — | — | Constant speed throughout the animation. |
Sine |
|
|
| Gentle, smooth acceleration and deceleration. |
Quad |
|
|
| Mild acceleration and deceleration. |
Cubic |
|
|
| Stronger acceleration and deceleration than Quad. |
Quart |
|
|
| More pronounced acceleration and deceleration. |
Quint |
|
|
| Very pronounced acceleration and deceleration. |
Expo |
|
|
| Exponential acceleration and deceleration. |
Circ |
|
|
| Circular-style easing with a pronounced speed change. |
Back |
|
|
| Pulls backward or overshoots the target before settling. |
Elastic |
|
|
| Spring-like movement with oscillation. |
Bounce |
|
|
| Simulates bouncing movement at the beginning, end, or both. |
2. Special easing functions
DOTween also documents four special Flash easing functions.
Easing | Behavior |
|---|---|
| Creates a flashing effect during the tween. |
| Applies the flashing effect with an ease-in pattern. |
| Applies the flashing effect with an ease-out pattern. |
| Applies the flashing effect with an ease-in-out pattern. |
3. Starting, Stopping, and Controlling Tweens
Concept | What you'll learn |
|---|---|
| Permanently stop an active tween |
| Temporarily stop an animation |
| Resume a paused animation |
| Start an animation again from the beginning |
| Return to the starting state and pauses it |
| Check whether a tween is playing |
| Check whether a tween has finished |
| Control what happens when a tween finishes |
Setting | Behavior |
|---|---|
| Automatically removes tween after completion |
| Keeps tween available after completion |
When you intend to reuse a tween with Restart(), Rewind(), or other control methods after it finishes, use SetAutoKill(false).
This is particularly useful for UI animations that happen repeatedly.
4. Complete Sequence Reference Chart
Save this table in your notes.
Method | Purpose | Example |
|---|---|---|
| Adds tween to the end |
|
| Plays alongside the last inserted tween |
|
| Starts tween at a specific time |
|
| Adds tween to beginning |
|
| Adds a pause for the tween |
|
| Adds a pause at the beginning |
|
| Executes code at the end |
|
| Executes code at a specific time |
|
| Executes code at the beginning |
|
5. AppendCallback()
AppendCallback() lets you execute a piece of C# code at a specific point in the sequence.
For example:
Sequence mySequence = DOTween.Sequence();
mySequence.Append(transform.DOScale(1.5f, 1f));
mySequence.AppendCallback(() =>
{
Debug.Log("Scaling finished!");
});
mySequence.Append(transform.DOScale(1f, 1f));The sequence now does this:
Scale up.
Print
"Scaling finished!"in the Console.Scale back down.
The callback itself doesn't animate anything. It simply executes code when the sequence reaches that point.
This opens up more possibilities for game development:
Open a menu, then enable its buttons.
Play a sound when a button animation finishes.
Trigger an enemy attack after an animation.
Display text after a UI transition.
Execute gameplay logic at specific animation milestones.
6: Callbacks and Events
Concept | What you'll learn |
|---|---|
Callback | Execute code during an animation |
| Execute code when a tween starts |
| Execute code when a tween begins or resumes playing |
| Execute code as a tween updates |
| Execute code when a tween finishes |
| Execute code when a tween is killed |
| Execute code at a specific point in a sequence |
| Execute code at a specific timeline position |
| When the tween is rewound |
| Every time one loop cycle completes |
OnStart() executes only on the first start. Resuming a paused tween does not trigger it again.
OnPlay() executes whenever the tween enters the playing state, including when it resumes from being paused.
using UnityEngine;
using DG.Tweening;
public class TweenCallbacks : MonoBehaviour
{
void Start()
{
mySequence.Join(transform.DORotateQuaternion(origRot, 1f)).SetEase(Ease.Linear).SetAutoKill(false)
.OnComplete(() => Debug.Log("Animation Has Been Complete"));
}
}OnComplete()tells DOTween when to execute the callback.() =>is a C# lambda expression.The code inside
{ }is what gets executed.The callback runs when the tween reaches completion.
AppendCallback()
AppendCallback() executes code at the end of the current sequence timeline.
Example:
Sequence sequence = DOTween.Sequence();
sequence.Append(transform.DOScale(1.5f, 1f));
sequence.AppendCallback(() =>
{
Debug.Log("Scaling finished!");
});
sequence.Append(transform.DOScale(1f, 1f));Here's the execution order:
Sequence timeline
Scale up
0–1 second
AppendCallback()
Prints "Scaling finished!" at 1 second.
Scale down
1–2 seconds
InsertCallback()
InsertCallback() works similarly, except you specify the exact time when you want the code to execute.
The structure:
sequence.InsertCallback(timePosition, callback);Example:
Sequence sequence = DOTween.Sequence();
sequence.Append(transform.DOMoveX(5f, 2f));
sequence.InsertCallback(1f, () =>
{
Debug.Log("One second has passed!");
});What's happening?
The movement begins at 0 seconds.
At 1 second, the callback executes.
The movement continues until 2 seconds.
You can execute code while another animation is still running.
Using Named Methods Instead of Lambdas
You don't always have to put all your code inside a callback.
You can call an existing method instead.
For example:
void Start()
{
transform.DOScale(1.5f, 2f)
.OnComplete(AnimationFinished);
}
void AnimationFinished()
{
Debug.Log("Animation finished!");
}Compare the two approaches:
Lambda approach:
.OnComplete(() =>
{
Debug.Log("Finished!");
});Named method approach:
.OnComplete(AnimationFinished);Both work. Use lambdas for small pieces of code and named methods when you have more logic to execute or want to reuse that logic.
7.Loops & Delays
Concept | What you'll learn |
|---|---|
| Repeat an animation |
| Start over after each loop |
| Play forward and backward |
| Continue increasing the target value |
| Delay the beginning of a tween |
| Add a pause inside a sequence |
Infinite loops | Repeat an animation indefinitely |
Loop callbacks | Detect when individual loops finish |
SetLoops()
Let's start with the basic concept.
In DOTween, SetLoops() determines how many times an animation repeats.
The general structure is:
transform.DOScale(1.5f, 1f)
.SetLoops(3);This tells DOTween to:
Scale the object to 1.5.
Repeat that animation.
Complete three loops in total.
The important distinction is that the default loop type is LoopType.Restart.
LoopType.Yoyo
LoopType.Yoyo makes the animation play forward and then backward.
Example:
transform.DOScale(1.5f, 1f)
.SetLoops(4, LoopType.Yoyo);Instead of snapping back to its original scale, the object smoothly returns to its starting scale.
Yoyo animation
Loop 1
Scale 1.0 → 1.5
Loop 2
Scale 1.5 → 1.0
Loop 3
Scale 1.0 → 1.5
Loop 4
Scale 1.5 → 1.0
Finished at scale 1.0
Important: The loop count includes both directions. Four loops means two complete forward-and-back cycles.
This is particularly useful for:
Button pulsing.
Idle animations.
Floating objects.
UI notifications.
Repeated highlighting effects.
LoopType.Incremental
More advanced
LoopType.Incremental makes the animation continue increasing its target value with every loop.
Unlike Restart and Yoyo, it doesn't return to the original target.
Example:
transform.DOMoveX(2f, 1f)
.SetLoops(3, LoopType.Incremental);Assuming the object's initial X position is 0:
Incremental animation
Loop 1: X = 0 → 2
Loop 2: X = 2 → 4
Loop 3: X = 4 → 6
Finished at X = 6
Each loop adds the original change (2 units) to the next destination.
This is useful when you want continuous progression instead of repeating the same movement.
SetDelay()
Now let's look at delays.
SetDelay() determines how long DOTween waits before starting an animation.
The general structure:
transform.DOScale(1.5f, 1f)
.SetDelay(2f);What's happening?
Wait 2 seconds.
Begin scaling.
Finish scaling after 1 second.
These two methods are easy to confuse.
They both create waiting periods, but they serve different purposes.
|
|
|---|---|
Delays the start of a tween | Adds a pause inside a sequence |
Usually used on individual tweens | Used when constructing a Sequence |
Can be applied before the tween starts | Can be placed between animations |
8. UI Animations
A RectTransform adds properties specifically designed for UI layout:
Anchors
Pivot
Anchored Position
Width and Height
Size Delta
What's the difference between Position and Anchored Position?
Property | What it represents |
|---|---|
| World position |
| Position relative to parent |
| UI position relative to its anchors |
| Size adjustment relative to its anchors |
Method | What it animates | Common use |
|---|---|---|
| Anchored position | Sliding menus |
| Horizontal UI position | Horizontal movement |
| Vertical UI position | Vertical movement |
| UI size | Expanding panels |
| UI scale | Button hover |
| Local rotation | UI rotation |
| UI color | Button color changes |
| Transparency | Fade transitions |
| Image fill amount | Health bars |
| Slider value | Progress bars |
Camvas Group
Canvas
└── MainMenu
├── Background
├── Title
├── PlayButton
└── SettingsButtonRather than individually fading every child, you can attach a CanvasGroup to MainMenu.
Then:
CanvasGroup menuGroup = GetComponent<CanvasGroup>();
menuGroup.DOFade(0f, 0.5f);All the UI graphics under that CanvasGroup gradually become transparent.
CanvasGroup properties
Property | Purpose |
|---|---|
| Controls transparency, from 0 to 1 |
| Controls whether child UI elements can be interacted with |
| Controls whether the group blocks pointer raycasts |
Important: Setting alpha = 0 does not automatically disable UI interaction.
That's why it's common to combine transparency with:
menuGroup.interactable = false;
menuGroup.blocksRaycasts = false;