My Player playing sound in every hit, and I don’t want it, because when 2 or more enemy attacking together, the sound played almost simultaneously
How to fix this?
My Player playing sound in every hit, and I don’t want it, because when 2 or more enemy attacking together, the sound played almost simultaneously
How to fix this?
That’s actually a tricky one, because each weapon’s sound goes to a different AudioSource.
You have to have a way to determine if there is an AudioSource already playing, which is tricky because you have multiple AudioSources…
So let’s create a new class
public class LockingAudioSource : MonoBehaviour
{
[SerializeField] private AudioSource source;
public static bool isPlaying = false;
public void PlayClip()
{
if (isPlaying) return;
isPlaying = true;
source.Play();
StartCoroutine(ClearWhenClipPlayed());
}
private IEnumerator ClearWhenClipPlayed()
{
while (source.isPlaying)
{
yield return null;
}
}
Add this to the same GameObject as the AudioSource and assign the AudioSource to the source field.
Instead of linking to AudioSource.Play(), instead link to LockingAudioSource.PlayClip();
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.