Forum rules - please read before posting.

Talk Animation Random

I just started using the talk animation bool and it's pretty handy. I was wondering is it possible to randomize it so that it doesnt play the same talk animation every time my character talks.

It would be a nice feature as it's easy to set up. Having multiple talk animations that play at random. Also maybe even ones that play based on emotions.

Comments

  • You can set up your Animator Controller to play a different talking animation based on the value of an integer parameter, but still have them only play when the "Talk bool" is set to True.

    You can then use a custom script to set this Integer's value randomly when the character speaks - which is best done by hooking into the OnStartSpeech custom event.

    For example, let's say you named the Integer "TalkStyle". Taking the script covered in the above tutorial, you could replace the "GetSpeech" function's contents with the following:

    if (character != null)
    {
        int randomValue = Random.Range (0, 5); // Random from 0 to 4
        character.GetAnimator ().SetInteger ("TalkStyle", randomValue);
    }
    
  • I can't get this to work. How should the script be exactly.

  • using UnityEngine;
    using System.Collections;
    using AC;
    
    public class SpeechEventTest : MonoBehaviour
    {
    
        private void OnEnable ()
        {
            EventManager.OnStartSpeech += StartSpeech;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnStartSpeech -= StartSpeech;
        }
    
    
        private void StartSpeech (AC.Char character, string lineText, int lineID)
        {
            if (character != null)
            {
                int randomValue = Random.Range (0, 5); // Random from 0 to 4
                character.GetAnimator ().SetInteger ("TalkStyle", randomValue);
            }
        }
    
    }
    
  • Alright thanks this works pretty well. I had to use a blank animation state for integer option 0.

    I have another question, what if I want emotions and animations.

    For example, if my character says something like What!! I have a facial expression for it.

    What if I want my character to also do the what animation randomly from 2 what animations. I know I can use mecanim and play animation action but can It be done through script..

    This one works when the character speaks, how do I do an emotion one.

    Do I use expression Integer ID? If so, how does that work exactly?

  • edited July 2019

    i'm having some issues with the script when in conversation with multiple characters.

    The variable seems to change everytime multiple characters talk even though the script is only on one character.

    I have 4 animations set up. the first one which is integer 0 is blank so the character a doesnt always move the hand when talking. The other 3 have animations.

    However when character b talks, the if the integer is setup to the 1,2,3 the character still moves the arm even though they aren't talking.

    How do i set the integer back to 0 when the character a finishes talking.

  • Do I use expression Integer ID? If so, how does that work exactly?

    The Expression Integer ID parameter is a special-case parameter that AC controls according to the [expression:Value] token inserted into speech text. For details on how to use this, see the Manual's "Facial expressions" chapter.

    The variable seems to change everytime multiple characters talk even though the script is only on one character.
    How do i set the integer back to 0 when the character a finishes talking.

    The script above was more of a demonstration to illustrate the principles you're looking to use. The following script will only affect the character it's attached to, and reset the "TalkStyle" parameter to zero when they stop speaking.

    using UnityEngine;
    using System.Collections;
    using AC;
    
    public class SpeechEventTest : MonoBehaviour
    {
    
        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 != null && character == GetComponent <AC.Char>())
            {
                int randomValue = Random.Range (0, 5); // Random from 0 to 4
                character.GetAnimator ().SetInteger ("TalkStyle", randomValue);
            }
        }
    
        private void StopSpeech (AC.Char character)
        {
            if (character != null && character == GetComponent <AC.Char>())
            {
                character.GetAnimator ().SetInteger ("TalkStyle", 0);
            }
        }
    
    }
    

    However when character b talks, the if the integer is setup to the 1,2,3 the character still moves the arm even though they aren't talking.

    Be aware that you can also wire up your Animator so that when the "Is talking" bool is set to False, the "TalkStyle" integer has no effect. AC will only control the parameters you allow it to - how you use those parameter values is up to you.

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.