Friday, 23 May 2014

Adding Sound

To begin with, i looked at Unity Answers to find a way of adding sound into the game. I found out how to start and stop sound, ways to loop sound from certain points and trace whether or not the sound was playing. However, i could not figure out how to get the sound file with the project window into the AudioSource variable. After a little digging on the Unity Docs site i found out that you needed to add an Audio Source Component. Unity Docs suggested to create an empty game object for this. This component can then be dragged into the AudioSource variable space in the inspector.
I wanted to have music on the main menu, but i wanted the music to continue if you changed between the main menu and the stats screen, but not when the games are being played. So i created an empty game object on the first scene (the one that never gets returned to) and wrote the following bit of code:

public var PlayMusic : AudioSource;
private var LoopReady : boolean = false;

function Start ()
{
 DontDestroyOnLoad(gameObject);
}

function Update ()
{
 if(Application.loadedLevel == 1 || Application.loadedLevel == 2)
 {
  if(!PlayMusic.isPlaying)
  {
    if(!LoopReady)
   {
     PlayMusic.Play();
     LoopReady = true;
   }else{

    PlayMusic.time = 16.55;
    PlayMusic.Play();
   }
  }
 }else{

  if(PlayMusic.isPlaying)
  {
   PlayMusic.Stop(); 
   LoopReady = false;
  }
 }
}


When the scene is loaded the game object is created but doesnt get destroyed when the scene changes. Next, when the game loads up the main menu (LoadedLevel 1), if the music is not already playing and if it is the first time it is being played, start playing the music from the beginning. When the music stops playing (track ends), the music is played again but from 16.5 seconds in which skips the intro part. If the game changes to a scene that is not the main menu or the stats screen and the music is still playing, it stops and waits for the game to change back to the main menu again.

I wanted to experiment with a different way of coding the music for the games. This is the code i ended up writing:

var PlayMusic : AudioSource;
var currentObject: GameEngine = null;

function Start ()
{
  if(currentObject == null)
  {
   currentObject = GameObject.Find("GameEngine").GetComponent(GameEngine);
  }
  if(currentObject.GamesCompleted == 0)
  {
   DontDestroyOnLoad(gameObject);
   PlayMusic.time = 62.00;
   PlayMusic.Play();
  }
}

function Update ()
{
  if(Application.loadedLevel == 1)
  {
   Destroy(gameObject);
  }

  if(!PlayMusic.isPlaying)
  {
   PlayMusic.time = 62.00;
   PlayMusic.Play();
  }
 
}

This gameobject gets added in on the game loader screen. However, the issue is that the game returns frequently to the game loader screen. So to stop the game from having several music tracks being played over the top of each over, i have the file trace to see if any games have been played yet, if no games have been played, it becomes active otherwise it stays inactive until the scene changes and it is removed. If the game changes back to the main menu, the game object is removed.

On the first pass of the code, because the music is one big file, i tried changing the music based on what game was playing. However, because the games are only 10 seconds or less, it sounded like a mess, so i changed it to this code.

The sound file was created in MilkyTracker by a fellow course mate Scott Taylor.

No comments:

Post a Comment