当另一个人开始演奏时声音消除

时间:2019-05-13 13:00:51

标签: c# unity3d audio

基本上,我主要是从教程中复制代码,但是在播放另一种声音后,我的声音取消存在问题。例如,当我射击并播放声音(如果我击中敌人)并且爆炸声音播放时,射击声音会静音。 我已经在空的gameobject soundmanager上有了此脚本,并可以通过soundmanager.instance.playSingle(sound)访问它。 我不知道该怎么做才能使声音不会互相抵消! 任何帮助表示赞赏

void Awake()
{
    //Check if there is already an instance of SoundManager
    if (instance == null)
        //if not, set it to this.
        instance = this;
    //If instance already exists:
    else if (instance != this)
        //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
        Destroy(gameObject);

    //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
    DontDestroyOnLoad(gameObject);
}


//Used to play single sound clips.
public void PlaySingle(AudioClip clip)
{
    //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    efxSource.clip = clip;

    //Play the clip.
    efxSource.PlayOneShot(efxSource.clip);
}
public void PlayCrash(AudioClip clip)
{
    //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    crashSource.clip = clip;

    //Play the clip.
    crashSource.PlayOneShot(crashSource.clip);
}

public void PlayShoot(AudioClip clip)
{
    //Set the clip of our efxSource audio source to the clip passed in as a parameter.
    ShootSource.clip = clip;

    //Play the clip.
    // ShootSource.PlayOneShot();
    ShootSource.PlayOneShot(ShootSource.clip);
}

//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
public void RandomizeSfx(params AudioClip[] clips)
{
    //Generate a random number between 0 and the length of our array of clips passed in.
    int randomIndex = Random.Range(0, clips.Length);

    //Choose a random pitch to play back our clip at between our high and low pitch ranges.
    float randomPitch = Random.Range(lowPitchRange, highPitchRange);

    //Set the pitch of the audio source to the randomly chosen pitch.
    efxSource.pitch = randomPitch;

    //Set the clip to the clip at our randomly chosen index.
    efxSource.clip = clips[randomIndex];

    //Play the clip.
    efxSource.Play();
}

}

1 个答案:

答案 0 :(得分:0)

您需要创建一个单独的音频源来处理爆炸(可能在弹丸对象/类上),或者如果不需要任何花哨的内容,则可以使用AudioSource.PlayClipAtPoint()See docs.

要使其生效,您可以修改PlayCrash方法以接受崩溃的发生位置(或者,如果没关系,只需使用Vector3.Zero

public void PlayCrash(AudioClip clip, Vector3 location)
{
    AudioSource.PlayClipAtPoint(clip, location);
}