Implementing the Cucumber Man audio

In this section, we will configure the Cucumber Man prefab so that it supports audio when the Cucumber Beetles eat, when they run while standing, and when they die. Here are the steps:

  1. Select the Cucumber Man in the Hierarchy panel.
  2. In the Inspector panel, scroll to the bottom and click the Add Component button.
  3. Select Audio | Audio Source.
  4. Uncheck the Play On Awake box.

Normally, we would assign an AudioClip to our Audio Source component. Since our Cucumber Man will have more than one audio clip, we will not assign one here. 

Our next step is to edit the BeetleNPC script. Open that script file and make the following modifications:

  1. Add the following member variables:
      public AudioSource audioSource;
public AudioClip dying;
public AudioClip respawning;

public AudioClip gameOver;
  1. Create a Start() method, shown as follows:
      void Start () {
audioSource = GetComponent<AudioSource> ();
}
  1. Edit the Update() method, as shown here, so that it includes the three audioSource.PlayOneShot() statements:
      if (livesRemaining == 2) {
Destroy (GameObject.Find ("Life3"));
anim = GetComponent<Animator> ();
anim.Play ("CM_Die");
audioSource.PlayOneShot (dying);
StartCoroutine ("ReSpawnCucumberMan");
}
if (livesRemaining == 1) {
Destroy (GameObject.Find ("Life2"));
anim = GetComponent<Animator> ();
anim.Play ("CM_Die");
audioSource.PlayOneShot (dying);
StartCoroutine ("ReSpawnCucumberMan");
}
if (livesRemaining == 0) {
Destroy (GameObject.Find ("Life1"));
anim = GetComponent<Animator> ();
anim.Play ("CM_Die");
audioSource.PlayOneShot (gameOver);
}
  1. Edit the ReSpawnCucumberMan() method as shown here. You can see that we added the audioSource.PlayOneShot() statement:
      IEnumerator ReSpawnCucumberMan() {
int randomNumber = Random.Range (1, 4);
if (randomNumber == 1) {
yield return new WaitForSecondsRealtime (4);
this.transform.position = SpawnPad1.transform.position;
} else if (randomNumber == 2) {
yield return new WaitForSecondsRealtime (4);
this.transform.position = SpawnPad2.transform.position;
} else {
yield return new WaitForSecondsRealtime (4);
this.transform.position = SpawnPad3.transform.position;
}
audioSource.PlayOneShot (respawning);
anim.Play ("CM_Idle");
}

Now that our script changes for the CucumberManManager script file are complete, we need to assign the designated audio clips to the variables we created. Here are the steps:

  1. In the Inspector panel, scroll until you see the Cucumber Man Manager (Script) component.
  2. Drag the cm_die audio clip from the Project panel's Assets | Audio folder to the appropriate spot in the Cucumber Man Manager (Script) component.
  3. Repeat step 10 for the respawn and game_over audio clips. Your Cucumber Man Manager (Script) component should look like this:

So far, we have taking care of the dying, respawning, and game over audio clips. Next, we will handle the jumping and throwing audio clips:

  1. Open the PlayerController script for editing.
  2. Add the following member variables:
      public AudioSource audioSource;
public AudioClip jumping;
public AudioClip throwing;
  1. Add the following statement to the Start() method:
       audioSource = GetComponent<AudioSource> (); 
  1. Add the following statement to the beginning of the Jump() method:
       audioSource.PlayOneShot (jumping); 
  1. Add the following statement to the beginning of the Throw() method:
       audioSource.PlayOneShot (throwing); 

Now that our script changes for the PlayerController script file are complete, we need to assign the designated audio clips to the variables we created. Here are the steps:

  1. In the Inspector panel, scroll until you see the Player Controller (Script) component.
  2. Drag the jump audio clip from the Project panel's Assets | Audio folder to the appropriate spot in the Player Controller (Script) component.

 

  1. Drag the throw audio clip from the Project pane's Assets | Audio folder to the appropriate spot in the Player Controller (Script) component. Your Player Controller (Script) component should look like the following:

The last audio clip for us to implement is the victory clip. We will start by editing the VictoryManager script. Open that script file and make the following modifications:

  1. Add the following member variables:
      public AudioSource audioSource;
public AudioClip victory;
  1. Create a  Start() method, as shown here:
      void Start () {
audioSource = GetComponent<AudioSource> ();
}
  1. Edit the Update() method, shown as follows. You will notice that we only added an audio clip playback for the victory condition, as we've already taken care of the defeat condition:
      void Update () {
beetleCount = BeetleManager.currentBeetleCount;
if (beetleCount == 0) {
Victory.text = ("You won!");
audioSource.PlayOneShot (victory);
}

cucumberCount = CucumberManager.currentCucumberCount;

if (cucumberCount == 0) {
Victory.text = ("You Lost!");
}
}

Now that our script changes for the VictoryManager script file are completed, we need to assign the victory audio clip to the variable we created. Here are the steps:

  1. In the Hierarchy panel, select HUD_Canvas | EndOfGame | Victory.
  2. In the Inspector panel, scroll until you see the Victory Manager (Script) component.
  3. Drag the victory audio clip from the Project panel's Assets | Audio folder to the appropriate spot in the Victory Manager (Script) component. Your Victory Manager (Script) component should look like the following screenshot:

You are now ready to test this new functionality by playing the game.

This is an excellent time to save both your scene and your project. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset