x


Why is the motion jerky in a simple 2D game?

I've been exploring the possibility of using Unity to build a 2D game. I'm looking at a number of 2D helper libraries, including ex2D and Orthello. The problem is: When I run the example projects that come with these libraries, the motion of the sprites is jerky. (The problem exists with both of the libraries, and actually just with Unity in general - more on that below.)

Let me explain what I mean: Every 1-2 seconds, there's very subtle but noticeable "blip" in the motion of the graphics. The hiccup is like a metronome; the motion is perfectly smooth for 1 second, and then for 1 instant (a single frame probably), the sprite noticeably jerks very subtly. Then it's smooth again for 1 second, then a frame blip, etc., ad infinitum. The jerky movement is probably only a matter of a pixel or two; you have to watch carefully to see it.

I dug a little deeper and created a simple Unity game from scratch with an orthographic camera and cube (and nothing else -- no 2D libraries, no physics, no nothing), and sure enough, the motion is still jerky. Every 1-2 seconds, like clockwork, a hiccup occurs in the motion.

I come from a Microsoft XNA background, and I experienced a problem just like this with XNA once upon a time. The solution was to disable "FixedTimeStep" for the game, and then the game ran perfectly smoothly. Is there a similar setting in Unity? I found the Time settings, but there's seemingly no way to disable fixed timestep.


For those curious, here are the objects/code for my simple project that exhibits the jerky animation behavior:

  • Main Camera: Orthographic, size 2, position(0,0,-1)
  • Cube: position(0,0,0)

And then this C# script on the Cube:

using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour
{
    protected float min;
    protected float max;
    protected float speed;

    void Start ()
    {
        speed = -0.01f;
        min = transform.position.x - 2f;
        max = transform.position.x + 2f;
    }

    void Update ()
    {
        transform.position = new Vector3(
            transform.position.x + (Time.deltaTime * speed),
            transform.position.y,
            transform.position.z
        );

        if (transform.position.x < min || transform.position.x > max)
            speed *= -1;
    }
}

EDIT:

As the initial responses correctly pointed out, in my original example code I completely forgot to multiply by Time.deltaTime. But here's the weird thing: Multiplying by Time.deltaTime actually makes the problem more pronounced! I just updated my code to include Time.deltaTime, since that doesn't seem to be what's causing my specific problem. (Regardless, thanks for your responses Eric5h5 and malraux.)

More information: I just discovered that building the game (instead of playing it in the Unity editor) and running it at "Graphics quality: Fastest" seems to eliminate the motion jitter. The problem still exists if I run it at "Good" quality or higher. My video card is an Nvidia 8800GT with up-to-date drivers. I know it's not the greatest card in the world, but it runs plenty of other 3D games at decent quality with no frame rate hiccups. And my example project is about as simple as you can get: a single untextured cube moving back and forth.

Any other thoughts?

more ▼

asked Mar 16 '12 at 06:12 AM

theremin gravatar image

theremin
336 6 8 10

Your code is frame-rate dependent; you need to use Time.deltaTime. Physics can't be completely turned off, but you can set the fixed timestep to 10, which is the maximum allowed. Although if you have no physics, that won't really make much difference.

Mar 16 '12 at 02:04 PM Eric5h5

Argh, yes, I had tested that previously but forgot it in this example code. Here's the weird thing: Correctly multiplying by deltaTime actually makes the problem more pronounced! I'm beginning to think that there must be some specific problem with my video card (Nvidia 8800GT) and fixed timesteps. It's truly bizarre. Regardless, thanks for your response!

Mar 16 '12 at 03:14 PM theremin

Maybe turn vsync on.

Mar 16 '12 at 04:39 PM Eric5h5

OK, the VSync setting definitely has an effect. With VSync on ("Every VBlank"), the stutter problem is pretty bad. With VSync off, the stutter problem virtually disappears completely. (That's odd, I would've thought it would be the reverse.) I'm not going to answer my own question and accept the answer, so if someone would like to answer with something about VSync (preferably with some ideas about why it's causing this problem on my system), I'll gladly accept your answer. Thanks again for all of the responses.

Mar 16 '12 at 05:06 PM theremin

Hi All,

I was wondering if you ever got to the bottom of this problem? I am using the follow code to move the object and camera.

var Speed : float = 28.0;

function FixedUpdate ()

{

transform.position += transform.forward * Speed * Time.deltaTime; }

I can not make it any more simple, I have tried it on two pc's and on a mac book, and still I get jerky steps every 2 seconds. I have tried all the differnt update types. All that I can proove is that when I remove the delta time all works well. I am at my witts end with this problem. I am now going to look at using the constant from a RTC and forget this delta time has some kind of joke, I do know that if I load an old copy of Unity I do not have the problem. Can anybody throw any light on this problem. What I was thinking, is to read the delta time before each movment and remove any larger fluctuations that happen within a percentage of say 1 second intervals by avarage mearsurments, I will give this a shot and see if it can be smoothed somehow. Thanks All Dean

