I’m asking because, actually , AudioSource components cannot play multiple sounds simultaneously.
I made an AudioManager game object which adds an audio source for each sound, according to a public class I created. I am playing all my sounds through that. But since I created that through different tutorials and documentation, I am not very well equipped to answer why they are playing simultaneously but they do…=)
I am aware of the animation events and I used them frequently in this project and that might be the answer to my problem and I might use that at the end but I would like to solve this through code if I can because I can not understand why this is not working.
This is the coroutine in Fire()
IEnumerator RapidFire()
{
while (true)
{
if (pUpMan.mGunOne == true)
{
aM.StopSound("PlayerGun3Loop");
if (pUpMan.damageUpIsActive)
{
GameObject laserP = Instantiate(mGunBulletPowerdUp, firePosition.position, Quaternion.identity) as GameObject;
Debug.Log("PowerdUp");
laserP.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
aM.PlayOneShot("DamageUp");
animatorMuzzleFlash.SetFloat("Muzzle", 0.6f);
//float periodNewOne = 1f;
//projectileFiringPeriod = periodNewOne;
}
else
{
GameObject laser = Instantiate(mGunBullet, firePosition.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
aM.PlayOneShot("PlayerGun");
animatorMuzzleFlash.SetFloat("Muzzle", 0.6f);
//float periodNewOne = 1f;
//projectileFiringPeriod = periodNewOne;
}
}
if (pUpMan.mGunTwo == true)
{
if (pUpMan.damageUpIsActive)
{
GameObject laser = Instantiate(mGunBulletPowerdUp, firePosition2.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("DamageUp");
GameObject laser2 = Instantiate(mGunBulletPowerdUp, firePosition3.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("DamageUp");
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser2.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
animatorMuzzleFlash2.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash3.SetFloat("Muzzle", 0.6f);
//yield return new WaitForSeconds(0.1f);
}
else
{
GameObject laser = Instantiate(mGunBullet, firePosition2.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("PlayerGun2");
GameObject laser2 = Instantiate(mGunBullet, firePosition3.position, Quaternion.identity) as GameObject;
//FindObjectOfType<AudioManager>().PlayOneShot("PlayerGun2");
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser2.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
animatorMuzzleFlash2.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash3.SetFloat("Muzzle", 0.6f);
//yield return new WaitForSeconds(0.1f);
}
}
if (pUpMan.mGunThree == true)
{
aM.PlaySound("PlayerGun3Loop");
if (pUpMan.damageUpIsActive)
{
GameObject laser = Instantiate(mGunBulletPowerdUp, firePosition.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("DamageUp");
GameObject laser2 = Instantiate(mGunBulletPowerdUp, firePosition2.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("DamageUp");
GameObject laser3 = Instantiate(mGunBulletPowerdUp, firePosition3.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser2.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser3.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
animatorMuzzleFlash.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash2.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash3.SetFloat("Muzzle", 0.6f);
//float periodNewThree = 0.1f;
//projectileFiringPeriod = periodNewThree;
}
else
{
GameObject laser = Instantiate(mGunBullet, firePosition.position, Quaternion.identity) as GameObject;
aM.PlayOneShot("PlayerGun");
GameObject laser2 = Instantiate(mGunBullet, firePosition2.position, Quaternion.identity) as GameObject;
// FindObjectOfType<AudioManager>().PlayOneShot("PlayerGun3");
GameObject laser3 = Instantiate(mGunBullet, firePosition3.position, Quaternion.identity) as GameObject;
// FindObjectOfType<AudioManager>().PlayOneShot("PlayerGun3");
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser2.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
laser3.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
animatorMuzzleFlash.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash2.SetFloat("Muzzle", 0.6f);
animatorMuzzleFlash3.SetFloat("Muzzle", 0.6f);
//float periodNewThree = 0.1f;
//projectileFiringPeriod = periodNewThree;
}
}
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
Now I solved the biggest problem by adding the loop sound inside if (pUpMan.mGunThree == true) and stop it inside if (pUpMan.mGunOne == true). (mGunOne is the default state and when the player gets a power-up, it stays active for a while and returns to the default state). In this case, if the player presses the button , the start and loop sounds are heard and if they release the button the loop sound stops, and the end sound is heard.
If they don’t release the button, after the power-up timer ends, the loop sound stops when the default state is active. They won’t hear the end sound in this case but I can live with that…=)
But still, I cannot understand why the boolean is not working to stop the sound.
if (Input.GetButtonDown("Jump"))
{
rapidFireCoroutine = StartCoroutine(RapidFire());
if (pUpMan.mGunThree)
{
aM.PlayOneShot("PlayerGun3Start");
aM.PlaySound("PlayerGun3Loop");
}else if (!pUpMan.mGunThree)
{
aM.StopSound("PlayerGun3Loop");
}
Is it when you press the button down that the block fires and keeps running until you release the button even some values inside the block is changing (like the boolean) but these values don’t register because it didn’t finish running the block which started when you first pressed the button and you never released it?
Isn’t this block being inside Update() runs it from the start every frame even the button is not released?