-1
\$\begingroup\$

The main goal is to move an object to target with ease in out speed. The speed should increase at the start to max than near the target to slow down smoothly and stop at the target.

At the top of the script, I did

public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public Transform start;
public Transform end;
public float duration = 10.0f;
private float t;

At the Start

void Start()
    {
        t = 0.0f;
    }

The Update

void Update()
    {
        switch (state)
        {
            case TransitionState.MovingTowards:
                var v = destinationTransform.position - transform.position;
                if (v.magnitude < 0.001f)
                {
                    state = TransitionState.Transferring;
                    originTransform = destinationTransform;
                    timer = 0;
                    return;
                }
                t += Time.deltaTime;
                float s = t / duration;
                transform.position = Vector3.Lerp(transform.position,
                    destinationTransform.position, curve.Evaluate(s));
                break;
                
            case TransitionState.Transferring:
                timer += Time.deltaTime;
                this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
                if (timer >= 1.0f)
                {
                this.transform.parent = destinationTransform;
                isChild = true;
                state = TransitionState.None;
                this.enabled = false;
                return;
                }
                break;

            default:
                this.enabled = false;
                return;
        }
    }

This is where I use the lerp and the animation curve :

t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(transform.position,
destinationTransform.position, curve.Evaluate(s));

The variable destinationTransform is the target.

No matter what value I put in the duration variable the transform is moving too fast to the target. Tried to change the duration to 1 to 100 to 10 same speed.

This is how the animation curve graph is in the inspector :

I changed the animationcurved to be like that in the editor in the inspector but it's not affecting the speed of the transform movement in any way. There is no ease in/out effect. The speed is constant at a very very high speed.

Animation Curve Graph

The full script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveToTarget : MonoBehaviour
{
    public enum TransitionState
    {
        None,
        MovingTowards,
        Transferring
    }

    public Transform destinationTransform;
    public float speed;
    public float lerpTime;
    public bool isChild = false;

    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public Transform start;
    public Transform end;
    public float duration = 10.0f;
    private float t;

    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;

    void Start()
    {
        t = 0.0f;
    }

    void Update()
    {
        switch (state)
        {
            case TransitionState.MovingTowards:
                var v = destinationTransform.position - transform.position;
                if (v.magnitude < 0.001f)
                {
                    state = TransitionState.Transferring;
                    originTransform = destinationTransform;
                    timer = 0;
                    return;
                }
                Vector3 moveDir = v.normalized;
                //transform.position += moveDir * speed * Time.deltaTime;
                t += Time.deltaTime;
                float s = t / duration;
                transform.position = Vector3.Lerp(originTransform.position,
                    destinationTransform.position, curve.Evaluate(s));
                break;
                
            case TransitionState.Transferring:
                timer += Time.deltaTime;
                this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
                if (timer >= 1.0f)
                {
                this.transform.parent = destinationTransform;
                isChild = true;
                state = TransitionState.None;
                this.enabled = false;
                return;
                }
                break;

            default:
                this.enabled = false;
                return;
        }
    }
}

The part I'm using the lerp this time is to move the transform to the target not to make the transform child like in the other question.

From lines 49 to 52 :

t += Time.deltaTime;
float s = t / duration;
transform.position = Vector3.Lerp(originTransform.position,
    destinationTransform.position, curve.Evaluate(s));
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Compare your Lerp expression to the one in your previous question. You stopped using the start transform in the first argument, and instead you're moving relative to your current position, creating a feedback loop. \$\endgroup\$
    – DMGregory
    Aug 11, 2021 at 23:36
  • \$\begingroup\$ Working. what the duration is in units ? It's not seconds because when I set it to 20 it does ease in/out but it's taking maybe 4-5 seconds not 20. and in the end near the target it's not slowing down like it was speeding up slowly to max at the start. In the end the slow down is almost not feeling. maybe the graph of the animnationcurved is not accurate on the out ? \$\endgroup\$
    – Jhon RA
    Aug 12, 2021 at 2:36
  • \$\begingroup\$ Sounds like you should edit your question to show a Minimal Complete Verifiable Example of your current problem. \$\endgroup\$
    – DMGregory
    Aug 12, 2021 at 2:37
  • \$\begingroup\$ I found the right graph and it's working now the ease in/out also the duration is now working fine. last thing I want to do for testing is a pig pong but once. I mean ease in out to the target like now but how do I make that it will ease in out from the target back to the start ? It will move to the target and then back to start and stay at the start. not ping pong all the time only once. \$\endgroup\$
    – Jhon RA
    Aug 12, 2021 at 2:47
  • \$\begingroup\$ If you've solved your problem, post your solution as an answer. If you need help with something, edit your question to show what you need help with. \$\endgroup\$
    – DMGregory
    Aug 12, 2021 at 2:49

1 Answer 1

0
\$\begingroup\$

Solution :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveToTarget : MonoBehaviour
{
    public enum TransitionState
    {
        None,
        MovingTowards,
        Transferring
    }

    public Transform destinationTransform;
    public float speed;
    public float lerpTime;
    public bool isChild = false;

    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public Transform start;
    public Transform end;
    public float duration = 10.0f;
    private float t;

    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;
    private Vector3 originTrans;

    void Start()
    {
        t = 0.0f;

        curve.postWrapMode = WrapMode.PingPong;
        originTrans = transform.position;
    }

    void Update()
    {
        switch (state)
        {
            case TransitionState.MovingTowards:
                var v = destinationTransform.position - transform.position;
                if (v.magnitude < 0.001f)
                {
                    state = TransitionState.Transferring;
                    originTransform = destinationTransform;
                    timer = 0;
                    return;
                }
                Vector3 moveDir = v.normalized;
                t += Time.deltaTime;
                float s = t / duration;
                transform.position = Vector3.Lerp(originTrans,
                    destinationTransform.position, curve.Evaluate(s));
                break;

            case TransitionState.Transferring:
                timer += Time.deltaTime;
                this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
                if (timer >= 1.0f)
                {
                    this.transform.parent = destinationTransform;
                    isChild = true;
                    state = TransitionState.None;
                    this.enabled = false;
                    return;
                }
                break;

            default:
                this.enabled = false;
                return;
        }
    }
}

and the graph in the editor :

I selected the last one to the right on the bottom graphs options :

AnimationCurve Graph

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This answer would be better if it explained what you changed and why that solves the problem. \$\endgroup\$
    – DMGregory
    Aug 12, 2021 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .