Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

DOTween Animation Techniques for Unity

Tech May 13 1

DOTween Official Documentation

Understanding DG.Tweening.Ease Enumeration

December 12, 2019 Update:

Problem: When DOTween animations are triggered multiple times before completing (such as consecutive plays, forward or backward playback), it can cause display issues or errors.

Solution: Before starting any animation, include the following code to kill existing tweens:

<ol><li><div><div></div></div><div><div>transform.DOKill();</div></div></li><li><div><div></div></div><div><div>transform.RectTransform().DOKill();</div></div></li></ol>

Making DOTweenAnimation Ignore Time.timeScale = 0:

GetComponent<DOTweenAnimation>().tween.SetUpdate(true);

June 25, 2019 Update:

Adding DOTweenAnimation Dynamically in Unity

Method 1

In testing, this approach sometimes fails unexpectedly. Using prefabs is recommended instead.

<ol><li><div><div></div></div><div><div>using DG.Tweening;</div></div></li><li><div><div></div></div><div><div>using DG.Tweening.Core;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>DOTweenAnimation animComponent = targetObject.gameObject.AddComponent<DOTweenAnimation>();</div></div></li><li><div><div></div></div><div><div>animComponent.animationType = DOTweenAnimationType.LocalRotate;</div></div></li><li><div><div></div></div><div><div>animComponent.easeType = Ease.Linear;</div></div></li><li><div><div></div></div><div><div>animComponent.duration = 0.3f;</div></div></li><li><div><div></div></div><div><div>animComponent.loops = -1;</div></div></li><li><div><div></div></div><div><div>animComponent.endValueV3 = new Vector3(0, 50, 0);</div></div></li></ol>

Method 2

Implementation for continuous rotation of an object.

The default third parameter is RotateMode.Fast, which restricts rotation operations to 360°. Using RotateMode.FastBeyond360 allows rotations beyond this limit.

<ol><li><div><div></div></div><div><div>icon.transform.DOLocalRotate(new Vector3(0, 0, -360), 2, RotateMode.FastBeyond360)</div></div></li><li><div><div></div></div><div><div>                .SetEase(Ease.Linear).SetLoops(-1, LoopType.Restart).Play();</div></div></li></ol>

1. DOTween.To()

Moves a value from one point to another over a specified duration.

<ol><li><div><div></div></div><div><div>using DG.Tweening;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>Vector3 currentValue = new Vector3(0, 0, 0);</div></div></li><li><div><div></div></div><div><div>DOTween.To(() => currentValue, x => currentValue = x, new Vector3(10, 10, 10), 2);</div></div></li><li><div><div></div></div><div><div>                 Initial value            Target value            Duration</div></div></li></ol>

2. DOMove()

Moves the target object from its current position to a new position within 1 second.

<ol><li><div><div></div></div><div><div>transform.DOMove(new Vector3(1, 1, 1), 1);</div></div></li><li><div><div></div></div><div><div>transform.DOLocalMove(new Vector3(1, 1, 1), 1);</div></div></li></ol>

3. DOMoveX(), DOMoveY(), DOMoveZ()

Moves the object along the X/Y/Z axis to a target position of 5 units.

transform.DOMoveX(5, 1);

4. From()

Moves from a target position to the current position.

<ol><li><div><div></div></div><div><div>Absolute position: If current position is (1,0,0), it moves from 5 to 1</div></div></li><li><div><div></div></div><div><div>transform.DOMoveX(5, 1).From();</div></div></li><li><div><div></div></div><div><div>transform.DOMoveX(5, 1).From(false);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>Relative position: If current position is (1,0,0), it moves from 6 to 1 (6-1=5 relative displacement)</div></div></li><li><div><div></div></div><div><div>transform.DOMoveX(5, 1).From(true);</div></div></li></ol>

5. Pause(), DOPlay(), DOPlayForward(), DOPlayBackwards(), DORestart()

Methods for pausing, playing, playing forward, playing backward, and restarting animations.

<ol><li><div><div></div></div><div><div>//DOTween creates tweens that store animation data. By default, tweens are destroyed after completion.</div></div></li><li><div><div></div></div><div><div>//To play backward, set AutoKill to false</div></div></li><li><div><div></div></div><div><div>Tweener animation = transform.DOLocalMove(new Vector3(1, 1, 1), 1);</div></div></li><li><div><div></div></div><div><div>animation.SetAutoKill(false);</div></div></li><li><div><div></div></div><div><div>//Pause animation</div></div></li><li><div><div></div></div><div><div>animation.Pause();</div></div></li><li><div><div></div></div><div><div>//Play animation (only once, subsequent calls will have no effect)</div></div></li><li><div><div></div></div><div><div>transform.DOPlay();</div></div></li><li><div><div></div></div><div><div>//Play animation forward</div></div></li><li><div><div></div></div><div><div>transform.DOPlayForward();</div></div></li><li><div><div></div></div><div><div>//Play animation backward</div></div></li><li><div><div></div></div><div><div>transform.DOPlayBackwards();</div></div></li><li><div><div></div></div><div><div>//Restart animation: When using visual editor, ensure AutoKill is disabled</div></div></li><li><div><div></div></div><div><div>transform.DORestart();</div></div></li></ol>

6. SetEase()

Sets the animation curve, defining how the animation progresses (similar to setting animation effects in presentation software).

<ol><li><div><div></div></div><div><div>Tweener animation = transform.DOLocalMoveX(0, 5);</div></div></li><li><div><div></div></div><div><div>animation.SetEase(Ease.InBounce);</div></div></li></ol>

