Game Development Reference
In-Depth Information
The problem is that since we destroy both the bullet and the alien upon collision, we
need to delay the destruction of the bullet until the audio clip is played; otherwise no
sound will be played because the bullets gets destroyed before the audio clip is ac-
tually played.
There are many ways to avoid this problem, here is our solution.
1. Once you have attached the
Audio Source
component to the
PLBullet
pre-
fab and dragged the audio clip into the
Audio Source
slot, we can then make
some modifications to the
ControlPLBullet
script. This is the logic we im-
plemented.
2. When the bullet collides with an alien, we play the enemy destroyed audio
clip and destroy the alien enemy. We also disable the
Collider
component
attached to the bullet and put the bullet itself on the layer of assets not to be
rendered by the camera, so that the bullet disappears from the scene.
3. Finally, we set a Boolean variable to
true
, so that in the
Update()
function
we can check at every frame the value of the Boolean variable. If the Boolean
variable is set to
true
and the audio component attached to the bullet is not
playing, it means we can finally destroy the bullet.
4. This is the updated code lines of the
ControlPLBullet
script:
#pragma strict
//var used if the bullet must be
destroyed in the Update()
//function
var bMustDestroy:boolean;
function Start () {
//set the initial boolean value to
false
bMustDestroy=false;
}
function Update () {
//move bullet up once created 2