Movement via ienumorator not working

So I have this enemy that I try to move via Ienumorator, It is a simple movement moving from one point to another and back again. Nut after moving to one point and back again it just stands there. I can see the “end” transform in the editor and it just updates once, Why does this happen?

public class PupilAttack : MonoBehaviour
    
public float movementSpeed;
public GameObject enemyFront, enemyBack;
Transform end;

void Update ()
    {
        if (transform.position.z == enemyBack.transform.position.z)
        {
            end = enemyFront.transform;

            StartCoroutine(Movement(end));           
        }
        if (transform.position.z == enemyFront.transform.position.z)
        {
            end = enemyBack.transform;
            StartCoroutine(Movement(end));
        }
    }


    IEnumerator Movement(Transform end)
    {

        
        while (transform.position.z != end.position.z )
        {

            transform.position = Vector3.MoveTowards(transform.position,
            new Vector3(end.position.x, transform.position.y, end.position.z),
            movementSpeed * Time.deltaTime);
            yield return null;
        }

    }

You generally don’t want to compare floating point values for equality. Due to rounding and other things, you will almost never have two values that are equal when you’re iterating them.

Instead you want to check for a value that’s close to another. For this you can use Mathf.Approximately.

You will need to use this on lines 9, 15, and 27.

Tried it on line 9,15 (didnt know how to write it with ! on line 27). The change on line 9 and 15 should be enough to see a change on the “end” transform in the inspector but it was still the same. The enemy character always makes two turns, no more no less.

If your positions are fixed you can use a lerp function.

    public float movementDuration;
    public Transform enemyFront, enemyBack;

    private float timeMovementStarted;
    private bool isFrontMovement;

    private void Start()
    {
        timeMovementStarted = Time.time;
        StartCoroutine(Movement());
    }

    IEnumerator Movement ()
    {
        while (true)
        {
            float timeSinceMovementStarted = Time.time - timeMovementStarted;
            float movementRate = timeSinceMovementStarted / movementDuration;

            if (movementRate <= 1f)
            {
                if (isFrontMovement)
                {
                    transform.position = Vector3.Lerp(enemyBack.position, enemyFront.position, movementRate);
                }
                else
                {
                    transform.position = Vector3.Lerp(enemyFront.position, enemyBack.position, movementRate);
                }
            }
            else
            {                
                yield return new WaitForSeconds(2f);
                isFrontMovement = !isFrontMovement;
                timeMovementStarted = Time.time;
            }

            yield return null;
        }
    }

The movement speed is controlled by the movementDuration variable, and the line yield return new WaitForSeconds(2f) makes the character stops for 2 seconds before change the movement direction.

It is something with how the Coroutines work. I added “StopAllCoroutines()” on line 35 and now it works without Mathf.Approximately. Seems like the Coroutines dont stop after they are finished. If anybody know how this work, plz share.