- Home /
Sprite movement is jittery/laggy
Hi guys,
My problem is - I'm moving a 64x64 sprite with a 2d collider on, using transform.translate and the faster I move it the laggier and jitterier it becomes.
My situation is - I literally have only a white background scene and a black square moving about randomly using Transform.translate
, the square has a 2d collider on in order for players to tap it and that's all, there's nothing more on the scene or much else in code (except timers and trivial stuff). The square is moved by the below code and at random intervals, the directionX/directionY variables change:
transform.Translate (directionX * moveSpeed * mySmoothDelta,
directionY * moveSpeed * mySmoothDelta, 0);
where directionX is either 0, -1, or 1, movespeed is some float and mySmoothDelta is my custom variation of Unity's Time.smoothDeltaTime
and still, when I build this on iPod 5 the square's movement is very laggy and jittery.
How it started is, at first I had just Time.deltaTime
and it was jittery even in my editor. Then I removed Vsynch and it was better, then I tried Time.smoothDeltaTime
and it was a bit better but I had no control over the number of previous frames to control the smoothness so I wrote a custom smoothDelta and it became a bit better again. My smoothDelta is this:
frameTimesCounter += 1;
frameTimesCounter = frameTimesCounter % 60;
frameTimesArray[frameTimesCounter] = Time.deltaTime;
frameTimesSum = 0;
for (int i = 0; i < frameTimesArray.Length; i++)
{
frameTimesSum += frameTimesArray[i];
}
mySmoothDelta = frameTimesSum/60;
now It runs almost perfectly in my editor if I turn off Vsync and not restrict the framerate via Application.targetFrameRate
but if I build it on an iOS device, it's just beyond bad, and the higher the movement speed the worse it gets. Another problem is that because it lags it also fails to detect whether the square was tapped or not. I also have an iPad and iPhone 4 (weaker than iPod 5) but haven't build on those yet, though i'm sure i'll get the same result. Before I built it, I also experimented with putting a rigidbody on my sprite and moving it with a velocity instead but I got the same result as with Time.deltaTime so I proceeded with solutions on how to make translate better and after searching online came to the custom delta smooth.
I tried a lot of things and my online search provides no more leads, if anyone can make sense of this I'd highly appreciate it.
All I want is for this square to move as smoothly as possible, as this is one of the main goals of this project.
Thank you all in advance.
If you disable the collider but keep everything else the same does the lag go away?
Additionally, is it possible the "random variables" are too short and the square is constantly changing directions?
Lastly, I have bever used Transform.Translate before but it seems like it requires a Vector3 as its first input. If the Vector3 you're trying to go for is this:
(directionX * moveSpeed * mySmoothDelta, directionY * moveSpeed * mySmoothDelta, 0)
then perhaps it might work like this:
Vector3 tempDirection = new Vector3(directionX * moveSpeed * mySmoothDelta,
directionY * moveSpeed * mySmoothDelta, 0);
transform.Translate(tempDirection);
Are you using FixedUpdate? If so, change that to Update. Any moving collider should have a rigidbody regardless of how you move it, because otherwise the physics engine has to rebuild the colliders. @Superrodan: that's not how Translate works. It's just relative movement; look it up in the docs.
I haven't tried building without the collider (can't build now since I have apple at my office) but when it was jittery in editor, I tried disabling the collider there and it didn't really do much.
Random time intervals are not too short and it is clearly visible when the square changes the direction.
why would one store Vector3 in a tempDirection when it can be directly passed like this?
transform.Translate (directionX * moveSpeed * mySmoothDelta,
directionY * moveSpeed * mySmoothDelta, 0);
@Eric5h5 No, I'm using regular Update(). Are you talking about the fact that if it's not rigidbody it redraws all the colliders in the scene on every frame? I've heard of this but since I only have one square on the entire scene and moving with velocity didn't help I didn't put the rigidbody on since I didn't see the need for it. So you're saying I should just put the rigidbody2d component anyway despite the fact that I'm neither using velocity nor any rigidbody component parameters? (should I put all of them on 0 then?)
I apologize for my misunderstanding of Translate. I misread the documentation.
On the desktop, you should turn on vsync for best results, and don't use Application.targetFrameRate. On iOS, it's impossible to disable vsync, and ins$$anonymous$$d targetFrameRate is used to set whether you're using 30 or 60fps, though ideally you should use 60. But yes, you should always use a rigidbody when moving a collider, under any circumstances. Not that it would make a noticeable difference in the case you describe, but you should make a habit of it. Also I wouldn't bother with any kind of smoothed deltaTime; Time.deltaTime is enough. Seems like fiddling with it would lead to inaccuracies.
Answer by Bigproblem01 · Jan 21, 2015 at 12:59 PM
I tried using Application.targetFrameRate = 60 this morning and that appears to have fixed the issue. Turns out iOS always defaults to 30 frames per second because when on 60 fps, if there's a drop in frame rate it's very noticeable... anyway, the issue's fixed.
It fixed my trouble. I had some very little lag when moving 2d sprite and when I've used this line Application.targetFrameRate = 60;
it did the trick. I'll test this out on Android or IOS to see if still work.
Your answer
Follow this Question
Related Questions
Pixelated sprite when changing position 0 Answers
Lag in scene view 3 Answers
Moving 2D collider and lag 1 Answer
Jitter on object falling down on PC and android both. 0 Answers
Moving sphere tremble in moving camera view[Solved] 0 Answers