• 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 Brijac · May 11, 2021 at 07:26 PM · fpsstutterlaggyjittering

2D game with high FPS feels jittering/stuttering/laggy

Hello, I'm making this 2D game where I use rb2d.AddRelativeForce(Vector3.up); (rb2d is rigidbody) to fly with my character on button hold (he falls when the button is released) and that flying feels kinda jittering/stuttering/laggy on my android phone even tho the FPS is 60. Sometimes I feel it in the editor too.

My settings on rigidbody are: Mass: 0.05, Linear Drag: 0, Angular Drag: 0.05 and Gravity Scale: 1.

This is the Profiler: alt text

I tried to deal with this WaitForTargetFPS problem but I can't do anything about it. VSync Count is set to Don't Sync.

I would appreciate any help!

profiler.png (73.4 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JasonBennett · May 12, 2021 at 02:20 PM

It looks like your Physics2DFixedUpdate is not hogging much time, so I don't think it's a performance issue. Are you detecting the button in Update() or FixedUpdate()?

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 Brijac · May 12, 2021 at 02:58 PM 0
Share

This is my script for flying:

 private void Update()
     {
         if (isDead == false)
         {
             if (Input.GetKey(KeyCode.Mouse0))
             {
                 BatFly();
             }
         }
     }
 
     public void BatFly()
     {
         rb2d.AddRelativeForce(Vector3.up);
     }

Now I've read about putting physics in FixedUpdate and I did try putting FixedUpdate instead of Update above but I saw no change while testing.

avatar image JasonBennett Brijac · May 12, 2021 at 03:42 PM 0
Share

Hmmmm... that code looks perfect. I don't think it's the flying code. What I would recommend is creating a simplified scene and adding in components / features one by one to see which one is creating the jitter. My hunch is that it is NOT a performance issue, but a bug in code somewhere else.

What are some of the other features of the game?

avatar image Brijac JasonBennett · May 13, 2021 at 06:54 PM 0
Share

I have a player character created from a few sprites (animation) so it looks like he's flapping his wings to fly, particle system is attached to the player and both animation and particle system is working when the button Mouse0 is pressed. There is a map that I drew and I put a Polygon Collider 2D on it. I have an audio source attached to the play so the music is played in the background while playing. AdManager is on the scene to show AdMob ads.

I'll try to do what you said, to create a simplified scene and add components one by one and see which one will create the problem.

avatar image
0

Answer by devondyer · May 13, 2021 at 07:01 PM

The code in your comment shows you are moving the rigidbody in the Update() function. All physics based calculations should be done in FixedUpdate() and any movement of a rigidbody should be multiplied by Time.deltaTime to be framerate independent when making calculations (e.g. speed).

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 Brijac · May 13, 2021 at 07:04 PM 0
Share

Hello, I tried doing it in FixedUpdate but the result was the same. It was still laggy/stuttering.

Can you explain that "any movement of a rigidbody should be multiplied by Time.deltaTime" a bit, how do I do that?

avatar image devondyer Brijac · May 14, 2021 at 04:50 PM 0
Share

@Brijac, That's pretty weird, have you tried increasing the mass of the rigidbody you are moving (and adjusting your other values accordingly)? Having an extremely small mass could be what's causing Unity to make miscalculations. In fact, I am willing to bet that increasing it would fix your problem along with multiplying your movement by Time.deltaTime in FixedUpdate. Put your input calculations in Update so that your keypresses aren't triggered mulitple times per frame.


Time.deltaTime is the time measured between frames. Say you want to move a cube 1 unit every second. On your computer, you are running at 60 frames per second, so you set your cube's speed variable to move it 1 unit every second. So, for every frame, the cube is told to move 1/60th of a unit. You want me to test out your game, so you send the project over to me and I run it on my computer. My computer runs it at 120 frames per second. The cube is still told to move 1/60 of a unit every frame, but because I am running the game at 120 fps, the cube is being told to move twice as many times per second, doubling the distance it travels. By multiplying the cube's speed variable by Time.deltaTime when you move it, you will move the cube at the same 1 unit per second speed no matter how fast the game is running on someones computer.

avatar image Brijac devondyer · May 21, 2021 at 04:26 PM 0
Share

Thanks for explaining it to so well.

I will try to play with the mass of the rigidbody a bit. I will report the results here.

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

159 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

Related Questions

60FPS but the game seems lagging 2 Answers

Game stuttering only in iPhoneX and above. Not a performance issue. 0 Answers

micro Jittering, stuttering... bug of unity? 2 Answers

Stuttering Game problem 1 Answer

How to fix extremely jerky movement on AI car? 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