The question is answered, right answer was accepted
animator: goes to another state that i not expect
This is my state machine
State Change,,,,,,,,,Conditions,,,,,,,,,,,,,,,,,Input Control
Idle -> Walk,,,,,,,,,,,,,,,IsMove == true,,,,,,,,,,,,,,MouseDown anywhere in game
Walk -> Idle,,,,,,,,,,,,,,,IsMove == false,,,,,,,,,,,,,MouseUp anywhere in game
Walk -> Attack,,,,,,,,,,Trigger "Attack",,,,,,,,,,,,,MouseUp on enermies
Attack -> Idle,,,,,,,,,,,,None,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,None
When MouseUp on enermies, it occurs two things:
Trigger "Attack" and set "IsMove" to false.
I'm sure that trigger "Attack" happens before set "IsMove" to false.
But state goes to Walk -> Idle, not Walk -> Attack.
So Attack Animation not played.
Can anyone tell me why? thank you!
Answer by Le-Pampelmuse · Apr 18, 2016 at 02:56 PM
Hi.
Edit your question and remove the code formatting for your normal text please.
This seems to be a logic problem.
MouseUp anywhere is a bigger condition than MouseUp on enemies.
if(Input.GetKeyUp(KeyCode.Mouse0) //<= Clicking anywhere happens ALWAYS...
{
walk->Idle //...so this also happens always, regardless of anything that comes after it.
if(MouseOverEnemy) // happens only when a enemy is marked
{
walk->Attack //so this will happen because the anywhere loop is happening anyways.
}
}
That means if you have a mouse up on an enemy, it will still go to Idle. You need to check if the mouse Is on an enemy or if it is NOT on an enemy when clicking:
if(Input.GetKeyUp(KeyCode.Mouse0) //<= Clicking anywhere happens ALWAYS
{
//walk->Idle This is removed
if(MouseOverEnemy) // happens only when a enemy is marked
{
walk->Attack //so this will happen because the anywhere loop is happening anyways.
}
else
{
walk->Idle
}
}
I hope I could clear things up for you. Feel free to ask if you are still having trouble.
What an eyesore xD
One question: Why did you write the logic like part of a code (with exception of the walk->idle parts)? To make it better understandable? If that is the reason, thanks, I could understandn it that way :D
Some people aren't born with golden text formatting skills, 95% of the internet proves that. :P
I did it for the exact reason you mentioned, yes.
Grateful for your reply.
Yes, i can write a logic like yours to solve this problem.
But what i curious is why i trigger "Attack" first, the state machine still goes to walk->idle.
I even tried both:
anim.SetTrigger("Attack");
anim.SetBool("Is$$anonymous$$ove", false);
and
anim.SetBool("Is$$anonymous$$ove", false);
anim.SetTrigger("Attack");
And both goes to walk->idle.
It will occur walk->Attack when the state transits to walk next time.
Is it because like attack state not loaded already when the ti$$anonymous$$g i trigger "Attack" or something?
Oops, you wrote that reply as an answer to your question ;) I've converted it to a comment to my answer. $$anonymous$$ake sure you click reply on the comment you want to reply to. The "Add Reply" button is for an answer, a solution.
$$anonymous$$y pseudocode was just to explain the logic of the conditions.
Yes both will go to walk->idle because is move is false at that moment. It makes no difference if you put the SetTrigger line before the other one.
Attack will be true and is$$anonymous$$ove will be false at the moment the script executes those lines. It will not make attack= true first and wait for the attack animation.
Like I said, it's a logic problem. You need a more complex condition if you want to have an additional transition like walk->attack.
Just try it. If you are already in the walking state:
anim.SetTrigger("Attack");
//anim.SetBool("Is$$anonymous$$ove", false);
..you will get a transition from walk to atttack. Because is$$anonymous$$ove is still true and the condition for the attack state is only depending on the attack trigger.
But if you are walking and have:
//anim.SetTrigger("Attack");
anim.SetBool("Is$$anonymous$$ove", false);
The transition will be walk->idle, because is$$anonymous$$ove is false and is$$anonymous$$ove is the only parameter the transition is depending on.
I don't know how the script you have works but you have to check the conditions.
Hope you understand this now ;)
Thank you very much.
Your explanation is very easy to understand.
Now I'm clear.
Thanks again
Follow this Question
Related Questions
Animation plays faster from default state than any other. 0 Answers
Animator GetBehavior Question 0 Answers
How to replace an Animation State in Animator, without rebuilding all the transitions? 1 Answer
Why StateMachineBehaviour not calling OnStateExit when doing animator.Crossfade through code? 0 Answers
How can an Animator FSM get public variables?,Making a Variable Holder for an FSM 0 Answers