老师好,想问下为什么我自己写的代码会一直调用footStep.Play(),导致声音重复播放,难道不应该是第一帧打开了AudioSource,第二帧 !footStep.isPlaying就为false了吗
我的代码:
private AudioSource footStep;
void Awake ()
{
anim = GetComponent();
footStep = GetComponent();
}
void Update ()
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion") && !footStep.isPlaying)
{
footStep.Play();
print("play");
}
AudioSource中的Loop是勾选了的
附上siki老师写的代码,是正确的:
void Update ()
{
if (anim.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
{
PlayFootStep();
}
private void PlayFootStep()
{
if (!footStep.isPlaying)
{
footStep.Play();
print("play");
}
}