播放时声音超级失真

时间:2017-09-26 06:14:32

标签: c# unity3d unity5

当敌人死亡时,他会播放声音,但声音最终会变得非常扭曲。我认为这是因为它在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 个答案:

答案 0 :(得分: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();
}