ItsJustJoshhh
ItsJustJoshhh

Reputation: 58

Sound is super distorted when played

When ever the enemy dies, he plays the sound but the sound ends up being super distorted. I think it's because its playing in the Update method, but I'm not sure how to overcome that.

From other forums I've read they say to use a boolean but how would I go about implementing a boolean in this situation?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.AI;

public class aiscript : MonoBehaviour
{

NavMeshAgent agent;

public GameObject bloodExplosion;

private AudioSource audioSource;

public GameObject render;

public float Health;

public GameObject bloodSpawn;
private Transform bloodSpawned;

public GameObject player;

void Start()
{
    agent = GetComponent<NavMeshAgent>();

}


void Update()
{
    agent.SetDestination(player.transform.position);
    if(Health <= 0)
    {
        render.SetActive(true);
        audioSource = gameObject.GetComponent<AudioSource>();
        audioSource.PlayOneShot(audioSource.clip);
        Instantiate(bloodExplosion, bloodSpawn.transform.position, 
bloodSpawn.transform.rotation);
        Die();
    }
}

public void Die()
{
    Destroy(this.gameObject, audioSource.clip.length);
}

}

Upvotes: 0

Views: 59

Answers (1)

ItsJustJoshhh
ItsJustJoshhh

Reputation: 58

Found out how, if anyone was wondering I added an if statement:

if(Health <= 0)
{
    render.SetActive(true);
    audioSource = gameObject.GetComponent<AudioSource>();
    if(!audioSource.isPlaying)
    {
       audioSource.PlayOneShot(audioSource.clip);
    }
    Instantiate(bloodExplosion, bloodSpawn.transform.position, 
    bloodSpawn.transform.rotation);
    Die();
}

Upvotes: 1

Related Questions