• 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
Question by 11tomi12 · Nov 11, 2017 at 06:55 PM · androidmovementinputbuttonlag

Input lag on Andorid but not in Unity

I made a simple script to move an object on the X and Y axis. It works fine in Unity, but when I build it and run it on android I get lag, meaning that the object changes direction after a delay and not right when I press the button. The code I have:

   void FixedUpdate () {
         if (!isMoving)
             return;
         //Controls();
         Vector2 temp = new Vector2(transform.position.x + direction.x * speed * Time.deltaTime * 50, 
  transform.position.y + direction.y * speed * Time.deltaTime * 50);
         gameObject.transform.position = temp;
     }

This code is called when the left or right button is pressed and it simply changes the direction vector:

 public void Controls()
     {
 
         if (Input.GetKeyDown("left") || leftPressed)
         {            
             if (direction == new Vector2(0, 1))
             {
                 direction = new Vector2(-1, 0);
             }
             else if (direction == new Vector2(1, 0))
             {
                 direction = new Vector2(0, 1);            
             }
             else if (direction == new Vector2(0, -1))
             {
                 direction = new Vector2(1, 0);
             }
             else if (direction == new Vector2(-1, 0))
             {
                 direction = new Vector2(0, -1);
             }
 
         }
            
         else if (Input.GetKeyDown("right") || rightPressed)
         {            
             if (direction == new Vector2(0, 1))
             {
                 direction = new Vector2(1, 0);
             }
             else if (direction == new Vector2(1, 0))
             {
                 direction = new Vector2(0, -1);
             }
             else if (direction == new Vector2(0, -1))
             {
                 direction = new Vector2(-1, 0);
             }
             else if (direction == new Vector2(-1, 0))
             {
                 direction = new Vector2(0, 1);
             }
         }
 
         leftPressed = rightPressed = false;
        
 
 
 
     }



Comment

People who like this

0 Show 7
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 pako · Nov 11, 2017 at 08:11 PM 0
Share

This could happen if your Android test device is not as performant as the PC where your run Unity. Does it behave like this on many Android devices that you test, or just one? Other than that, if there's something I would do to optimize the code, it would be using the predefined values of Vector2, instead of all the new Vector2(x, y) in the code. i.e. use Vector2.down instead of new Vector2(0, -1), or Vector2.right instead of Vector2(1, 0), etc. See:

https://docs.unity3d.com/ScriptReference/Vector2.html

So, replace all these in your code and try again and see if it makes any difference.

avatar image 11tomi12 · Nov 11, 2017 at 08:32 PM 0
Share

Thank you for your reply! I was testing it on my own phone only, a 8 core Huawei P9 Lite. I don't think that the new Vector2() is the issue, since it is called only whenever the left or right keys are pressed. Do you think the lag could be because of creating a new Vector2 in the FixedUpdate function?

avatar image pako 11tomi12 · Nov 11, 2017 at 09:44 PM 0
Share

@11tomi12 you say

object changes direction after a delay and not right when I press the button

Don't I understand well what you are saying? I understand that you press a button, so Controls() is called, then there's a delay, then direction changes. I'm a bit confused why you have commented out the call to Controls() in line 4 inside FixedUpdate (). I assumed that Controls() does get called in line 4 under normal conditions. Otherwise, if Controls() is never called, why do you post its code? So, why you now say

it is called only whenever the left or right keys are pressed

From the code you have posted, I can only see direction changing only after the left or right keys are pressed. What am I missing?

You should not be having any performance problems with Huawei P9 Lite. I would be surprised if new Vector2 in the FixedUpdate function could create such a lag. However, I would personally declare Vector2 temp as a class variable anyway, since it is used many times per frame.

avatar image 11tomi12 pako · Nov 11, 2017 at 10:11 PM 0
Share

I commented out Controls(), because I used Input.GetKey in it for testing purposes(left and right keyboard keys), but now that I use buttons for it it doesn't have to be called every frame. You are right, Controls() gets called whenever left or right button is pressed. The button pressed sets it's corresponding boolean to true, after that Controls() gets called and changes the direction Vector(). Could it be possible that Unity has some weird default settings for android, which would cause the delay?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by 11tomi12 · Nov 11, 2017 at 11:38 PM

Found the problem!

Since I'm using OnClick() for the buttons, the function runs AFTER the button is release, meaning obviously delay. I fixed this by simply adding an Event Trigger component to the buttons and added a PointerDown event, which runs the code right after the button is pressed.

Comment

People who like this

0 Show 0 · 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

193 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 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

Getting My Character to Move 1 Answer

Facing the players game object (upward, downward, left, right) on Android phone using joystick 0 Answers

Help with touchscreen controls 2 Answers

How to Ignore Input.MousePosition if button is pressed 2 Answers

how to rolle a ball by touch or mouse 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