First of all, I’d like to apologize if my English isn’t very good but I’ll try my best to make it easy to understand. I’m making a 2D game where the player controls some tiles on a grid by using some movement cards that tell where the cards are going to go. I want the card to move 1 unit every x seconds (where to depends on the card), so in order to achieve that I tried using an IEnumerator with a for loop inside (I only put code on the “Up” part to check if everything was working as intended):
IEnumerator AplicarMovimento()
{
for (int i = 0; i < Peças.Length; i++)
{
if (Peças *== "Up")*
{
transform.position = new Vector2(transform.position.x, transform.position.y + 1);
}
else if (Peças == “Down”)
{
}
else if (Peças == “Left”)
{
}
else if (Peças == “Right”)
{
}
yield return new WaitForSeconds(TimeStep);
}
canMove = false;
}
The problem that I have is that the for loop is applying the vector while the TimeStep doesn’t reach its destined time, and what I wanted was it applies the vector ONCE and then waits the x seconds (TimeStep), and then it would go into the next “i” in the for-loop. Is there any way to achieve that? Am I missing something?
Any help would be appreciated. Thank you for your time!
you can exit a loop using break or return, break will exit the loop and return will exit the function.
– logicandchaos