Pacing delays in 2d turn based roguelike

In a top down, 2d, turn based roguelike in Unity3d 5 I am having an issue with game pacing. I am using a very simple state machine to control execution but I want enemies that all move in the same turn to do so with a pause between each one if they are in the players line of sight so that animations and sound have time to occur without overlapping (Especially sounds). The only issue I am having is getting the actual pause to occur without affecting animations or camera movement (The camera may be sliding to follow a player movement and it was stopping). I only need about a 100ms pause before moving to the next enemy.

The enemies are taking their actions in a foreach loop in an EnemyManager script

 private void takeAllEnemyActions()
  {
  // Cycle through all enemies and have them perform their actions
  if (Enemies != null && Enemies.Length > 0)
  {
  foreach (GameObject enemy in Enemies)
  if (enemy.GetComponent<Enemy>().NextMoveTick <= GameManager.Instance.CurrentTick)
  enemy.GetComponent<Enemy>().TakeEnemyActions();
  }
  GameManager.States.NextPlayState();
  }

I tried creating a routine to invoke each enemy to act but the issue there was that execution of other enemies continued.

I tried using a coroutine but again the issue was the game would move on into the next state.

I even tried a while-do loop using Time.RealTimeSinceStartup just to see what it would do (I know, a very VERY bad idea)

I’m certain this is simple and I am just having a brain cramp but I’ve been trying things and google-binging for hours with no progress. Thank goddness for git to roll back.

Does anyone have any suggestions on the best way to go? I’m not needing someone to write my code, I just need pointed in the right direction.

Thanks

The best way in Unity is to use a coroutine. Essentially your main combat movement “manager” script tells every enemy to figure out where they want to go, and then it aggregates them into a list of “things to move.”

Then it fires off a coroutine that:

  • disables user input (apart from perhaps a “SKIP” button)
  • proceeds iteratively through the list of characters, actually moving them each one at a time
  • waits the requisite amount of time for each character (here you could even query the character, “have you done moving yet?”)
  • once all the characters have moved, restore user input.

Hope that helps. Coroutines are key for this sort of behavior-over-time stuff in Unity, and once you master them, you’ll find bazillions of uses for them in games.