2017-09-26 2 views
0

Quand l'ennemi meurt, il joue le son mais le son finit par être déformé. Je pense que c'est parce qu'il joue dans la méthode Update, mais je ne sais pas comment surmonter cela. D'autres forums, j'ai lu qu'ils disent d'utiliser un booléen mais comment pourrais-je mettre en œuvre un booléen dans cette situation?Le son est très déformé lorsqu'il est joué

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

} 

Répondre

1

Trouvé comment, si quelqu'un me demandais-je ajouté une instruction 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(); 
}