'Why when pressing the b key the transform is moving backward but not in the same duration when moving forward?

For example the duration is set to 10 seconds. When the flag bool go is true the transform will move in 10 seconds to the target(destinationTransform). but when I press the b key in the middle while the transform is moving forward the transform start moving backward to the original position but not in 10 seconds but faster.

Is that because the distance is not the same as when it's moving forward ?

I'm trying to do that when i press the b key in the middle the transform will move backward but in the duration time no matter the distance. maybe i messed the lerp code in the backward state ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cinemachine;

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

    public Transform destinationTransform;
    public bool isChild = false;
    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public float duration = 10.0f;
    public bool go = false;

    private float t;
    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;
    private Vector3 originPosition;
    private SphereCollider col;
    private bool enableCollider = true;
    private bool updateOriginPosition = true;

    void Start()
    {
        t = 0.0f;

        curve.postWrapMode = WrapMode.Once;
        originPosition = transform.position;
        col = GetComponent<SphereCollider>();
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.B))
        {
            t = 0.0f;
            state = TransitionState.MovingBackward;
        }

        if (go)
        {
            if (updateOriginPosition == true)
            {
                originPosition = transform.position;
                updateOriginPosition = false;
            }

            if (col != null && enableCollider)
            {
                //col.enabled = false;
                //transform.GetComponent<Rigidbody>().isKinematic = true;

                Destroy(col);
                Destroy(transform.GetComponent<Rigidbody>());
                //transform.GetComponent<InteractableItem>().enabledInteraction = false;

                enableCollider = false;
            }

            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(originPosition,
                        destinationTransform.position, curve.Evaluate(s));

                    break;

                case TransitionState.MovingBackward:
                    t += Time.deltaTime;
                    float s1 = t / duration;

                    transform.position = Vector3.Lerp(transform.position,
                        transform.parent.position,
                        curve.Evaluate(s1));

                    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;
                        transform.localPosition = new Vector3(0, 0, 0);
                        isChild = true;

                        go = false;

                        state = TransitionState.None;
                        this.enabled = false;
                        return;
                    }

                    break;

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


Solution 1:[1]

The solution is first to update the originPosition in the Update when pressing the B key :

if(Input.GetKeyDown(KeyCode.B))
        {
            t = 0.0f;
            originPosition = transform.position;
            state = TransitionState.MovingBackward;
        }

Then in the MovingBackward state to set the originPosition instead the transform.position.

case TransitionState.MovingBackward:
                    t += Time.deltaTime;
                    float s1 = t / duration;

                    transform.position = Vector3.Lerp(originPosition,
                        transform.parent.position,
                        curve.Evaluate(s1));

                    break;

The complete code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cinemachine;

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

    public Transform destinationTransform;
    public bool isChild = false;
    public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    public float duration = 10.0f;
    public bool go = false;

    private float t;
    private Transform originTransform;
    private float timer;
    private TransitionState state = TransitionState.MovingTowards;
    private Vector3 originPosition;
    private SphereCollider col;
    private bool enableCollider = true;
    private bool updateOriginPosition = true;

    void Start()
    {
        t = 0.0f;

        curve.postWrapMode = WrapMode.Once;
        originPosition = transform.position;
        col = GetComponent<SphereCollider>();
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.B))
        {
            t = 0.0f;
            originPosition = transform.position;
            state = TransitionState.MovingBackward;
        }

        if (go)
        {
            if (updateOriginPosition == true)
            {
                originPosition = transform.position;
                updateOriginPosition = false;
            }

            if (col != null && enableCollider)
            {
                Destroy(col);
                Destroy(transform.GetComponent<Rigidbody>());
                enableCollider = false;
            }

            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(originPosition,
                        destinationTransform.position, curve.Evaluate(s));

                    break;

                case TransitionState.MovingBackward:
                    t += Time.deltaTime;
                    float s1 = t / duration;

                    transform.position = Vector3.Lerp(originPosition,
                        transform.parent.position,
                        curve.Evaluate(s1));

                    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;
                        transform.localPosition = new Vector3(0, 0, 0);
                        isChild = true;

                        go = false;

                        state = TransitionState.None;
                        this.enabled = false;
                        return;
                    }

                    break;

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Mcnize