- Home /
Stuttering/Jerky at constant movement Unity3D.
Hi everyone!
I'm working on a very simple 2D game for android, the game only have a ground and a ball which moves at constant speed on X axis and nothing else.
The ground has a collider and the ball has a Rigidbody2D, a collider and this simple script to move it at constant velocity:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float running = 10f;
void Update() {
rigidbody2D.velocity = new Vector2(running, rigidbody2D.velocity.y);
}
}
When I play the game on my tablet or any other mobile device and the ball moves at constant speed you can see every 2-3 seconds a little stuttering. The stuttering occur just on the object which the script of movement are applied, in this case, the ball.
Is not a type of stuttering of Interpolate/Extrapolate or Update/FixedUpdate, this stuttering looks different.
I have tried almost everything what I have seen on google searches but none have solved my problem.
I have tried too delete the rigidbody at the ball and move it with differents scripts, but the stuttering still there. Looks like the problem is something related with the movement engine.
Can somebody help me with this problem please?
I'm trying to find a solution to this problem more than a year, this is very frustating!
I appreciate any help you can give me.
Thanks!
Think you could upload a video to show what you mean? What comes to $$anonymous$$d is, of course, the code not being in FixedUpdate, but you said that that's not it, so I think it might help to see it at least.
Also, I don't know the scope of your project, but if it's big and has other scripts, it might help to run a Profiler on it and see if the spikes are caused by expensive calls in those other scripts.
Hey thanks for your answer.
I have tried to record a video to show you what I am talking about, but in the video you can't appreciate the stuttering. The only way to see the stuttering is if I give you the file .apk of my game and you can see it in your own device.
In my project I have many others scripts, but I have deleted them and I have left only the script of movement, the player and the ground.
With only these 3 things I have tested on my tablet with the Profiler and everything goes at 60fps without spikes.(This is very stange).
I have noticed, the stuttering is more noticeable if the screen of device where I test the game is more bigger.
I think the stuttering is not related with the script, I think is something related with Physics or Physics2D. Also I have changed some values in Time and $$anonymous$$aximumAllowedTime, this has helped me a lot but dosn't eli$$anonymous$$ate completely the stuttering.
You might be right about the problem lying in physics. in general, the rigidbody velocity is being calculated by unity, you can override it but sometimes it can create jerkiness. So the best way is to change the gameobject position via modifying transform (translate, or lerp on position) or by rigidbody but using add force. Try to avoid changing velocity directly. Test it and tell me if it works.
Hi @Andrei23, I am also facing same issue on android devices only. Done with all the profiling and reduced the problem down to this one. Can you please tell me which unity version you are using. next week unity will release new public version V.4.6.8. Will test in that also if this issue is fixed.
Hi @Nickyj, I am using Unity V.4.5.0f6. If you finally get find the solution to this issue, please let me know.
Answer by dsmeathers · Aug 18, 2015 at 03:33 PM
I've had this problem on Android before, and it was down to the number of fixed updates per frame fluctuating. You can verify this by using this test code:
int FixedUpdatesPerFrame = 0;
void FixedUpdate()
{
FixedUpdatesPerFrame++;
}
void Update()
{
Debug.Log( "FixedUpdatesPerFrame: " + FixedUpdatesPerFrame );
FixedUpdatesPerFrame = 0;
}
If you have your time step parameters set to something sensible like: Fixed timestep = 0.016666 Maximum timestep = 0.0166666 then you should see this:
FixedUpdatesPerFrame: 1
If you see a fluctuation in that value at the same time as the stuttering then you've found your problem. I'm guessing it's down to either a bug in Unitys time management, or maybe the Android timer isn't very precise.
I solved it by reducing the Fixed timestep a bit. A better solution for a simple case like this would be to remove the rigid body and just move it inside Update() by modifying transform.position.
Hi! Thanks for your help and sorry for my late response.
I have tried your test code and I can see a fluctuation in the values as you said.
So I changed Fixed timestep = 0.0005 / $$anonymous$$aximumAllowedTime = 0.014 and I put the line of movement in FixedUpdate and this has helped me a lot.
With these changes, I have tried again your test code and I keep having fluctuations. The stuttering still there but is not appreciated to much so I am happy with the result.
I guess if is a bug of unity or android, I can't do anything more to get a "perfect movement".
Thank you very much for your help!
No problem. The only solution really is to not use Unity rigid bodies and ins$$anonymous$$d do all of your movement from a script inside the Update() function. Fixed update is always a bit erratic on Android.
The fixed timestep you're using is very low, it could be very expensive (performance wise) as it'll have to call FixedUpdate very often. Also, I wouldn't set the maximum allowed timestep lower than 0.016666 as that is the $$anonymous$$imum amount of time an Update can take even when running at 60fps.
Thank you so much! For some reason my maximum allowed time was 0.01 while my fixed timestep was 0.01667 - changing them both to 0.01667 fixed the problem!
Answer by zorksox · Jan 26, 2018 at 06:37 AM
I know this thread is 2 years old, but I'm not sure the question has been answered fully. By default, Unity runs all it's games in a fullscreen windowed mode. This prevents the engine from having control over the refresh rate of the monitor. Try going into the player settings and set the fullscreen mode to "exclusive mode". I made a video compiling all the fixes to stuttering that I know of. I hope this helps someone.
Hey zorksox, good point about using exclusive mode, although the original question was specific to Android.
One point on your advice to not use delta time inside fixed update: You should never multiply forces by Time.deltaTime. Forces are scaled by the timestep internally by Unity. $$anonymous$$ultiplying the force before you apply it will result in it being scaled by the timestep twice.
However, contrary to your advice, you should use Time.deltaTime inside fixed update when you are directly changing velocities or positions. Time.deltaTime is set to fixedDeltaTime when FixedUpdate is called. This means it will always match your fixed update rate, it is independent of the refresh rate of your monitor. If you do not multiply by delta time in these cases then behaviour will change if you change the fixed update rate. Doing this will not have any affect on stuttering because the timestep inside fixed update is always constant (that is the entire point of fixed update).
Answer by Kenvill101 · Oct 10, 2020 at 12:39 PM
I'm having a similar issues im making a zombie/survival tps and when I fire the animations are jerky. Only when I fire. Top half of body jerks. I can't seem to fix it.