Graphics Reference
In-Depth Information
tailored to suit the projectile. For example, perhaps a small laser blast has a less powerful
sound effect than a larger blast, or perhaps a green laser sounds different from a red laser.
In the example project for this topic, the code to play a iring sound from the pro-
jectile will already be in place. The code resides within the ProjectileController.cs script.
Note that placing the call to play the projectile sound in the Awake() or Start() func-
tion may cause problems because of the time it takes to initialize versus the time it takes
for the script creating the projectile to get around to position it correctly in the 3D world.
To ensure that the projectile will have been positioned correctly when the audio is played,
this code goes into the Update() loop with a simple Boolean flag to stop the sound getting
played more than once.
The variable declarations related to the firing effect, to go with the other declarations
at the beginning of the script, are as follows:
private bool didPlaySound;
private int whichSoundToPlayOnStart= 0;
The code to play the sound is placed at the top of the Update() function. It checks first
to make sure that the sound has not already been played and then makes a call out to the
BaseSoundController static variable Instance. The PlaySoundByIndex parameters are the
contents of the integer whichSoundToPlayOnStart variable and the projectile transform's
position. After the sound call, didPlaySound is set to true to prevent duplicate calls to play:
if(!didPlaySound)
{
BaseSoundController.Instance.PlaySoundByIndex(whichSoundToPlayOnStart,
myTransform.position);
didPlaySound=true;
}
Search WWH ::




Custom Search