dotween icon indicating copy to clipboard operation
dotween copied to clipboard

Move To follow target in DOTweenAnimation component.

Open LesserM7md opened this issue 2 years ago • 9 comments

I'm trying to get an object to follow a moving target using the animation component but the DOTweenAnimation only loops between the original position the target object was at. I see here that that was the intended behavior. But do you plan on adding the DOTweenFollow component in the future?

LesserM7md avatar Apr 07 '22 10:04 LesserM7md

The DOTweenFollow I think it is necessary, you can add this function pls, thanks!

brucezhu99 avatar Jul 27 '22 02:07 brucezhu99

I have the same struggle. It would be so lovely to have something like follow transform at runtime. When the tracked transform's position changes, maybe the tween we set will follow with some offset distance?

MuhammedResulBilkil avatar Sep 02 '22 15:09 MuhammedResulBilkil

I second this as a major drawback of DOTween. We need something akin to transform.MoveTo(target.transform, 0.5f).

public static void MoveTo(this Transform transform, Transform target, float duration) {
    // Poll the target position each update tick
}

Execution on / off main thread can causes challenges when working with transforms, but this is something that should be possible - even if we have to accept main thread only execution for this scenario.

otdavies avatar Oct 09 '22 01:10 otdavies

Same, we need some follow tween.

Voley avatar Dec 09 '22 15:12 Voley

You can make the object move in the target local space that would be similar to moving toward the target object. Something like that:

public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveInTargetLocalSpace(this Transform transform, Transform target, Vector3 targetLocalEndPosition, float duration)
        {
            var t = DOTween.To(
                () => transform.position - target.transform.position, // Value getter
                x => transform.position = x + target.transform.position, // Value setter
                targetLocalEndPosition, 
                duration);
            t.SetTarget(transform);
            return t;
        }

You can also try playing with formulas to make the movement even better. All that you need is set the right value in DOSetter. I think you got the idea.

Anma-wp avatar Dec 25 '22 08:12 Anma-wp

http://forum.demigiant.com/index.php?topic=225.0 this also

builder-main avatar Feb 24 '23 21:02 builder-main

This works pretty well for me, feel free to adapt. It updates the end position at some defined time. you could reduce steps and nextUpdate initial value. Chunks needs to be large enough or you'll enter the ease out and start having weird speed issues.

                    float GetDistance()
                    {
                        return Vector3.Distance(tmpTarget.transform.position, target.position);
                    }

                    var updateStep = GetDistance() * 0.3f;
                    var nextUpdate = GetDistance() * 0.7f;
                    tween
                        //updating target for taking moving into account
                        //.OnStart(() => { tween.ChangeEndValue(target.position, snapStartValue: true);})
                        .OnUpdate(() =>
                    {

                        if ( GetDistance() < nextUpdate)
                        {
                            DebugGraph.Log("Updated", GetDistance());
                            tween.SetEase(GetOutEaseVersion(tweenSettings.EaseType))
                                .ChangeEndValue(target.position, snapStartValue: true, newDuration: tween.Duration() - tween.Elapsed());
                            nextUpdate -= updateStep;
                        }


                    });


      /// <summary>
        /// Update the ease type to out only when updating the move target to avoid re-easing in.
        /// </summary>
        /// <param name="tweenSettingsEaseType"></param>
        /// <returns></returns>
        private static Ease GetOutEaseVersion(Ease tweenSettingsEaseType)
        {
            switch (tweenSettingsEaseType)
            {
                case Ease.InOutSine: return Ease.OutSine;
                case Ease.InOutFlash: return Ease.OutFlash;
                case Ease.InOutQuad: return Ease.OutQuad;
                case Ease.InOutCubic: return Ease.OutCubic;
                case Ease.InOutQuart: return Ease.OutQuart;
                case Ease.InOutQuint: return Ease.OutQuint;
                case Ease.InOutExpo: return Ease.OutExpo;
                case Ease.InOutCirc: return Ease.OutCirc ;
                case Ease.InOutElastic: return Ease.OutElastic;
                case Ease.InOutBack: return Ease.OutBack;
                case Ease.InOutBounce: return Ease.OutBounce;
                default:
                    return tweenSettingsEaseType;
            }
        }

builder-main avatar Feb 24 '23 21:02 builder-main

You can make the object move in the target local space that would be similar to moving toward the target object. Something like that:

public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveInTargetLocalSpace(this Transform transform, Transform target, Vector3 targetLocalEndPosition, float duration)
        {
            var t = DOTween.To(
                () => transform.position - target.transform.position, // Value getter
                x => transform.position = x + target.transform.position, // Value setter
                targetLocalEndPosition, 
                duration);
            t.SetTarget(transform);
            return t;
        }

You can also try playing with formulas to make the movement even better. All that you need is set the right value in DOSetter. I think you got the idea.

Thanks! Works perfectly! In my case targetLocalEndPosition is just Vector3.zero

Dacusx avatar Mar 23 '23 12:03 Dacusx

You can make the object move in the target local space that would be similar to moving toward the target object. Something like that:

public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveInTargetLocalSpace(this Transform transform, Transform target, Vector3 targetLocalEndPosition, float duration)
        {
            var t = DOTween.To(
                () => transform.position - target.transform.position, // Value getter
                x => transform.position = x + target.transform.position, // Value setter
                targetLocalEndPosition, 
                duration);
            t.SetTarget(transform);
            return t;
        }

You can also try playing with formulas to make the movement even better. All that you need is set the right value in DOSetter. I think you got the idea.

Works wonderfully! As if it was part of the library

AvinoamHeroicRealm avatar Mar 30 '23 22:03 AvinoamHeroicRealm