Oct 20 '12 at 01:03 PM iphonedeano
(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

So, this problem has been plaguing me for weeks, and I finally just solved it... sort of. My project has gone through a number of changes, so this solution doesn't necessarily apply to the question exactly the way I asked it. But I'm going to post this information here anyway, because I've never seen this solution mentioned in any of the jitter/judder/jerky movement questions about Unity. Hopefully this solution will help someone else out.

First of all, I'm now using Physics and Rigidbodies. And I still had the motion judder/jitter problem with a Rigidbody and FixedUpdate. But if you're using Physics, this solution may help you:

It all comes down to one little setting on the Rigidbody: Interpolate.

http://unity3d.com/support/documentation/ScriptReference/Rigidbody-interpolation.html

Setting the Interpolate property to Interpolate makes my object move completely smoothly now. Hope that helps someone else.

more ▼

answered Apr 20 '12 at 10:53 PM

theremin gravatar image

theremin
336 6 8 10

(comments are locked)
10|3000 characters needed characters left

I am currently having this problem. I hadn't noticed it until I got further along in my project, with more and more units on my screen.

It does seem heavily vsync/monitor refresh rate related. I get little frame "blips"/jittering about every 2-4 seconds, and it's really noticeable. At my flatscreen lcd monitors refresh rate of 60 hz, it's pretty bad jittering. When I switch to 100 hz refresh rate on my monitor, it gets a lot smoother, although it doesn't eliminate the "blipping" completely -- it's still there.

The weird thing is that if I set my monitor refresh rate to 120 hz, it's almost as bad as at 60 hz. I am completely stumped. I've tried everything in this thread, and it helps smooth it out quite a bit, but again it's never completely eliminated. Even with setting my models rigidbodies to interpolate as suggested, and setting my monitors refresh rate to 100 hz, and setting vsync to off in my project settings.

Any leads as to how to fix this would be great. I know this is an old thread though. It is currently 8/15/2012 lol. Unity version 3.5.4f1. I'm beginning to think it's something the Unity programmers changed in the vsync option in one of the recent updates.

Update: I just tried building my game with settings on "fastest" in the project quality settings. Then selecting "fastest" in the selection tab upon starting my game up(with it built). This pretty much does work. I noticed almost no jittering/blipping -- possibly some very miniscule blipping, but almost unnoticeable. This is great! But the question is why? We shouldn't have to run our game on fastest in order to eliminate this terrible vsync blipping or whatever it is. Anyways, for now, at least there's one way to minimize it almost completely. Thanks for all the tips and advice so far.

more ▼

answered Aug 16 '12 at 04:08 AM

Velo222 gravatar image

Velo222
15 1 1 2

As far as I can tell, it's absolutely vsync/monitor refresh rate related. You can read about the problem in much greater detail in an updated question that I posted here:

http://answers.unity3d.com/questions/275016/updatefixedupdate-motion-stutter-not-another-novic.html

I don't think it has anything to do with game complexity; I see the stuttering problem in the absolute simplest possible game. (You can download a sample game project via the link above.) The problem is definitely most severe with VSync enabled. I'm convinced not everyone sees the issue. Perhaps only certain video cards and/or monitors are affected? I have an Nvidia GeForce 8800, how about you?

Aug 16 '12 at 04:30 AM theremin
(comments are locked)
10|3000 characters needed characters left

I had a problem with my Unity iPhone game freezing every few seconds. It made the animations "jerky". After I tried everything from this thread and got rid of all the objects in the game it was still freezing. Then I changed from "Portrait" to "Landscape Right" as default orientation and all was ok. If I switch back, it's jerky again.

more ▼

answered Jun 28 '12 at 12:01 AM

A.Small gravatar image

A.Small
0

(comments are locked)
10|3000 characters needed characters left

As Eric pointed out, use deltaTime:

void Start ()
{
    this.speed = -1f; // note I changed the name to reflect the usage
    min = transform.position.x - 2f;
    max = transform.position.x + 2f;
}

void Update ()
{
    transform.position.x += this.speed * Time.deltaTime;

    if (transform.position.x < min || transform.position.x > max)
        this.speed *= -1;
}

Note that I'm calling it speed now... deltaTime gives you the difference in time since the last update. Multiply that by a speed and you are interpolating the position value based on the speed, since speed (velocity) is distance over time.

Also, if you're using a RigidBody, make sure to use FixedUpdate instead of Update: MonoBehaviour.FixedUpdate

more ▼

answered Mar 16 '12 at 02:20 PM

malraux gravatar image

malraux
30 1 1 3

Argh, yes, I had tested that previously but forgot it in this example code. Here's the weird thing: Correctly multiplying by deltaTime actually makes the problem more pronounced! I'm beginning to think that there must be some specific problem with my video card (Nvidia 8800GT) and fixed timesteps. It's truly bizarre. Regardless, thanks for your response!

Mar 16 '12 at 03:15 PM theremin

@theremin Did you try it in FixedUpdate?

Mar 16 '12 at 04:31 PM malraux

Good question, I hadn't, but I just did. As it turns out, FixedUpdate produced the worst results of all: the motion jitter switches from once per second to pretty much constant. It was a worth a try though, thanks for the idea. As it turns out, it seems that having VSync enabled might be the culprit. (See my comment above.)

Mar 16 '12 at 05:10 PM theremin
(comments are locked)
10|3000 characters needed characters left

I have the stutter problem bug in the editor still to this day. It happens randomly but sometimes I need to just close the editor then re-open it, and the stutter goes away.

EDIT : This post is old, don't ever get these issues anymore.

more ▼

answered Mar 16 '12 at 05:29 PM

Meltdown gravatar image

Meltdown
5.6k 18 25 49

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1071
x96

asked: Mar 16 '12 at 06:12 AM

Seen: 3247 times

Last Updated: Jan 24 at 08:49 PM