Search code examples
c#unity-game-enginemove

How can I move a object twice in a IEnumerator function


I want to move my object from one place to another at certain time, after a while i want to move it to another place. can I do it in one IEnumerator function?

Here is my code, btw, the start here is the start trigger, which works fine.

void Start()
{
    StartCoroutine(Begin());
}

IEnumerator Begin()
{
    while (!start)
    {
        yield return null;
    }
    StartCoroutine(goDown());
}

IEnumerator goDown()
{
    yield return new WaitForSeconds(62);

    while ( transform.position.x < -1.4f 
        || transform.position.y > -1.3f 
        || transform.position.z < 9.33f)
    {

        if(transform.position.x < -1.4f)
        {
            transform.position = new Vector3(transform.position.x + Time.deltaTime * 0.6f, 
                transform.position.y, transform.position.z);
        }

        if (transform.position.y > 1.3f)
        {
            transform.position = new Vector3(transform.position.x, 
                transform.position.y - Time.deltaTime * 0.6f, 
                transform.position.z);
        }

        if (transform.position.z < 9.33f)
        {
            transform.position = new Vector3(transform.position.x , 
                transform.position.y, 
                transform.position.z + Time.deltaTime * 0.6f);
        }

        yield return null;
    }

    yield return new WaitForSeconds(25);

    while (transform.position.x > -8.79f 
        || transform.position.y < 1.67f 
        || transform.position.z < 9.86f)
    {

        if (transform.position.x > -8.79f)
        {
            transform.position = new Vector3(transform.position.x - Time.deltaTime * 0.6f, transform.position.y, transform.position.z);
        }

        if (transform.position.y < 1.67f)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y + Time.deltaTime * 0.6f, transform.position.z);
        }

        if (transform.position.z < 9.86f)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - Time.deltaTime * 0.6f);
        }

        yield return null;
    }
}

I did that, the object can only move to the first place but cannot mover to the next. I don't know whats wrong with it?


Solution

  • It's because your while loop and your if condition don't match:

        || transform.position.y > -1.3f 
    

    is different than

        if (transform.position.y > 1.3f)
    

    So your coroutine gets stuck in the first loop because the transform's y is stuck between -1.3 and 1.3.

    As a sidenode, you can assign the transform's current goal to a variable and use Vector3.MoveTowards to avoid this sort of copy-paste error:

    IEnumerator goDown()
    {
        yield return new WaitForSeconds(62);
    
        Vector3 goal = new Vector3(-1.4f, -1.3f, 9.33f);
    
        while (transform.position != goal) 
        {
            transform.position = Vector3.MoveTowards(transform.position, goal, 
                                                     1.03f * Time.deltaTime);
            yield return null;
        }
    
        yield return new WaitForSeconds(25);
    
        goal = new Vector3(-8.79f, 1.67f, 9.86f);
    
        while (transform.position != goal) 
        {
            transform.position = Vector3.MoveTowards(transform.position, goal, 
                                                     1.03f * Time.deltaTime);
            yield return null;
        }
    }