gpt4 book ai didi

audio - 使声音触发器起作用

转载 作者:行者123 更新时间:2023-12-02 23:11:00 24 4
gpt4 key购买 nike

我不明白为什么我的声音触发无法正常工作。我创建了一个GameObject并向其中添加了一个音频源(在音频源上,我检查了Play On WakeLoop。然后添加了一个Sphere Collider(我检查了Is Trigger)。最后,我添加了脚本并将声音片段添加到了检查器,Unity还说:"There are 2 audio listeners in the scene."我几乎没有检查所有的音频监听器,但仍然说出来,我在做什么错?

这是我的代码:

 #pragma strict

var WalkAudio:AudioClip;
var OutCry:AudioClip;

function Update (){

var Audio= gameObject.GetComponent(AudioSource);

if(Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{

Audio.Play();
}
else
{

Audio.Pause();
}
}

function OnControllerColliderHit (hit : ControllerColliderHit)
{
var Audio= gameObject.GetComponent(AudioSource);

if (hit.gameObject.tag == "templeFloor")
{
var groundType = 1;
print("Temple");
Audio.clip=OutCry;
}

else{
Audio.clip=WalkAudio;
}

}

最佳答案

如你所说:

I have pretty much unchecked all the audio listeners



附加到 AudioListener 的组件Main Camera必须启用。可能您已使用 LoadLevelAdditive,所以您有两个摄像头和两个 AudioListener组件,每个组件一个。请确保此附加摄像机和任何其他gameObject均应禁用此组件。

同样在运行时,您可以在 AudioListener窗口搜索栏中使用该组件搜索附加了 Hierarchy组件的组件。
t:AudioListener

这应该可以帮助您。还要使用 Debug.Log仔细检查 OnControllerColliderHit函数调用,并且分配后 Audio.clip不为null。还需要用 Update播放声音吗?

另外,由于性能原因,您应该将此行从 Update移到 Start函数:
var Audio= gameObject.GetComponent(AudioSource);

并在整个脚本中使用此 Audio变量。

关于audio - 使声音触发器起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355361/

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