Using Dotween to make animation

You can use unity timeline to make animation, but compare to using code make animation, the computation cost is too much and also not flexibility.

the following Shape.cs use the basic method to move the objects

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Shape : MonoBehaviour
{
    [SerializeField] private Transform _innerShape, _outerShape;
    [SerializeField] private float _cycleLength = 20f;

    void Start()
    {
        transform.DOMove(transform.position + new Vector3(10, 0, 0), _cycleLength).SetEase(Ease.InOutSine).SetLoops(-1, LoopType.Yoyo);
        _innerShape.DORotate(new Vector3(0, 360, 0), _cycleLength / 2, RotateMode.FastBeyond360).SetLoops(-1, LoopType.Restart).SetEase(Ease.Linear);
        _innerShape.DOLocalMove(new Vector3(0, -3, 0), _cycleLength / 2f).SetEase(Ease.InOutSine).SetLoops(-1, LoopType.Yoyo);
    }
}

using sequence

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class WildShapeManager2 : MonoBehaviour
{

    [SerializeField] private Transform[] coins;
    [SerializeField] private float duration = 2f;
    void Start()
    {
        Sequence sequence = DOTween.Sequence();
        foreach (Transform coin in coins)
        {
            sequence.Append(coin.DOMoveX(coin.position.x + 2, duration).SetEase(Ease.Linear));
        }
        sequence.OnComplete(() =>
        {
            foreach (Transform coin in coins)
            {
                coin.DOScale(Vector3.zero, 0.5f).SetEase(Ease.InBounce);

            }
        });
    }

}

using async

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;

public class WildShapeManager3 : MonoBehaviour
{
    [SerializeField] private Transform[] coins;
    [SerializeField] private float duration = 2f;

    void Start()
    {
        AsyncBaby();
    }
# Using Async
    async void AsyncBaby()
    {
        foreach (Transform coin in coins)
        {
            await coin.DOMoveX(5, duration).SetRelative().SetEase(Ease.Linear).AsyncWaitForCompletion();
        }

        foreach (Transform coin in coins)
        {
            coin.DOScale(Vector3.zero, 0.5f).SetEase(Ease.InBounce);

        }
    }
# using Task
    async void Tasks()
    {
        List<Task> tasks = new List<Task>();
        foreach (Transform coin in coins)
        {
            tasks.Add(coin.DOMoveX(5, duration).SetRelative().SetEase(Ease.Linear).AsyncWaitForCompletion());
        }
        await Task.WhenAll(tasks);
        foreach (Transform coin in coins)
        {
            coin.DOScale(Vector3.zero, 0.5f).SetEase(Ease.InBounce);
        }
    }
}

A method using OnComplete Callback

public class WildShapeMananger : MonoBehaviour
{
    [SerializeField] private GameObject[] coins;
    [SerializeField] private float duration = 2f;

    void Start()
    {
        coins[0].transform.DOLocalMoveX(5, duration).OnComplete(() =>
        {
            coins[1].transform.DOLocalMoveX(5, duration).OnComplete(() =>
            {
                coins[2].transform.DOLocalMoveX(5, duration).OnComplete(() =>
                {
                    foreach (GameObject coin in coins)
                    {
                        coin.transform.DOScale(Vector3.zero, 1f).SetEase(Ease.InBounce);
                    }
                });
            });
        });
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.