Implementing the Cucumber Beetle audio

In this section, we will configure the Cucumber Beetle 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 beetle prefab in the Project panel's Assets | Prefabs folder. If you have more than one prefab, be sure to use the one that is used in your game.
  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 Beetles 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 eating;
public AudioClip attack;
public AudioClip die;
  1. Add the following statement to the Start() method:
      audioSource = GetComponent<AudioSource> ();
  1. Edit the OnTriggerEnter() method as shown here. You will see two audioSource.PlayOneShot() statements, one each for the eating and attack audio clips:
      void OnTriggerEnter(Collider theObject) {
if (theObject.gameObject.CompareTag ("Cucumber")) {
cucumberToDestroy = theObject.gameObject;
BeetlePatrol.isEating = true;
animator.Play ("Eating on Ground");
audioSource.PlayOneShot (eating);
StartCoroutine ("DestroyCucumber");
} else if (theObject.gameObject.CompareTag ("Cherry")) {
_ptsManager = GameObject.Find
("Score_Value").GetComponent<PointsManager>();
PointsManager.currentScore = PointsManager.currentScore + 10;
BeetlePatrol.isAttacking = true;
cherryHit = true;
animator.Play ("Stand");
audioSource.PlayOneShot (attack);
}
}
  1. Edit the DestroySelfOnGround() method, shown as follows. Here you can see that we added the audioSource.PlayOneShot(die) statement:
      IEnumerator DestroySelfOnGround() {
yield return new WaitForSecondsRealtime (4);
animator.Play ("Die on Ground");
audioSource.PlayOneShot (die);
Destroy (this.gameObject, 4);
}
  1. Edit the DestroySelfStanding() method as shown in the following code block. Here, you can see we added the audioSource.PlayOneShot(die) statement:
      IEnumerator DestroySelfStanding() {
yield return new WaitForSecondsRealtime (4);
animator.Play ("Die Standing");
audioSource.PlayOneShot (die);
Destroy (this.gameObject, 4);
cherryHit = false;
}

Now that the scripting task is complete, we need to assign the designated audio clips to the variables we created:

  1. In the Inspector panel, scroll until you see the Beetle NPC (Script) component.
  2. Drag the eating audio clip from the Project panel's Assets | Audio folder to the appropriate spot in the Beetle NPC (Script) component.
  3. Repeat step 11 for the attack and beetle_die audio clips. Your Beetle NPC (Script) component should look like the following:

All that's left is for you to test this new functionality by playing the game.

..................Content has been hidden....................

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