7. SetLoops

Sets the number of times an animation plays. The following example plays the animation twice.

<ol><li><div><div></div></div><div><div>Tweener animation = transform.DOLocalMoveX(0, 5);</div></div></li><li><div><div></div></div><div><div>animation.SetLoops(2);</div></div></li></ol>

8. Event Functions

Animation event callbacks for various animation states.

<ol><li><div><div></div></div><div><div>Tweener animation = transform.DOLocalMoveX(0, 5);</div></div></li><li><div><div></div></div><div><div>//Callback when animation completes</div></div></li><li><div><div></div></div><div><div>animation.OnComplete(YourMethod);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>//Callback when animation is killed</div></div></li><li><div><div></div></div><div><div>animation.OnKill(YourMethod);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>//Callback when animation plays (called continuously)</div></div></li><li><div><div></div></div><div><div>animation.OnPlay(YourMethod);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>//Callback when animation pauses</div></div></li><li><div><div></div></div><div><div>animation.OnPause(YourMethod);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>//Callback when animation rewinds</div></div></li><li><div><div></div></div><div><div>animation.OnRewind(YourMethod);</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>//Callback when animation starts playing</div></div></li><li><div><div></div></div><div><div>animation.OnStart(YourMethod);</div></div></li></ol>

9. DOText Text Animation

If the text field is empty, displays text character by character over 3 seconds. If text already exists, it will be replaced character by character.

GetComponent<Text>().DOText("Next, we will enter the second chapter", 3);

10. DOShakePosition Shake Effect

Apply this to a camera to create a shake effect.

<ol><li><div><div></div></div><div><div>//Duration, using default values for other parameters</div></div></li><li><div><div></div></div><div><div>transform.DOShakePosition(1);</div></div></li><li><div><div></div></div><div><div>//Duration, intensity (example: shake only on X and Y axes)</div></div></li><li><div><div></div></div><div><div>transform.DOShakePosition(1,new Vector3(3,3,0));</div></div></li></ol>

11. DOColor Color Change

Changes the color from its original value to red over 2 seconds.

GetComponent<Text>().DOColor(Color.red, 2);

12. DOFade Opacity Change

Fades text opacity from 0 to 1 over 3 seconds.

GetComponent<Text>().DOFade(1, 3);

13. Visual Animation Editor

This component is part of the DOTween Pro plugin.

Animations can be controlled programmatically using their IDs.

<ol><li><div><div></div></div><div><div>using UnityEngine;</div></div></li><li><div><div></div></div><div><div>using DG.Tweening;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>public class UIController : MonoBehaviour {</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>    DOTweenAnimation animationComponent;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>	void Start () {</div></div></li><li><div><div></div></div><div><div>        animationComponent = GetComponent<DOTweenAnimation>();</div></div></li><li><div><div></div></div><div><div>    }</div></div></li><li><div><div></div></div><div><div>	</div></div></li><li><div><div></div></div><div><div>    public void OnClick()</div></div></li><li><div><div></div></div><div><div>    {</div></div></li><li><div><div></div></div><div><div>        animationComponent.DOPlayBackwards();</div></div></li><li><div><div></div></div><div><div>    }</div></div></li><li><div><div></div></div><div><div>}</div></div></li></ol>

14. Visual Path Editor

This component is part of the DOTween Pro plugin.

Controls:

  • Shift+Ctrl: Add path points
  • Shift+Alt: Delete path points
  • Duration: Animation duration
  • Delay: Delay before animation starts
  • Ease: Motion mode
  • Loops: Number of plays (-1 for infinite, 0 for once)
  • Path Type: Linear/Catmull Rom (Linear is straight lines, Catmull Rom smooths the path)
  • Close Path: Connects start and end points to form a loop
  • Local Movement: Movement in local coordinates
  • Orientation: Direction of movement (To Path, Look At Transform, Look At Position)
  • Relative: When checked, selecting an object allows moving the entire path
  • Show Indexes: Displays path point indices
  • Handles Type: Free/Full (Full shows three coordinates for each point)
  • Reset Path: Deletes all points and resets the path

Code Control:

The component includes built-in control methods that can be called directly or triggered through Unity UI buttons.

<ol><li><div><div></div></div><div><div>using UnityEngine;</div></div></li><li><div><div></div></div><div><div>using DG.Tweening;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>public class UIController : MonoBehaviour {</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>    DOTweenPath pathAnimation;</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>	void Start () {</div></div></li><li><div><div></div></div><div><div>        pathAnimation = GetComponent<DOTweenPath>();</div></div></li><li><div><div></div></div><div><div>    }</div></div></li><li><div><div></div></div><div><div>	</div></div></li><li><div><div></div></div><div><div>    public void OnClick()</div></div></li><li><div><div></div></div><div><div>    {</div></div></li><li><div><div></div></div><div><div>        //Start animation</div></div></li><li><div><div></div></div><div><div>        pathAnimation.DOPlay();</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>        //Toggle pause: First click continues, second click pauses at current position</div></div></li><li><div><div></div></div><div><div>        pathAnimation.DOTogglePause();</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>        //Restart animation</div></div></li><li><div><div></div></div><div><div>        pathAnimation.DORestart();</div></div></li><li><div><div></div></div><div><div></div></div></li><li><div><div></div></div><div><div>        //Kill animation (cannot be played again afterward)</div></div></li><li><div><div></div></div><div><div>        pathAnimation.DOKill();</div></div></li><li><div><div></div></div><div><div>    }</div></div></li><li><div><div></div></div><div><div>}</div></div></li></ol>

Tags: Unity

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.