I have 2 audiosources in my gameobject. The first one play whenever I hit an enemy and it goes the way it should but the second one( plays when the player dies) gets distorted. I think I read somewhere that the reason is that it starts to play after every frame but I put a boolean to try and avoid that. Is there a way to fix this?
void Start ()
{
playclip2 = false;
AudioSource[] sounds = GetComponents<AudioSource>();
clip1 = sounds[0];
clip2 = sounds[1];
}
void Update ()
{
walking = true;
}
void FixedUpdate ()
{
GroundCheck ();
if (grounded == false || dragged == true)
{
float h = Input.GetAxis ("Mouse X");
if (h > 0 && !facingRight)
Flip ();
else if (h < 0 && facingRight)
Flip ();
}
if (grounded == true)
{
if (walking == true)
{
walker ();
}
Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);
foreach (Collider2D c in frontHits) {
// If any of the colliders is an Obstacle...
if (c.tag == "DirectionTrigger") {
Flip ();
break;
}
if (c.tag == "Enemy") {
if (enemyhit == false){
if (c.gameObject.GetComponent<Bugbot> ().destructionImminent == false)
{
animator.SetTrigger("swat");
walking = false;
clip1.Play();
}
}
break;
}
}
}
if(HP <= 0 && !dead)
{
// ... call the death function.
Death ();
playclip2 = true;
StartCoroutine(delayAnim());
}
if(playclip2 && !audio.isPlaying)
{
playclip();
}
}
public void playclip()
{
clip2.Play();
}