Home > Net >  How to use dotween to ease in out a float?
How to use dotween to ease in out a float?

Time:06-07

The goal to tween the "Forward" value.

This is example from the dotween exmaples.

Instead moving the cube i want to change the "Forward" value between 0 and 1.

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

public class Sequences : MonoBehaviour
{
    public Transform cube;
    public float duration = 4;
    public Animator anim;

    private valueToLerp = 0;

    IEnumerator Start()
    {
        // Start after one second delay (to ignore Unity hiccups when activating Play mode in Editor)
        yield return new WaitForSeconds(1);

        // Create a new Sequence.
        // We will set it so that the whole duration is 6
        Sequence s = DOTween.Sequence();
        // Add an horizontal relative move tween that will last the whole Sequence's duration
        anim.SetFloat("Forward", valueToLerp);
        s.Append(cube.DOMoveX(6, duration).SetRelative().SetEase(Ease.InOutQuad));
        // Insert a rotation tween which will last half the duration
        // and will loop forward and backward twice
        s.Insert(0, cube.DORotate(new Vector3(0, 45, 0), duration / 2).SetEase(Ease.InQuad).SetLoops(2, LoopType.Yoyo));
        // Add a color tween that will start at half the duration and last until the end
        s.Insert(duration / 2, cube.GetComponent<Renderer>().material.DOColor(Color.yellow, duration / 2));
        // Set the whole Sequence to loop infinitely forward and backwards
        s.SetLoops(-1, LoopType.Yoyo);
    }
}

CodePudding user response:

You can use DoVirtual.Float, you will not need the valueToLerp variable in this method.

// anim.SetFloat("Forward", valueToLerp);

s.Append(DOVirtual.Float(0, 1f, 1f, v => anim.SetFloat("Forward", v)));

CodePudding user response:

Also the solution (but DOVirtual.Float from another comment is better):

    var forwardTween = DOTween.To(
            () => anim.GetFloat("Forward"),
            (val) => anim.SetFloat("Forward", val),
            6,
            duration)
        .SetRelative()
        .SetEase(Ease.InOutQuad);
    s.Append(forwardTween);
  • Related