Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I properly use an IEnumerator?

I want to put a delay on the characters movement every time an arrow button is pressed so the player can’t just spam move, but for some reason my IEnumerator function doesn’t work and it doesn’t wait. I’m not sure if I’m using it wrong since I’m getting back into unity. I’m using version 2019.3.14f1 if that helps.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;

public class Movement : MonoBehaviour
{
    void Start()
    {
        transform.position = new Vector3(0, 0, 0);
    }

    void Update()
    {
        CalculateMovement();
    }

    private IEnumerator delay()
    {
        yield return new WaitForSeconds(1);
    }

    void CalculateMovement() 
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            if (transform.position.y != 4)
            {
                StartCoroutine(delay());
                transform.position += new Vector3(0, 1, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (transform.position.y != -4)
            {
                StartCoroutine(delay());
                transform.position += new Vector3(0, -1, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (transform.position.x != 4)
            {
                StartCoroutine(delay());
                transform.position += new Vector3(1, 0, 0);
            }
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (transform.position.x != -4)
            {
                StartCoroutine(delay());
                transform.position += new Vector3(-1, 0, 0);
            }
        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The coroutine part is just this:

private IEnumerator delay()
{
    yield return new WaitForSeconds(1);
}

You can’t call StartCoroutine() on it and then do your work expecting it to be delayed, the delayed part has to be part of your coroutine, like this:

private IEnumerator BetterNamePlease()
{
    yield return new WaitForSeconds(1);
    transform.position += new Vector3(0, 1, 0); // use parameters here
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading