Thursday, May 7, 2015

Week 8 - Syncing a Countdown Sound With Game

With everything loading at the beginning of the level, it can be a bit tricky to make sure the sound plays at exactly the right moment.

Eventually I got into our StartTimer script that controls all the logic for displaying the countdown text. At first, I started the sound in the Start method of the script, which worked really well most of the time. However some later code changes seemed to make this less precise and the sound would play well before the text started it's countdown.

To get around this I made a state variable to keep track of what the sound was doing, and added logic to FixedUpdate to count for that state:


        
public class StartTimer : MonoBehaviour, Startable, Pausable
{
  ....
  bool hasStarted = false;
  ....
  
  void FixedUpdate()
  {
    ....
    if(!hasStarted)
    {
      hasStarted=true;
      soundController.PlayLevelStartCountdown();
    }
    ....
}


I was also able to use some of that logic to make sure the sound played correctly during pausing to ensure everything felt normal.

The beat ended up matching with the countdown really well!

No comments:

Post a Comment