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 /
avatar image
0
Question by jasebeaumont · Feb 24, 2021 at 02:17 PM · inputinput.getaxis

Input Lag in Animation Script

In the Editor and after building the game on Very-Low Quality (although I can easily run max quality), whenever I press a button, it takes maybe 300ms to actually register the input. And when I tap semi-quick my sprite moves but doesn't animate.

I'm pretty new and am tired of looking for so long so I posted here. The problem seems to be that it's taking a while for my animator to receive the boolean (double-checked in animator window).

Oh, and not to forget, my movement script is not affected by this problem although it also uses Input.GetAxis. However, it is a one-liner.

 public class RunningAnimation : MonoBehaviour
 {
     public bool isRunning;
     public Animator anim;
     bool flipX;
     // Start is called before the first frame update
     void Start()
     {
         anim = GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         float moveDirection = Input.GetAxis("Horizontal");
 
         switch (moveDirection)
         {
             case 0:
                 isRunning = false;
                 break;
             case 1:
                 isRunning = true;
                 flipX = false;
                 break;
             case -1:
                 isRunning = true;
                 flipX = true;
                 break;
 
         }
         anim.SetBool("isRunning", isRunning);
         this.gameObject.GetComponent<SpriteRenderer>().flipX = flipX;
     }
 }


alt text

forumpost.png (16.6 kB)
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

1 Reply

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

Answer by Llama_w_2Ls · Feb 24, 2021 at 02:45 PM

Input.GetAxis returns a value between -1 and 1. As you hold a key on your keyboard, the value increases up to 1, using smoothing, to produce natural movement. The issue here is, your switch statement only works when the cases are 0, 1 and -1. As I said, it returns a value between -1 and 1 and does take some time to get there. The input lag isn't the problem, but how you are reading the value.


I suggest doing some rounding to get the input immediately, for example, right before your switch statement, you could write:

 float moveDirection = Input.GetAxis("Horizontal");
 
 // Rounds values to -1, 0 or 1
 if (moveDirection > 0)
      moveDirection = 1;
 else if (moveDirection < 0)
      moveDirection = -1;


@jasebeaumont

Comment
Add comment · Show 3 · 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 jasebeaumont · Feb 24, 2021 at 08:32 PM 0
Share

WHAAT?? It's interpolated??? I would have not thought at all. Well, of course now I'm realizing that's probably for people using controllers. Anyway, whatever. Works like a charm! Thank you!

avatar image Mishka2121 · Mar 03 at 12:41 PM 0
Share

You can also use Input.GetAxisRaw() if you do not want any interpolation.

avatar image jasebeaumont Mishka2121 · Mar 06 at 05:45 PM 0
Share

Well, it's been over a year so I know that now, but I guess that will be useful for any noobs who see this in the future.

Also, using GetAxisRaw() worked much better for most projects. Thank you for pointing it out

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

146 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Input returns 0 until released and pressed again if held down before start of scene 0 Answers

Input.GetAxis normal behaviour when both axis inputs active? 0 Answers

Smooth value in Input System 0 Answers

Brake on wheel collider "slips" 1 Answer

Joystick Input - "3rd Axis" etc, not sure what works/doesn't 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