Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Oddeye01 · Dec 10, 2013 at 08:15 AM · animation2dmovementanimator4.3

Animation Lag when Changing Movement Direction 2D (4.3)

I'm currently trying to build a script to handle the players movement for a 2d top-down rpg. The animation is fine for all directions, as well as moving idle.

The problem I'm having is when holding the left arrow to move left, then switching to down, the character will start to move down, but the animation is still running to the left for half a second to a second. This only happens generally when moving left or right, and attempting to then go north and south.

For a simplified script :

 public class playerController : MonoBehaviour
 {
  
     private Animator animator;
  
     // Use this for initialization
     void Start()
     {
         animator = this.GetComponent<Animator>();
     }
  
     // Update is called once per frame
     void Update()
     {
  
         var vertical = Input.GetAxis("Vertical");
         var horizontal = Input.GetAxis("Horizontal");
  
         if (vertical > 0)
         {
             animator.SetInteger("Direction", 2);
         }
         else if (vertical < 0)
         {
             animator.SetInteger("Direction", 0);
         }
         else if (horizontal > 0)
         {
             animator.SetInteger("Direction", 1);
         }
         else if (horizontal < 0)
         {
             animator.SetInteger("Direction", 3);
         }
     }
 }

Basically, is there any way to force an animation switch? I tried speeding up the animation, which did not correct the issue. Also, I tried to lower the time in the transition, which had no effect. To give an overall idea, if I hold up for a second, then left for a second, and keep going back and forth, the player will never face left, but will continue to move up then left, etc. Thanks!

Note: This is for a Unity 2D project from 4.3, with a Fire Emblem, GBA Zelda type view.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Oddeye01 · Dec 10, 2013 at 07:04 PM

To anyone that has this trouble themselves, the problem is the "input gravity" for 2d characters, which is defined as "Speed (in units/second) that the output value falls towards neutral when device at rest". The character is still walking ever so slightly in the previous direction, forcing them not to change animations until the other direction eventually comes to a complete stop.

To solve this, go to the edit -> project settings -> input, and adjust the horizontal and vertical axes gravity values. By having a higher input gravity, the time between letting the button go and coming to a complete stop will decrease.

Note this is not the same as a physics gravity, and is intended in decreasing the time between key release of a direction, and the stopping of your character.

Comment
Add comment · Show 5 · 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 Oddeye01 · Dec 23, 2013 at 05:23 AM 0
Share

Why just the down vote with no explanation of why? Would it have been better if I didn't post the answer to my problem?

avatar image infinitypbr · Dec 23, 2013 at 06:37 AM 0
Share

I think the answer you've posted, while it may work, isn't a "Best Practice" solution. If it's just a switch, I'd suggest always having the character moving "forward", and simply change the rotation in code.

avatar image Oddeye01 · Dec 23, 2013 at 06:50 AM 0
Share

Interesting, I've read your answer but cant see why using hard code switches is better then the animator? Whats the problem with one vs the other. And why is code switches best practice? The 2d example by unity uses the animator approach albeit without the need of vertical movement, thus avoiding gravity. Thanks! :D

avatar image infinitypbr · Dec 23, 2013 at 09:55 AM 0
Share

Personally, having to adjust the gravity for this seems to be a difficult way to solve the problem.

I'm imaging the original zelda, top-down, but that you're doing it in a 3d world, correct? I suppose that's what i got from "Top Down 2D", since Unity's 2D stuff is made for side-scrolling.

If I'm wrong, then I guess adjusting gravity would be your solution.

But if it's top-down (like literally camera is pointing down) in a 3D world, then just adjusting the direction the character is facing and always moving "forward" seems far more elegant.

avatar image Oddeye01 · Dec 23, 2013 at 10:24 AM 0
Share

No, I am not building it in a 3D world. Its just using simple 2D sprites. You can still use the 2D part of Unity for RPG-esq games, I'm unsure why it would only be for side-scrolling. The character is made from a spritesheet, thus making "always" forward, much more difficult, since he can not properly turn. I can update my question with that

Sorry I should have noted that I'm not talking about gravity as physical gravity, but rather your "input gravity," which handles you input variables decent from moving to stop.

The exact definition being: "Speed (in units/second)that the output value falls towards neutral when device is at rest." Output being the speed value in my specified direction. Since this is a 2d style re$$anonymous$$iscent of gba zelda, or fire emblem, my vertical input is movement directions (a and s), and not "jumping", which unity has already mapped to, and assigned an "input gravity" to. So when my device is at rest (ie : the key press) the character will come to a complete stop sooner.

I'll correct that in my answer. But these variables are meant for this specific reason, and is not "gravity" as in falling speed from a jump.

Thanks sfbaystudios! :)

avatar image
0

Answer by infinitypbr · Dec 10, 2013 at 07:42 PM

Have you thought about simply changing the "forward" direction of the player? Instead of using the directions in the Animator, simply have the player move "Forward" if any button is pressed, and change the direction the player if facing when the button pressed is different than their current rotation.

From the script it looks like there's no smooth turn, just an old-school zelda-style "switch" to the new direction. If so, then using code to change the rotation, and having the player move forward regardless of where they're facing, would be easier, I'd bet.

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.

Follow this Question

Answers Answers and Comments

17 People are following this question.

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

Related Questions

2D Animation does not start 1 Answer

Animator warning in editor, animation is preventing script from working 1 Answer

Animator behaviour on GameObject 1 Answer

Snapping Child to Root after Animation 2 Answers

help with animation and movement?,i want to sort out my animation in my movement? 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