2017-09-26 60 views
0

當敵人死亡時,他播放聲音,但聲音最終變得超級失真。我認爲這是因爲它使用Update方法,但我不知道如何克服這一點。播放時聲音超級失真

從其他論壇,我讀過他們說要使用布爾值,但我怎麼會在這種情況下執行布爾值?

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); 
} 

} 

回答

1

發現瞭如何,如果有人想知道,我添加了一個if語句:

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(); 
}