gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:29:36 25 4
gpt4 key购买 nike

基本上我主要是从教程中复制代码,但是我在另一个声音播放后取消声音时遇到了问题。例如,当我射击并播放声音(如果我击中敌人)并且爆炸声音播放时,射击声音会静音。
我已经在空的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();
}

}

最佳答案

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

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

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

关于c# - 当另一个人开始演奏时声音消除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56112905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com