• 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 /
avatar image
0
Question by b0r3d0m · Oct 25, 2016 at 09:24 PM · animation2drotationmovementphysics

Character still walking when no key is already pressed

I have the following animation controller:

alt text

As you can see, there are 4 different walking animations and 4 corresponding idle animations.

There are also transition conditions between them:

 any state -> walkdown -- DirY < -0.1
 any state -> walkleft -- DirX < -0.1
 any state -> walkright -- DirX > 0.1
 any state -> walkup -- DirY > 0.1
 
 walkdown -> idledown -- DirY < 0.1 && DirY > -0.1
 walkleft -> idleleft -- DirX > -0.1 && DirX < 0.1
 walkright -> idleright -- DirX < 0.1
 walkup -> idleup -- DirY < 0.1

I also wrote the following code for the character animation:

 void FixedUpdate() {
     float xAxis = Input.GetAxisRaw("Horizontal");
     float yAxis = Input.GetAxisRaw("Vertical");

     GetComponent<Rigidbody2D>().velocity = new Vector2(xAxis, yAxis) * Speed;

     GetComponent<Animator>().SetFloat("DirX", xAxis);
     GetComponent<Animator>().SetFloat("DirY", yAxis);
 }

It works but I have the following questions:

  • Is it the appropriate way to make transitions between such states? I mean, DirY < 0.1 && DirY > -0.1 and DirX > -0.1 && DirX < 0.1 looks quite rude but hey, is it ok?

  • I noticed that sometimes the character still walking when no key is already pressed. I t$$anonymous$$nk that's because of the FixedUpdate method nature -- it is called every n milliseconds. Am I right? Is there any workaround to change animation immediately? I already unchecked the Has Exit Time checkbox in Inspector.

8acqugl-imgur.png (36.4 kB)
Comment
Add comment · Show 1
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 Genei_180 · Oct 26, 2016 at 09:48 PM 0
Share

To make it exit immediately uncheck at the Connection arrow the box Has Exit Time For your other Problem i unfortunately can't help you but i did the same.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheXWolf · Dec 06, 2016 at 01:04 AM

Movement really makes more sense inside Update(), FixedUpdate() is best saved for physics, though it's timing has not$$anonymous$$ng to do with it. Also you'll probably want to try transform.Translate and doing * Time.DeltaTime or Time.SmoothDeltaTime.

As for the character still moving, consider having the idle states as the defaults then moving into the moving anim by the Speed variable without an exit time, then back to idle when the speed paramater changes. If you want the anim to stop immediately as well you'll need to untick the Has Exit Time for those as well.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to properly flip 2D character with new Unity4.3 2D framework? (Have problem with animation rotation) 1 Answer

Rotating my character only while moving in a 2D plane 3 Answers

Objects stuck dragging across floor 1 Answer

2D sprite animation issue 0 Answers

Animator behaviour on GameObject 1 Answer


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