Bounce sound effect

Before moving on, let's add a sound effect when the ball bounces. If you've installed the BallGameArt assets, there's a sound effects folder with a sound named bounce. We'll play the clip when the ball collides with anything. Let's script it now and we'll explain colliders more when we add the GoalCollider later.

In your Project Assets/ARPlayBall/Scripts folder create a new C# script named PlaySoundOnHit and open it for editing:

File: PlaySoundOnHit.cs
using UnityEngine; [RequireComponent(typeof(AudioSource))] public class PlaySoundOnHit : MonoBehaviour { public AudioClip clip; private AudioSource source; void Start() { source = GetComponent<AudioSource>(); source.spatialBlend = 1.0f; source.playOnAwake = false; source.clip = clip; } void OnCollisionEnter() //Plays Sound Whenever collision detected { source.Play(); } }

Because we include the directive to require an AudioSource component, when we add this script to the ball, Unity will also add an AudioSource component. Let's do this now:

  1. Drag the PlaySoundOnHit script onto the Ball as a component.
  2. From the Project Assets/BallGameArt/Sounds folder, drag Bounce onto the Clip slot.

Bouncy, bouncy, bouncy ball!

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

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