"Undertale style" audio clips while text is written?

Announcements, support questions, and discussion for the Dialogue System.
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

"Undertale style" audio clips while text is written?

Post by SycamoreWoods »

Hello!

We're a group of students who are developing an RPG with visual novel elements.
We have been following the video tutorial series on how to use the Dialogue System asset, but we can't quite figure this out.
We'd like to add a series of short audio sounds to play while a character is talking, and to stop once the text is done writing itself on screen. Kinda like in Undertale, or Animal Crossing.

What would be the best way to do this?

Regards,

Team Sycamore
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Undertale style" audio clips while text is written?

Post by Tony Li »

Hi,

Assign audio clip(s) to the subtitle line's typewriter effect.
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

Re: "Undertale style" audio clips while text is written?

Post by SycamoreWoods »

Thank you Tony! That was way easier than we thought, we were trying to add it with a custom sequence.

Following on the Animal Crossing style of dialogue, we have a script that switches two textures on the character's face, one with its mouth open and the other closed. Can we make it so this script gets disabled once the text is finished typing, directly from the Typewriter script too?

We are very thankful for your help!
Team Sycamore
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Undertale style" audio clips while text is written?

Post by Tony Li »

You may be interested in using sequencer commands for this. (tutorial series)

If your Dialogue Manager's Subtitle Settings > Subtitle Chars Per Second matches the typewriter's Characters Per Second, then you can use the {{end}} keyword in the Sequence field, such as:

Code: Select all

AnimatorPlay(Talking);
required AnimatorPlay(Idle)@{{end}}
(Note that you can set this on the Dialogue Manager's Camera & Cutscene Settings > Default Sequence so you don't need to set it for each dialogue entry node.)

However, if you already have the face animation working the way you want, as an alternative you can use the typewriter's OnEnd() event to turn off the animation.

In case you're interested in using animated portraits, I'll leave some info here on that:
Spoiler
Animated portraits. Also covered in: Dialogue UI Tutorial (text version).

If your Dialogue Manager's Subtitle Settings > Subtitle Chars Per Second matches the typewriter's Characters Per Second, then you can use the {{end}} keyword in the Sequence field, such as:

Code: Select all

AnimatorPlay(Talking, NPC Portrait Image);
required AnimatorPlay(Idle, NPC Portrait Image)@{{end}}
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

Re: "Undertale style" audio clips while text is written?

Post by SycamoreWoods »

We're not sure if any of those apply, since we're not using an animation but a script that makes the gameobject switch between 2 different materials.

Code: Select all

public class Mouth_Flap : MonoBehaviour {

	public Material[] material;
	Renderer rend;

	void Start () {

		rend = GetComponent<Renderer> ();
		rend.sharedMaterial = material [0];
		StartCoroutine(CharacterBlink());
	}

	IEnumerator CharacterBlink () {

		while(true)
		{

		rend.sharedMaterial = material [0];

		yield return new WaitForSeconds(0.2f);

		rend.sharedMaterial = material [1];

		yield return new WaitForSeconds(0.1f);

		rend.sharedMaterial = material [0];

		}
	}
}

Also, doesn't the OnEnd on the typewriter refer to the end of the conversation?
We'd like for the script to be disabled each time the NPC's text is done typing, and enabled once the player selects an answer or clicks to continue the conversation.

We appreciate the help!
Team Sycamore
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Undertale style" audio clips while text is written?

Post by Tony Li »

The typewriter's OnBegin, OnCharacter, and OnEnd events refer specifically to the typewriter, not the conversation.

OnBegin occurs whenever the typewriter starts typing a new string of text. OnCharacter occurs with each character that it types. OnEnd occurs when the typewriter is done typing the string of text.

(Side note: If you want to know when the conversation as a whole starts and ends, add a Dialogue System Events component to the Dialogue Manager, or add a script to the Dialogue Manager that listens for the corresponding script messages.)

Try this:

1. In your Mouth_Flap script, change:

Code: Select all

void Start () {
to:

Code: Select all

void OnEnable() {
Otherwise it will only run once. Start is only called once for the lifetime of a script. OnEnable is called every time the script is enabled or re-enabled. And stop the coroutine in OnDisable.

2. Add a script like this to your typewriter GameObject:
FlapWhileTyping.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

[RequireComponent(typeof(UnityUITypewriterEffect))] // Change to TextMeshProTypewriterEffect if you're using TMP.
public class FlapWhileTyping : MonoBehaviour
{
    void Awake()
    {
        // We're setting OnBegin() and OnEnd() here so you don't need to hook them up in the inspector:
        var typewriter = GetComponent<UnityUITypewriterEffect>();
        if (typewriter == null) return;
        typewriter.onBegin.AddListener(() => { SetFlapping(true); });
        typewriter.onEnd.AddListener(() => { SetFlapping(false); });
    }

    void SetFlapping(bool value)
    {
        var speaker = DialogueManager.currentConversationState.subtitle.speakerInfo.transform;
        if (speaker == null) return;
        var mouthFlap = speaker.GetComponentInChildren<Mouth_Flap_Test>(); // !!! In case script is on child.
        if (mouthFlap == null)
        {
            Debug.LogWarning("Can't find mouth flap component on speaker: " + speaker, speaker);
        }
        else
        {
            mouthFlap.enabled = value;
        }
    }
}
(Updated to add debug info and find flap script on child if needed.)
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

Re: "Undertale style" audio clips while text is written?

Post by SycamoreWoods »

Got it! Thank you so much for the help.

Team Sycamore
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

Re: "Undertale style" audio clips while text is written?

Post by SycamoreWoods »

Update:

We've been working on it and in theory it *should* be working, we're getting no syntax errors or anything.

We changed the Mouth_Flap script and added the FlapWhileTyping to the Subtitle Text gameobject (located in the NPC Subtitle Panel, in our Dialogue UI prefab), but it doesn't seem to work.
Did we put FlapWhileTyping in the wrong place? You said we should put it in our typewriter gameobject, so we assumed you meant the G.O. with "Unity UI Typewriter Effect" script attached.

Sorry about the late reply, we wanted to try and figure it out for ourselves instead of coming back for help straight away.

Team Sycamore
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: "Undertale style" audio clips while text is written?

Post by Tony Li »

Hi,

Here's an example: MouthFlapExample_2019-03-05.unitypackage

I also made a few changes to the script above that could be helpful in debugging, and added an OnDisable method to my copy of Mouth_Flap (which I named Mouth_Flap_Test).
SycamoreWoods
Posts: 7
Joined: Mon Feb 25, 2019 10:25 am

Re: "Undertale style" audio clips while text is written?

Post by SycamoreWoods »

Thank you for the code, although opening the package lists a "missing prefab" so all we get is a blue background, the sphere and the cube with a changing texture. We've tried updating the conversation system asset as well (since our project uses the previous version) but still it no luck.
Post Reply