Triggering the animations for a game object sprite instead of a UI sprite

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Basel
Posts: 17
Joined: Tue Jul 19, 2022 1:07 pm

Triggering the animations for a game object sprite instead of a UI sprite

Post by Basel »

Hello,

So I'd like to trigger the facial expressions and animations for a game object sprite instead of one in the UI. The tutorial shows how to do this for UI animations only, as far as I can tell.

1. To change the expressions for a scene game object, would the best way to go about it be to use the scene events in the inspector for the conversation?
2. Is there any way trigger the talking animation on the sprite game object to match the length of the typewriter effect of each conversation block?

Thanks for your help!
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Triggering the animations for a game object sprite instead of a UI sprite

Post by Tony Li »

Hi,

The answer to both questions is to use cutscene sequences.

For example, say the speaker's game object sprite has an Animator with animator states named "Idle" and "Talk". You can use this sequence to play the Talk animation while the typewriter is typing:

Code: Select all

AnimatorPlay(Talk);
required AnimatorPlay(Idle)@Message(Typed)
Basel
Posts: 17
Joined: Tue Jul 19, 2022 1:07 pm

Re: Triggering the animations for a game object sprite instead of a UI sprite

Post by Basel »

Thank you for the reply, Tony!
Should there be parameters to trigger Idle and Talk? Right now it looks like the code just seems to make the character Talk nonstop but the Idle doesn't ever trigger. I'm using this code:

Code: Select all

AnimatorPlay(Talk);
required Animator.Play(Mouth_Idle)@Message(Typed)
Not sure what I'm doing wrong.
Attachments
Screenshot states.png
Screenshot states.png (35.31 KiB) Viewed 204 times
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Triggering the animations for a game object sprite instead of a UI sprite

Post by Tony Li »

Hi,

Check the Console window for warnings.

Also try changing this line:

Code: Select all

required Animator.Play(Mouth_Idle)@Message(Typed)
to this:

Code: Select all

required AnimatorPlay(Mouth_Idle)@Message(Typed)
(Remove the "." between Animator and Play.)

You don't need any parameters for this. AnimatorPlay() tells the animator to play a state directly, without the need for parameter-based transition arrows.
Basel
Posts: 17
Joined: Tue Jul 19, 2022 1:07 pm

Re: Triggering the animations for a game object sprite instead of a UI sprite

Post by Basel »

Thanks, I can't believe I did that lol! (I also put the two animations in opposite spots in the code.)
I did fix the spelling error and I was still having issues. It took a while for me to figure out since I'm still new to Unity, but it seems like what was causing issues was the fact that I had a transition from the talk animation to mouth_idle in the animator. Any transitions between mouth_idle and talk seems like it made those animations only play once, instead of the entire time the text was being typed. I guess it was something I didn't understand in terms of how animation works in Unity, but leaving this here in case some one has the same problem.

Thank you again!
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Triggering the animations for a game object sprite instead of a UI sprite

Post by Tony Li »

Glad to help!
Post Reply