playing audio clips, over enemy animation

Hello!
I followed a tutorial on basic ai - that works fine.
When i decided to add sound to my little knight i ran into some problems.
I want the knight to play different sounds in different animation states, footsteps when he is walking and whistling when idle.

I can see that the audio-clip is changed in-game, but no sound is being played.
except for when he starts walking, then some sound plays that sound like old radio scratching.

thanks for your time!
below is how it looks atm, in editor and the code C#

using System.Collections;
using UnityEngine;
using UnityEngine.Audio;

public class AiBasic : MonoBehaviour
{
    public Transform player;
    public float speed;
    public float alertDistance;
    public float walkingDistance;
    public float attackingDistance;
    public AudioClip Idle;
    public AudioClip Walking;
    AudioSource audioSource;
    private bool isIdle = true;
    private bool isWalking = false;
    private Animator anim;
    private Vector3 direction;

    void Start()
    {
        anim = GetComponent<Animator>();
        audioSource = GetComponent<AudioSource>();
    }
void Update()
    {
        //Alert
        if (Vector3.Distance(player.position, transform.position) < alertDistance &&
            Vector3.Distance(player.position, transform.position) > walkingDistance)
        {

            anim.SetBool("isIdle", false);
            anim.SetBool("isWalking", false);
        }
        //Attacking / Walking
        else if (Vector3.Distance(player.position, transform.position) <= walkingDistance)
        {

            direction = player.position - transform.position;
            direction.y = 0;

            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 1.5f * Time.deltaTime);

            transform.Translate(0, 0, speed);

            anim.SetBool("isWalking", true);
            anim.SetBool("isIdle", false);

            isWalking = true;
            isIdle = false;

            if (direction.magnitude <= attackingDistance)
            {
                anim.SetBool("isAttacking", true);
                anim.SetBool("isWalking", false);
            }
        }
        //Idle
        else if (Vector3.Distance(player.position, transform.position) > alertDistance)
        {
            anim.SetBool("isIdle", true);
            anim.SetBool("isWalking", false);

            isWalking = false;
            isIdle = true;
        }
        if (isWalking == true)
        {
            audioSource.clip = Walking;
            audioSource.Play();
        }
        else if (isIdle == true)
        {

            audioSource.clip = Idle;
            audioSource.Play();
        }
    }
}

I would switch how this is working. I would create events at the start of the animations that play the sounds I want. I also (and this is just a personal preference) make a different audio source for each clip in an empty object on my characters. This allows me to keep things like sound volume or tone adjustments for each individual sound.

-Alexander

1 Like