Hey, how would I go about doing something after a sound clip has finishing playing. In my case when the user pressed r he will reload the gun which is just setting the magazine count 1 lower and setting the current bullets back to the max bullet count and it also plays a reload sound. Is there a way I can get the reloading of the gun to happen after the sound is finished playing?
Can you be a little more specific. You say you want a sound to play automatically after another has finished, and in the next sentence you say the user must hit r to reload? So do you want to play the reload sound when the user hits r, or when the magazine runs out? If it’s the former, you can just trigger it when you check for r. Simply stop the current sound and play the reload sound. If it’s the latter you can check when the sound has stopped using isPlaying().
I know that I can use isPlaying but the code inside reload is only ever called when you press the “r” key. So, the code won’t loop back over itself to check whether or not the sound is finished. The sequence of events that I wish to happen is when the player pressed the “r” key for the reload sound to play and once it has finished the steps taken to reload the gun carried out.
At the moment I have:
if (Input.GetKeyDown("r"))
{
if (currentMagazineCount > 0)
{
source.PlayOneShot(reload);
currentBulletCount = maxBulletCount;
currentMagazineCount -= 1;
}
}
The problem with this code is that I can start firing the gun again before the reloading sound is finished, do you get what I mean? I want the reload sound to finish before the player is allowed to fire his gun again.
I get it now. Why not something like the following. Once you start reloading set a boolean to true. Then make sure that you don’t play any gun fire sound until the reloading bool is false. Use the length of the reload clip to determine how long isReloading remains true. Should work. Seems like a simple enough solution.
bool reloading = false;
(...)
if (Input.GetKeyDown("r"))
{
if (currentMagazineCount > 0)
{
source.PlayOneShot(reload);
currentBulletCount = maxBulletCount;
currentMagazineCount -= 1;
reloading = true;
Invoke("ReloadingGun", reload.length);
}
}
(...)
void ReloadingGun()
{
//gun fire sound is only allowed when reloading is false;
reloading= false;
}