Home > Enterprise >  Unity 2D IEnumerator not working / processing the WaitForSeconds
Unity 2D IEnumerator not working / processing the WaitForSeconds

Time:10-04

[Update] here is my code that I am using. this is a in a if(Enemy != Chasing) which is in my update method.

this code is designated to have the enemy pick a set rotation of 90, 180, -90, 360 and then move in that direction for a set amount of time. then do it all over again. but when I run this all it does is

    private IEnumerator RomeCO() {
        int MovePick = Random.Range(1, 4);
        
        yield return new WaitForSecondsRealtime(1)

        switch (MovePick)
        {
            case 1:
            // TODO 
            // turn / face 0 for up
            Rb.SetRotation(0);
            yield return new WaitForSecondsRealtime(1);
            // give movement in that dirrection
            transform.Translate(new Vector2(0f, 1f) * EnemySpeed * Time.deltaTime); // up

            break;

            case 2:
            // TODO 
            // turn / face 180 for down 
            Rb.SetRotation(180);
            yield return new WaitForSecondsRealtime(1);
            // give movement in that dirrection
            transform.Translate(new Vector2(0f, -1f) * EnemySpeed * Time.deltaTime); // down
            break;

            case 3:
            // TODO 
            // turn / face 90 for left
            Rb.SetRotation(90);
            yield return new WaitForSecondsRealtime(1);
            // give movement in that dirrection
            transform.Translate(new Vector2(-1f, 0f) * EnemySpeed * Time.deltaTime); // left 
            
            break;

            case 4:
            // TODO 
            // turn / face -90 for right
            Rb.SetRotation(-90);
            yield return new WaitForSecondsRealtime(1);
            // give movement in that dirrection
            transform.Translate(new Vector2(1f, 0f) * EnemySpeed * Time.deltaTime); // right
            break;
        }


        GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        GetComponent<Rigidbody2D>().angularVelocity = 0.0f;

    


    }

CodePudding user response:

As mentioned in the comments, the reason you are observing a small translation is that you multiply by time of last frame (at 60fps this is equivalent to ~0.016.

Just multiplying by Time.deltaTime does not make the parameter animate. There's various ways to make the value transition from one to another, you can start a Coroutine, you could animate it in Update when a flag is up, or when target value is different from current, or use a plugin like LeanTween / DoTween etc.

CodePudding user response:

First of all you are doing stuff in Start which is called exactly ONCE.

You rather want to put it into Update which is called repeatedly every frame.


and then your first attempt

flaot RandomXmin = Random.Range(1, 2) 
transform.position = new Vector3(RandomXmin, 0) * Time.deltaTime;

will hard set your object to a position with RandomXmin * Time.deltaTime, 0, 0. Where RandomXmin is some float value between 1 and 2 and Time.deltaTime basically the time passed since the last frame. Within Start this an have invalid values anyway but would usually be really small. (e.g. 60 fps => 1/60 = 0.001666..)

And the second one

transform.Translate(Vector3.up * 2 * Time.deltaTime);

is already more suitable for moving your object with a constant velocity of 2 units per second upwards .. however still you call it only once.


So it is up to you but you would probably do something like e.g.

private void Update()
{
    transform.Translate(Vector3.up * 2 * Time.deltaTime);
    // basically equals
    //transform.localPosition  = Vector3.up * 2 * Time.deltaTime;
}
  • Related