【问题标题】:sound canceling when another one starts playing unity当另一个人开始统一播放时取消声音
【发布时间】:2019-05-13 13:00:51
【问题描述】:

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

}

【问题讨论】:

  • 不想被拖得太远,为什么不为第二个声音添加另一个音频源。

标签: c# unity3d audio


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多