- Home /
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:
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!
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()
?
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.
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?
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.
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).
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?
@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.
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.