Forum rules - please read before posting.

Play sound effect when a character talks

So I want to play a certain sound effect ONCE, every time a character starts talking.
How can I go about implementing this? If this can only be done with coding - can anyone help with the code (as I have no knowledge in coding.. :-( )
Thanks a lot!

Comments

  • edited October 2019

    This can be done by hooking into the OnStartSpeech custom event.

    Sample script:

    using UnityEngine;
    using AC;
    
    public class PlayAudioOnSpeech : MonoBehaviour
    {
    
        public AudioClip audioClip;
    
        private void OnEnable ()
        {
            EventManager.OnStartSpeech += StartSpeech;
        }
    
        private void OnDisable ()
        {
            EventManager.OnStartSpeech -= StartSpeech;
        }
    
        private void StartSpeech (AC.Char character, string lineText, int lineID)
        {
            AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
        }
    
    }
    
  • Thanks!
    Few questions:
    1.This script needs to be on a Game Object in the scene? can it be a component of the character game object prefab?
    2. Do I need to substitute lineText and lineID for something else or can I just leave it as is?
    3. Do I change audioClip with the name of the clip including extension (e.g. cry.mp3)?
    4. Where is the audio clip file needs to be placed (i.e. in what directory)?

    Thanks again for all the help...

  • This script needs to be on a Game Object in the scene? can it be a component of the character game object prefab?

    It needs to be in the scene, but if it's placed on a prefab that's present in the scene at runtime then that'll work also.

    Do I need to substitute lineText and lineID for something else or can I just leave it as is?
    Do I change audioClip with the name of the clip including extension (e.g. cry.mp3)?

    The script itself can be left alone - at least to achieve what you posted about.

    Where is the audio clip file needs to be placed (i.e. in what directory)?

    It can be anywhere, just assign it in the component's Inspector.

  • So I'm having some trouble setting this up correctly.
    I've (see pics):

    1. Created an empty GO in the scene.
    2. Added an audio source component to it with the clip I want played each time the character speaks
    3. Added a script component with the code you gave me above.
    4. Changed the 'character' in the code with the actual name of the character (genia).

    It doesn't work, and it also says the script can't be loaded (the console says the reference script is missing...)

    What do I need to do to make this work?
    Thanks a lot!


  • edited October 2019

    I think the name of the script needs to match its Class name, so you should name it "PlayAudioOnSpeech" not "GeniaSquak" (or you should rename its Class name in the script itself to "GeniaSquak")

  • Thanks!
    It works now, but it works when ANY character talks not just genia...
    Any idea how can I limit it to just genia?

  • edited October 2019

    Yeah, StartSpeech will get triggered every time ANY character speaks, so you need to add a check in there if you want to limit the Audio Clip to play only when a specific character speaks. You should really change the script back to Chris's original (i.e. "AC.Char character" not "AC.Char genia"), to reflect that, otherwise it will get confusing

    If Genia is the Player Character then you could add a test (assuming you've renamed the variable back to "character", as I suggested) something like:

    if (character == KickStarter.player)
    {
    AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
    }

    If Genia ISN'T the Player Character then, assuming that their Prefab's "SpeakerLabel" is "Genia", then you could add a test something like:

    if (character.speechLabel == "Genia")
    {
    AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
    }

    I'm sure that there are other, and probably better, ways of achieving the same thing, but those should work

  • Thanks a lot, will test this when I get back from trip.
  • edited October 2019

    It works now, but it works when ANY character talks not just genia...

    That was how I read your original request - you didn't mention this being only for a specific character.

    The script name doesn't need to change, and the speaking character is provided as a parameter in the OnStartSpeech event.

    Replace with the following and attach directly to the "Genia" character's root object:

    using UnityEngine;
    using AC;
    
    public class PlayAudioOnSpeech : MonoBehaviour
    {
    
        public AudioClip audioClip;
    
        private void OnEnable ()
        {
            EventManager.OnStartSpeech += StartSpeech;
        }
    
        private void OnDisable ()
        {
            EventManager.OnStartSpeech -= StartSpeech;
        }
    
        private void StartSpeech (AC.Char character, string lineText, int lineID)
        {
            if (character == GetComponent <AC.Char>())
            {
                AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
            }
        }
    
    }
    
  • Thanks Chris!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.
Do NOT follow this link or you will be banned from the site!