Unity sound effects play at high frequency, too fast

A kind of audio playback problem encountered by novices:

The main problem is that it is called every frame, and it can be solved by adding a layer of judgment to play().

if (horizontal != 0 || vertical != 0)
{             
   //如果声音源不在播放,保证不是每帧都重复调用播放
   if (!audioSource.isPlaying)
   {
       //播放音频剪辑
       audioSource.Play();
   }
}
else
{
      audioSource.Stop();
}

Guess you like

Origin blog.csdn.net/Kevin_meta/article/details/127157803