Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Do sprites jitter in 2D when moving? Is that how it's supposed to be?

Discussion in 'Editor & General Support' started by Kundabufer, Aug 31, 2023.

  1. Kundabufer

    Kundabufer

    Joined:
    Mar 27, 2023
    Posts:
    4
    I'm creating a 2D platformer or runner-type game, which means there's a player and platforms in the scene. The platforms jitter when moving. I'm implementing movement in every possible way:




    1. Moving the player and having a CinemachineVirtualCamera follow it (when using main.camera to follow, the result is the same in all cases). I'm moving using rb.velocity or transform.position with *Time.deltaTime and *Time.FixedDeltaTime, both with and without them. I'm trying this in FixedUpdate, Update, and LateUpdate. The player is floating in the air and doesn't touch the platform. The result is the same: jittering.

    2. Moving the platforms while the player is floating in the air and not moving, and the camera follows accordingly. I'm moving using rb.velocity or transform.position with *Time.deltaTime and *Time.FixedDeltaTime, both with and without them. I'm trying this in FixedUpdate, Update, and LateUpdate. The result is the same: jittering.

    3. Creating a new project. Adding 1 object and writing a script:


    Code (CSharp):
    1. public float horizontalMove = 10f;
    2.  
    3.  
    4.  
    5. Rigidbody2D rb;
    6.  
    7.  
    8.  
    9. void Awake()
    10.  
    11. {
    12.  
    13.     Application.targetFrameRate = 60;
    14.  
    15.     rb = GetComponent<Rigidbody2D>();
    16.  
    17.     rb.constraints = RigidbodyConstraints2D.FreezeRotation;
    18.  
    19. }
    20.  
    21.  
    22.  
    23. private void FixedUpdate()
    24.  
    25. {
    26.  
    27.     rb.velocity = new Vector2(horizontalMove, rb.velocity.y);
    28.  
    29. }


    When moving, the sprite sometimes jitters on both the computer and the phone.



    Application.targetFrameRate = 60; the result is the same with other values, like 120. Quality Vsync Count = Don't Sync for Android; changing this setting doesn't affect the result. Fixed Timestep = 0.0143 or 0.02 or even 0.002 doesn't affect the result. Disabling Event System and Canvas also doesn't affect the result.



    After recording a video at 60fps from the computer, I noticed the following: During smooth movement, the sprites should shift by 1*N pixels each frame. For example, a shift of 10 pixels should happen every frame: 10-10-10-10-10-10-10-10-10. However, sometimes the change in position isn't drawn, and then it catches up. This means that the shift doesn't occur every frame; sometimes there's no shift, and then it shifts two positions: 10-10-10-10-10-00-20-10-10-10. This appears as jittering. And this doesn't happen with a constant periodicity; sometimes it occurs, meaning it might jerk once in 5 seconds, and then blink 5 times in 2 seconds.



    This is evident on the computer and in the build on the phone.



    However, when I touch the phone screen, everything moves smoothly, without any jittering!!!



    The jittering returns only after releasing the touch. Touches aren't handled anywhere.



    What am I missing? What else can I try? Please help me understand what to do or if this behavior is normal.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Jittering is not normal behavior. Try removing the Application.targetFrameRate in Awake, ive never seen a new dev use that. Your framerate will be fine if you're making a platformer. Also, try multiplying your horizontalMove amount by Time.fixedDeltaTime where you are changing the velocity.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,528
    And you're using interpolation on the Rigidbody2D so you get per-frame updates correct? Have you tried not using physics and move without it using the Transform as a test? This will at least see if the stalling is because something else is running.

    Sprite in of themselves don't "jitter" so the question is a bit odd TBH. Smooth movement comes from consistent uninterrupted decent frequency updates, not an inherent problem with a Unity component.

    Focus your efforts on checking this.
     
  4. Kundabufer

    Kundabufer

    Joined:
    Mar 27, 2023
    Posts:
    4
    I removed Application.targetFrameRate = 60; - besides the rare stutter of a lagging frame, a constant jitter appeared when building for the phone, making the visuals flicker. That's why I added it back into the code. Initially, the settings are FixedTimestep = 0.02, Maximum FixedTimestep = 0.33. With these settings, the game displays at 30 fps on the phone and jitters even more than with FixedTimestep = 0.0143, Maximum FixedTimestep = 0.0143, or FixedTimestep = 0.01. That's why I set Application.targetFrameRate = 60; which solved the issue with the constant jitter, but the problem of occasional jitter remains, occurring at irregular intervals. Time.fixedDeltaTime - didn't make any difference.
     
  5. Kundabufer

    Kundabufer

    Joined:
    Mar 27, 2023
    Posts:
    4
    Interpolation - the result is the same, extrapolation - the result is the same, Transform.position with *Time.deltaTime - the result is the same
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,528
    In the end then, this is nothing related to sprites or physics and something else in the build; you mention sprites jittering but that's simply because it's showing you the position, you could render anything.

    Occasional iterruptions can be caused by many things, not least of which is the Garbage Collector kicking in. You can use the profiler to figure this kind of stuff out. You should try to do a basic test with an empty build doing nothing to cause GC waste and to isolate other things i.e. minimal packages installed etc.

    I'll move your thread to the Editor and General Support sub-forum but leave a redirect here for you.
     
  7. Kundabufer

    Kundabufer

    Joined:
    Mar 27, 2023
    Posts:
    4
    Turning off 'Use incremental GC' didn't help.
    Code (CSharp):
    1. void Start()
    2.     {
    3.         GarbageCollector.GCMode = GarbageCollector.Mode.Disabled;
    4.     }
    - didn't help.
    Image jittering continues to occur no matter what I do.

    This result is consistent across an empty project, on two computers, two phones, and three versions of Unity. Creating a new project, adding one sprite, one script - the same jitters occur whether using physics or transform. Strangely, when touching the phone screen, they disappear, and everything moves smoothly, with shifts happening every frame. If I keep my finger on the phone screen constantly, everything works perfectly!