I have enemies that are moving in my game, each one has sound on it that plays when player is detected, i created a sphere zone for that, now when enemies are stationary then sound plays fine, but when they moving, i hear nothing, why?
this is my code in void Update
if (isChainsaw)
{
if (playerDetected && !audioSource.isPlaying)
{
audioSource.Play();
}
}
if (!playerDetected && audioSource.isPlaying)
{
audioSource.Stop();
}
and these the methods of detection and audio
private void CheckPlayerDetection()
{
// Check if the player is within the detection radius
Collider2D playerCollider = Physics2D.OverlapCircle(transform.position, detectionRadius, LayerMask.GetMask("Player"));
playerDetected = (playerCollider != null);
}
public bool IsPlayerDetected()
{
return playerDetected;
}
public void PlayProjectileAudio(AudioClip audioClipToPlay)
{
if (audioSource != null && audioClipToPlay != null)
{
audioSource.clip = audioClipToPlay;
audioSource.Play();
}
}
Audio is located on every enemy object, so when enemy moves, the audio moves to, my player has audio listener.
It may help to add some Debug.Log prints in each of the functions required to play the audio. This can narrow down which part of the script is failing so you can focus your investigation.
– dstears