After Coroutine the position is restarted

Hi everyone!

I’ve a problem and I haven’t found any solution.

I’ve this coroutine:

public IEnumerator moveObjectBySquat()
    {
        Vector3 source = transform.position;
        Vector3 target = new Vector3 (source.x, source.y - 0.5f, 0f);

        float startTime = Time.time;
        while(Time.time < startTime + OVER_TIME)
        {
            transform.position = Vector3.Lerp(source, target, (Time.time - startTime)/OVER_TIME);
            yield return null;
        }
  transform.position=target;
}

And I call the coroutine from the Update method:

void Update(){
if(booleanVar){
         StartCoroutine(moveObjectToSourcePosition ());
  }
    }

It works correctly, but when coroutine is finished the gameObject restarts to their original position automatically…

Any idea?

I don’t really see any problems with this. Is the object’s position ever set by anything else? Is the object physically dynamic?

Are you sure your coroutine is lunched only one time ? Beacause in your coroutine i don’t see booleanVar = false; You should name your properties too :slight_smile:

@CrymX that’s what I thought too - looks like it’s launching hundreds of coroutines

Before lunching the coroutine (to be sure) you cant stop it with StopCoroutine

I’d suggest DOTween to move your game object over time. It’s simple as:

transform.DOMove(new Vector3(2,3,4),  1.0);