Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

playing audio clips, over enemy animation

Discussion in 'Scripting' started by KanonKongen, Jul 16, 2017.

  1. KanonKongen

    KanonKongen

    Joined:
    Sep 17, 2015
    Posts:
    18
    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#

    screnshet.png

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.Audio;
    4.  
    5. public class AiBasic : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float speed;
    9.     public float alertDistance;
    10.     public float walkingDistance;
    11.     public float attackingDistance;
    12.     public AudioClip Idle;
    13.     public AudioClip Walking;
    14.     AudioSource audioSource;
    15.     private bool isIdle = true;
    16.     private bool isWalking = false;
    17.     private Animator anim;
    18.     private Vector3 direction;
    19.  
    20.     void Start()
    21.     {
    22.         anim = GetComponent<Animator>();
    23.         audioSource = GetComponent<AudioSource>();
    24.     }
    25. void Update()
    26.     {
    27.         //Alert
    28.         if (Vector3.Distance(player.position, transform.position) < alertDistance &&
    29.             Vector3.Distance(player.position, transform.position) > walkingDistance)
    30.         {
    31.  
    32.             anim.SetBool("isIdle", false);
    33.             anim.SetBool("isWalking", false);
    34.         }
    35.         //Attacking / Walking
    36.         else if (Vector3.Distance(player.position, transform.position) <= walkingDistance)
    37.         {
    38.  
    39.             direction = player.position - transform.position;
    40.             direction.y = 0;
    41.  
    42.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(direction), 1.5f * Time.deltaTime);
    43.  
    44.             transform.Translate(0, 0, speed);
    45.  
    46.             anim.SetBool("isWalking", true);
    47.             anim.SetBool("isIdle", false);
    48.  
    49.             isWalking = true;
    50.             isIdle = false;
    51.  
    52.             if (direction.magnitude <= attackingDistance)
    53.             {
    54.                 anim.SetBool("isAttacking", true);
    55.                 anim.SetBool("isWalking", false);
    56.             }
    57.         }
    58.         //Idle
    59.         else if (Vector3.Distance(player.position, transform.position) > alertDistance)
    60.         {
    61.             anim.SetBool("isIdle", true);
    62.             anim.SetBool("isWalking", false);
    63.  
    64.             isWalking = false;
    65.             isIdle = true;
    66.         }
    67.         if (isWalking == true)
    68.         {
    69.             audioSource.clip = Walking;
    70.             audioSource.Play();
    71.         }
    72.         else if (isIdle == true)
    73.         {
    74.  
    75.             audioSource.clip = Idle;
    76.             audioSource.Play();
    77.         }
    78.     }
    79. }
     
    Last edited: Jul 17, 2017
  2. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    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
     
    KanonKongen likes this.