Forum rules - please read before posting.

Play speech sound without character talking animation

My game has no voice over, and I am using text scrolling sounds for characters speech. I have several Syllables, and I found this script: https://adventure-creator.fandom.com/wiki/Randomize_speech_audio_(Random_Syllables)

Which is super helpful in my case. However, it says it needs a character talking animation. While in my game, most of the time the characters are having custom animations when they talk. May I ask is there a way to check if the character is talking without using the isTalking variable or talking animation?

Thank you.

Comments

  • You can hook into the OnStartSpeech and OnStopSpeech custom events, which pass the character involved as a parameter:

    using UnityEngine;
    using AC;
    
    public class TalkExample : MonoBehaviour
    {
    
        private bool isTalking; 
    
        private void OnEnable ()
        {
            EventManager.OnStartSpeech += StartSpeech;
            EventManager.OnStopSpeech += StopSpeech;
        }
    
        private void OnDisable ()
        {
            EventManager.OnStartSpeech -= StartSpeech;
            EventManager.OnStopSpeech -= StopSpeech;
        }
    
        private void StartSpeech (AC.Char character, string lineText, int lineID)
        {
            if (character && character == GetComponent <AC.Char>())
            {
                isTalking = true;
            }
        }
    
        private void StopSpeech (AC.Char character)
        {
            if (character && character == GetComponent <AC.Char>())
            {
                isTalking = false;
            }
        }
    
        private void Update ()
        {
            if (isTalking)
            {
                // Do something
            }
        }
    
    }
    
  • Works as planned! Thanks!

  • I just encounter that when the speech is played in background, the script

    speaker.soundChild.audioSource.pitch = speaker.GetComponent<SpeechAudioRandomizer>().thePitch;

    Returns an error of NullReferenceException. But speaker is not null, it seems it cannot find the component in Update(). Is another way needed to get the Components attached to speaker when speech is played in background?

  • What's the context of this code?

    Are you sure its the GetComponent that's causing the problem? It may be speaker.soundChild.audioSource, if speaker is not null.

  • edited September 2020

    Just found out it's my problem. I forgot to attach the correct component. My bad.

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.