How to Delay Without Using Coroutine

Hi all

I am new bie to unity. In my project i am using animation. The enemy should come after certain seconds to the player after that the enemy should destroy with some seconds. i am using coroutine concept.

public IEnumerator RobotAnimation()
{

yield return new WaitForSeconds(1.50f);
if(test==false)
{

float step=speed* Time.deltaTime;
Robot1.transform.position = Vector3.MoveTowards(Robot1.transform.position, Player.transform.position, step);
yield return new WaitForSeconds(2.6f);
Robot.CrossFade(“Destroy”);

test=true;
updatecheck=false;
}
}

void Update()
{
if(updatecheck==true)
{
StartCoroutine(RobotAnimation());
}
}

I have done that. I have no problem with that. But i have found the numerous times calling in update. i think it will cause issues because of numerous time calling in update.

Is there any other way to delay. My Project the enemy should wait for 1.50 seconds after that it should walk after 2.6 seconds the enemy should destroy.

how can i make the delay without using coroutine. or any other ideas welcome

thanks in advance…

Hi Alexander! Just a reminder, use the code tags to post code.

public IEnumerator RobotAnimation()
{
    yield return new WaitForSeconds(1.50f);
 
    if( test == false)
    {
        float step=speed* Time.deltaTime;
        Robot1.transform.position = Vector3.MoveTowards(Robot1.transform.position, Player.transform.position, step);
        yield return new WaitForSeconds(2.6f);
        Robot.CrossFade("Destroy");

        test=true;
        updatecheck=false;
    }
}


void Update()
{
    if(updatecheck==true)
    {
        StartCoroutine(RobotAnimation());
    }
}

The Update method is being called every frame, it is clearly not what you want at the moment. Perhaps start your RobotAnimation routine in the Start method.

Actually, if you read the manual on the Start method you can see it is possible to just make that a coroutine.

IEnumerator Start()
{
        yield return new WaitForSeconds(1.5f);
        float step=speed* Time.deltaTime;
        Robot1.transform.position = Vector3.MoveTowards(Robot1.transform.position, Player.transform.position, step);
        yield return new WaitForSeconds(2.6f);
        Robot.CrossFade("Destroy");
}
1 Like

Coroutines are really easy. To start one (call this ONCE, NOT in Update function) use:

StartCoroutine(YourName());

And then use:

IEnumerator YourName()
    {
        //you can replace 3 with the amount of seconds to wait
        //for a time like 1.2 seconds, use 1.2f (to show it's a float)
        yield return new WaitForSeconds(3);
        Debug.Log("Finished waiting!");
    }
2 Likes

Don’t worry about the Update function being called very often. Many function calls does not necessarily mean a performance issue, if that is your concern (though you might want to keep on eye on your Profiler).

If you don’t want to use coroutines, you would need to keep track of time using your own variables and accumulating the elapsing time using Time.deltaTime in the Update function.

Alternatively, with animations, you could make use of Animation Events, which allows you to manually place events on your animation timeline.Then, when the animation is played and the event is reached, the corresponding function will be called on your MonoBehaviour. Beware that animation events are appropriate for cosmetic usage, such as synchronizing footstep sounds with the animation; it’s not reliable to rely on this system for game logics.

Also, if you want to handle more complex logic later on, you should look up for a programming techniques such as Finite State Machine or Behaviour Tree.

Just a thought. When using code like this you’ll want to be absolutely sure it’s only run once when needed, so:

void Update()
{
    //if(updatecheck==true)
    if (updatecheck)  // Shorter way to say the same
    {
        updatecheck = false;  // < Toggle it here
        StartCoroutine(RobotAnimation());
    }
}
1 Like

Hi Alexander

waiting for several delays using coroutines is really tedious to implement with coroutines. That’s because we created a simple asset (SuperInvoke) to implement delays and sequences in on line of code, to save time and make things easier.

With Super Invoke your code would be:

sequence.AddDelay(1.5f)
        .AddMethod(MoveRobotTowardPlayer)
        .AddDelay(2.6f)
        .AddMethod(DestroyRobot)
        .Run();

Check out this video for more details

Thankyou very much brother i used Ienumeraturs as waiter function using bool value

like function that takes time and bool which it makes true after waiting time