- Home /
Conflicting Animator SetTrigger
Apologies in advance, I'm very new and I'm sure my code is horribly ugly.
I've made up a very simple little Tamagotchi style game where you look after a frog.
Frog has needs that depreciate over time. It's a super simple setup so that it's idle animation is Happy, unless any of it's needs drop below 50, when Sad is triggered.
I'm trying to add the Croak animation when the Feed button is pressed. It works when it's needs are over 50, presumably because it's using it's idle anim, but I can't quite figure out how to overwrite the Sad trigger, as the Croak animation doesn't trigger if the pet is already Sad. I'm guessing it's to do with my If statements, I'm sure it's something simple that I'm missing.
Here's the section that triggers the animations (TaskonClick is another Animator.SetTrigger for Croak):
{
Button btn = Feed.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
if (Needs.Happiness < 50)
{
PetAnimator.SetTrigger("Sad");
}
if (Needs.Energy < 50)
{
PetAnimator.SetTrigger("Sad");
}
if (Needs.Food < 50)
{
PetAnimator.SetTrigger("Sad");
}
Answer by Odinwastaken · Oct 09, 2020 at 12:50 PM
I'm also quite new to unity but I'll try my best haha, do you have a transition between the sad animation and the croak animation in the animator window? Also if you want the frog to stay sad instead of using Triggers for your animations use booleans or "bool". Booleans in my opinion give you more control over which animation is playing when. I only use triggers for 1 time animations like e.g a eating or feeding animation while bools for repeating long animations. Example for how to use booleans in code.
if (Needs.Food <= 50)
{
anim.SetBool("Sad", true);
} else {
anim.SetBool("Sad", false);
}
You would be a transition going both ways for this to work in the animation window. There are a bunch of tutorials on youtube on the animation window if you just search up, "How to use the animation window unity." I believe your problem is that triggers ain't the best way of making idle animations.
$$anonymous$$y friend you're a star, booleans are much better for this, thank you! Now I have this simpler solution:
if (Needs.Happiness < 50 || Needs.Energy < 50 || Needs.Food < 50)
{
PetAnimator.SetBool("Sad", true);
PetAnimator.SetBool("Happy", false);
}
else
{
PetAnimator.SetBool("Happy", true);
PetAnimator.SetBool("Sad", false);
}
And a bog standard "play on click" for the Croak animation. All works perfectly. I am eternally grateful!
Your answer
Follow this Question
Related Questions
Input.GetKey(KeyCode.E) requires multiple presses. 1 Answer
play an audio clip in an if statement 2 Answers
Use multiple values for "if" statement 1 Answer
If statements not working right 1 Answer
Conflicting Animator SetTrigger 0 Answers