DOTween

Unity でイージングを簡単にするやつ.

gameObjectの軌道上を動く

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DG.Tweening;
using UnityEngine;
 
public class PathFollower : MonoBehaviour {
    [SerializeField]
    private List<GameObject> points;
    
    void Start() {
        
        
        var path = points
            .Select(g => g.transform.position)
            .ToArray();
        transform
            .DOLocalPath(path, 2.0f, PathType.CatmullRom)
            .SetEase(Ease.Linear)
            .SetLookAt(0.01f, Vector3.forward)
            // false: ループしない, Y固定
            .SetOptions(false)
            .SetLoops(-1);
    }
}

DOTweenをawaitableにする

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using DG.Tweening;
using UnityEngine;
 
// <https://www.shibuya24.info/entry/dotween_async_await>
 
/// <summary>
/// DOTweenをasync/awaitに返還すDOTweenAwaiterの拡張メソッド
/// </summary>
public static class AsyncDOTweenExtention {
    public static DOTweenAwaiter ToAwaiter(this Tween tween,
        CancellationToken cancellationToken = default,
        TweenCancelBehaviour behaviour = TweenCancelBehaviour.Kill) {
        return new DOTweenAwaiter(tween, cancellationToken, behaviour);
    }
}
 
/// <summary>
/// DOTweenをawaiterに変換する
/// </summary>
public struct DOTweenAwaiter : ICriticalNotifyCompletion {
    private Tween _tween;
    private CancellationToken _cancellationToken;
    private TweenCancelBehaviour _behaviour;
 
    public DOTweenAwaiter(Tween tween, CancellationToken cancellationToken, TweenCancelBehaviour behaviour) {
        _tween = tween;
        _cancellationToken = cancellationToken;
        _behaviour = behaviour;
    }
 
    public bool IsCompleted => _tween.IsPlaying() == false;
 
    public void GetResult() => _cancellationToken.ThrowIfCancellationRequested();
 
    public void OnCompleted(Action continuation) => UnsafeOnCompleted(continuation);
 
    public void UnsafeOnCompleted(Action continuation) {
        DOTweenAwaiter tmpThis = this;
        var tween = _tween;
        var regist = tmpThis._cancellationToken.Register(() => {
            // tokenが発火したらタイプをチェックしてTweenの終了振る舞いを変更する
            switch (tmpThis._behaviour) {
                case TweenCancelBehaviour.Kill:
                    tween.Kill();
                    break;
                case TweenCancelBehaviour.KillWithCompleteCallback:
                    tween.Kill(true);
                    break;
                case TweenCancelBehaviour.Complete:
                    tween.Complete();
                    break;
            }
        });
 
        _tween.OnKill(() => {
            // CancellationTokenRegistrationを破棄する
            regist.Dispose();
            // 続きを実行
            continuation();
        });
    }
 
    public DOTweenAwaiter GetAwaiter() => this;
}
 
/// <summary>
/// Tweenキャンセル時の振る舞い
/// </summary>
public enum TweenCancelBehaviour {
    Kill,
    KillWithCompleteCallback,
    Complete,
}

参考文献