0
\$\begingroup\$

The script is attached to the transform that moves to target :

using UnityEngine;
using UnityEditor;
using System.Collections;

public class LerpExample : MonoBehaviour
{
    [Header("Animation-Curve")]
    public AnimationCurve slideCurve;
    public Transform finalPosition;

    private Vector3 initialPosition;
    public float time;

    private void Awake()
    {
        initialPosition = transform.localPosition;
    }

    private void Start()
    {
        StartCoroutine(MoveObject());
    }

    private IEnumerator MoveObject()
    {
        float i = 0;
        float rate = 1 / time;
        while (i < 1)
        {
            i += rate * Time.deltaTime;
            transform.localPosition = Vector3.Lerp(initialPosition, finalPosition.position, slideCurve.Evaluate(i));
            yield return 0;
        }
    }
}

And the curve graph in the inspector :

Animation curve graph in the inspector

For example, I set the time in the inspector to 3 then the transform smoothes slowly start increasing speed and stop at the target.

But what if I want that the transform will decrease the speed slowly near the end and will stop completely once reaching the target?

I tried this graph then but this makes the transform also go back to the initialPosition and I don't want it to move back to the initialPosition I want it to increase the speed at the start and decrease speed near the end and stop at the target smooth slowly.

This graph is not what I need because the transform return to his original start position

I'm not sure what graph should I make.

This graph seems to be working.

The graph is working as I wanted

\$\endgroup\$
1
  • 1
    \$\begingroup\$ "This graph seems to be working." - Did you mean to post this as an Answer below? If not, what is it about this graph that you find unsatisfactory, that an answer should seek to improve? \$\endgroup\$
    – DMGregory
    Jul 28 at 4:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.