- Home /
Movement jitter every few seconds
Hi All,
Project Link: https://www.dropbox.com/sh/7xqzoz6pbpke7ya/U2pNHVqzRX
I am trying to move basic box objects from right part of the screen towards left with Lerp. I am testing it on my android (Galaxy S4) It moves smooth but every few seconds screen jitters or stutter which is very annoying.I have searched everywhere but no solid solution to this. With Vsync on its even worst.
I have also tried this with object pooling, still same jitter. PLEASE HELPPPPP
I have tried the following;
Update
FixedUpdate
LateUpdate
And for movement;
Translate
Lerp
Move forward
nothing seems to be working and any help would be appreciated.
using UnityEngine; using System.Collections;
public class moveScript : MonoBehaviour {
private Vector3 newPosition;
public float speed = 10.0f;
private int randomNum;
void Update () {
randomNum = Random.Range (0, 2);
movers ();
resetPosition ();
}
private void movers(){
Vector3 posB = new Vector3 (transform.position.x - 5 * Time.fixedDeltaTime , transform.position.y, transform.position.z);
newPosition = posB;
transform.position = Vector3.Lerp (transform.position, newPosition, Time.smoothDeltaTime * speed);
}
private void resetPosition ()
{
if (transform.position.x <= -4)
{
if(randomNum > 0)
{
transform.position = new Vector3(5.0f,-0.2f,0.0f);
}
else
{
transform.position = new Vector3(5.0f,0.2f,0.0f);
}
}
}
}
Uh, why are you using Lerp? If you want to travel along the X axis at a constant rate, you could just add to position.x.
Secondly, use Time.deltaTime or smoothDeltaTime if you're getting called from Update. Only use Time.fixedDeltaTime if you're getting called from FixedUpdate.
Thirdly, beware of "Power saving mode" on Samsung Galaxies. This will cap the framerate to 40fps and you'll never be properly vsynced with that on.
hey thanks for the reply, so i have tried moving along the x axis with;
Vector3 pos = transform.position;
pos.x += 10 * Time.smoothDeltaTime;
transform.position = pos;
and my Galaxy S4 "Power saving mode" is off, also tried with Update, FixedUpdate, LateUpdate. And when the Vsync is on it jitters even worst.
is it a frame rate problem or a processing problem? your code works fine on PC?
on PC its not as bad as it is on the Android. Frame rate is running at 60 but every few seconds it drops to 58-59.
what is GFX.WaitForPresent
Image for Profiler: https://www.dropbox.com/s/20pps15gbgrqock/Untitled-4.jpg
Present() is a direct3d call that swaps framebuffers. I guess WaitForPresent is the Unity equivalent where it waits out the rest of the Vsync interval.
Your profiler trace looks absolutely fine to me. No allocs and the spikes from 0.1ms to 1ms are probably just the OS scheduler taking a time slice here and there.
What kind of jittering are you seeing? I see a very slight shimmer from where the positions aren't rounded on to pixel boundaries, but it sounds like you're seeing short freezes. On a Galaxy S3 I get a couple of ~0.2 second stalls after start up, which is business as usual on Android. Otherwise it runs smoothly.
I guess you could try watching the OS logging via adb logcat to see what happens during the freeze.
Answer by nikescar · Apr 21, 2014 at 12:45 AM
Try turning development build off. Profiling can help track down problem scripts and stuff but, it has a performance overhead since it sends data frequently. Also, in the profiler, click on the spikes and see what is causing them. My guess is the skipping will disappear when the profiler is no longer running.
development build is off and on my pc i dont see the jitter/hiccups anymore..but its major on android.i have tried different phones galaxy S4 and S3. same on both.
Here is your project with my modifications
Look at my changes to the movers method. You didn't need to multiply posB by the Time.fixedDeltaTime because you are using lerp below that. Also, you want to match the deltaTime with the method you are using it in (if in Update use deltaTime, if in FIxedUpdate use fixedDeltaTime). I made this run in Update but, really, you want to move physics objects in FIxedUpdate always.
That said, I made the assumption that these were to be physics objects.
I also added a Rigidbody2D to each of your boxes (with is$$anonymous$$inematic checked) because, if I remember correctly, it is less of a hit on performance when moving colliders.
This runs perfectly smooth on my Nexus 7.