• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
18
Question by picobots · Mar 16, 2012 at 06:12 AM · 2dorthographic

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?

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Mar 16, 2012 at 02:04 PM 0
Share

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.

avatar image picobots · Mar 16, 2012 at 03:14 PM 0
Share

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!

avatar image Eric5h5 · Mar 16, 2012 at 04:39 PM 1
Share

$$anonymous$$aybe turn vsync on.

avatar image picobots · Mar 16, 2012 at 05:06 PM 0
Share

O$$anonymous$$, 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.

avatar image iphonedeano · Oct 20, 2012 at 01:03 PM 0
Share

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

Show more comments

12 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by volkswagenb · Jun 22, 2013 at 12:41 AM

I know this is an older thread, but I hope this helps somebody out:

I initially thought the jerkiness problem was related to Update() running in different threads by Unity, so I changed my objects to plain classes and update them manually from my "main", but this did not solve it.

The jerkiness problem is really related to variations in the frame rate, that's why -as far as I've seen- it happens regardless of the 'sync' scheme or Update vs FixedUpdate.

Let's say the last frame took for example 50ms, and you use that deltatime for your motion, but the actual current frame takes only 25ms to render, it will look to you as if the object jumped. This is particularly more evident if you have object 'seek' routines as I have, or if you're running lower framerates.

You can do this:

     Time.maximumDeltaTime=0.03; //<<just over your estimated average frame time.
     //or alternatively:
     t=Time.deltaTime;
     if(t>0.03){t=0.03;}//constrain it
     //Note that they also have Time.smoothDeltaTime, but it's not much help.

Now it's smooth as silk. What I'll probably do for a more robust solution is to keep a 1-second moving average of the frame rate, and then constrain 't' to that.

Edit: Sorry about the redundancy, I hadn't seen Deano's correct response.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Karmak · Jan 25, 2014 at 01:42 AM

Try to switch the v-sync option to "Don´t Sync". It helped a ton in my case.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by thekingofclubs · Aug 30, 2014 at 07:37 PM

What helped me is setting the Maximum Allowed Timestep to 0.2 under Edit - Project Settings - Time. Default is 0.3333... Basically, this sets the maximum allowed deltaTime.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 30, 2014 at 07:39 PM 0
Share

$$anonymous$$aximum Allowed Timestep deals with 'fixedDeltaTime' not 'deltaTime'.

http://docs.unity3d.com/$$anonymous$$anual/class-Time$$anonymous$$anager.html

avatar image
0

Answer by Mohamed Adly · Oct 29, 2014 at 03:20 PM

Change the value o FPS using the following code

Application.targetFrameRate = 50;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Xcruciate · Jan 09, 2015 at 06:09 PM

Okay after a long time I figured it out!. (this is a workaround!! this method worked for me. It might not work for you but the logic remains the same)

So basically Unity works in units and not pixels, however a 2d sprite is a bunch of pixels which unity scales to certain units. this is called the pixel to unit ratio when you have a high pixel to unit ratio it means you have dense pixels and since every movement that you do is in units it causes those entire bunch of pixels to move. Hence the stutter. For Eg. 100 pixel to Unit(PtU)means 100 pixels move at once. so whether you do updqate fixed update or lateupdate doesnt matter !

so to work around this is to make the size of the camera huge and decrease the pixel to ratio so that the movement evens out. keeping it around 20 PtU works smoothly. you can play with it and see which PtU suits your game. if your game is fast reducing it to 10 might to the trick. Try different values of PtU!

This might not work but it certainly did for me! i can now make a fast paced 2d game!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fadden · Nov 20, 2015 at 07:51 PM 0
Share

This only applies if you're moving in whole units. At 100 ppu, you can shift something by 0.5 units, and it will move 50 pixels.

  • ‹
  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is this pixel art not lining up correctly? 1 Answer

2D Animation does not start 1 Answer

Orthographic camera movement clamping 1 Answer

The infuriating Orthographic camera Vs Android Resolution. 2 Answers

Questions about Unity 2D camera setup 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges