• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed May 12, 2016 at 01:08 AM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted

avatar image
Question by kerrot · Apr 18, 2016 at 02:30 PM · animatorstate-machine

animator: goes to another state that i not expect

alt text

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!

state.png (39.1 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
Best Answer

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.

Comment
ItTookMe2MonthsToCreateAnAccountHereSuccessfully
SumFortyOne

People who like this

2 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ItTookMe2MonthsToCreateAnAccountHereSuccessfully · Apr 18, 2016 at 08:10 PM 1
Share

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

avatar image Le-Pampelmuse ItTookMe2MonthsToCreateAnAccountHereSuccessfully · Apr 18, 2016 at 08:32 PM 1
Share

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.

avatar image kerrot · Apr 19, 2016 at 12:03 AM 0
Share

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("IsMove", false);

and

 anim.SetBool("IsMove", 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 timing i trigger "Attack" or something?

avatar image Le-Pampelmuse kerrot · Apr 19, 2016 at 11:55 AM 1
Share

Oops, you wrote that reply as an answer to your question ;) I've converted it to a comment to my answer. Make sure you click reply on the comment you want to reply to. The "Add Reply" button is for an answer, a solution.

My 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 isMove 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("IsMove", false);

..you will get a transition from walk to atttack. Because isMove 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("IsMove", false);

The transition will be walk->idle, because isMove is false and isMove 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 ;)

avatar image kerrot Le-Pampelmuse · Apr 19, 2016 at 01:25 PM 0
Share

Thank you very much.
Your explanation is very easy to understand.
Now I'm clear.
Thanks again

Show more comments
avatar image SumFortyOne · Apr 26, 2016 at 11:21 AM 0
Share

Nice explaination! I vote up

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Animator not changing states on prefabs 1 Answer

How to reuse animations? 0 Answers

State Machine Behaviour OnStateExit doesn't call some time. 0 Answers

How do I move two first person shooter hands at once using animation? 0 Answers

Can't Open Exit Time Settings 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges