Jittery looping background

Hello Everybody,

I’ve been having this problem with a loopingBackground that gets jerky every seconds or so, as if it’s paused and generates again after a smooth movement.

Here is a video showing the problem (uploaded with a phone because using the screenrecorder adds some lag of its own) :

Video with a screen recorder : VID-20240409-WA0002.mp4 - Google Drive

Background parameters :Imgur: The magic of the Internet

Here is the code of the looping background :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class loopingBackground : MonoBehaviour
{

     public float backgroundSpeed;
    public Renderer backgroundRenderer;
 

    private void Update()
    {
   
        backgroundRenderer.material.mainTextureOffset += new Vector2(backgroundSpeed * Time.deltaTime, 0f);
    }

}

I also tried this from a member who have been having the same problem, to no avail.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class loopingBackground : MonoBehaviour
{

     public float backgroundSpeed;
    public Renderer backgroundRenderer;

   
    private void Update()
    {
        Vector2 offset = backgroundRenderer.material.mainTextureOffset;
        offset.x += backgroundSpeed * (Time.deltaTime / 10);
        offset.x = (offset.x) % 1;
        backgroundRenderer.material.mainTextureOffset = offset;
        backgroundRenderer.material.mainTextureOffset += new Vector2(backgroundSpeed * Time.deltaTime, 0f);
    }

}

Whether i place the code in Update or LateUpdate, whether it’s Time.deltaTime or Time.smoothDeltaTime there is still the same jittery movement every seconds or so.

I’ve joined the video as an example but the recording app adds way more lag on its own. Without it the movement is generally smoother, but every 3/4 seconds or so there is this jittery/impression of new generation movement.

If anyone has any idea or hint of where the problem may come from and in which direction to look to correct it, that would be very helpful.

Thank you in advance.

Update the texture offset in the Update loop seems ok for me.
Are you sure the render is smooth ? I mean, does the framerate is constant ? I’m asking because if there are some framerate stuttering (some frames takes double time to others for example), this can cause this visual artefact.

Sorry for the noob question, but how can i check that ?

I’m using this code to check the framerate and it gives me a stable framerate.

using UnityEngine;
using TMPro;

public class FPSDisplay : MonoBehaviour
{

    public TextMeshProUGUI FpsText;
    private float pollingTime = 1f;
    private float time;
    private int frameCount;


    // Update is called once per frame
    void Update()
    {
        time += Time.deltaTime;
        frameCount++;

        if (time >= pollingTime)

        {
            int frameRate = Mathf.RoundToInt(frameCount / time);
            FpsText.text = frameRate.ToString() + " FPS";

            time -= pollingTime;
            frameCount = 0;

        }

    }
}

I uploaded a video filmed with a phone (because using a screen recorder adds a lag of its own) : https://drive.google.com/file/d/1G3Hktsxpdx9HSTnXPJApl7FVyBkj1kGH/view?usp=sharing

I’ve tried on 2 different phones, the same problem happens in both, so it can be just a problem with a specific hardware.

try using lateUpdate

Already done, i’ve used both the codes in LateUpdate and still the same problem, even when i get the project to really the basics, it’s still jerky.

But thanks.

So is there really no solution for making a simple looping background not jittery ?

I’ve scrolled the whole of the internet, searched every tutorial, found this Timesteps and Achieving Smooth Motion in Unity — KinematicSoup Technologies Inc. tried to applied it but then realized that in my code there is only a looping background, no different objects moving in Update or FixedUpdate, and i still can’t find the slightest hint about the origin of the problem.

Really bizarre since i’ve scratched the whole project to the utter basics : only the looping background, nothing but the looping background, with very low resolution images, different speed, and yet the max that happens is making the jitter less jittery.

I can only give you some general advice, I’m afraid:

  • Never test performance in editor - always make a standalone build
  • Make sure the game reaches 60 FPS constantly and enable v-sync. On one of the videos you only get 45 FPS or so which means your game updates won’t match up with the screen refresh rate. (on mobile, it probably makes more sense to target 30 FPS and set QualitySettings.vSyncCount to 2)
  • Make sure that the background position doesn’t get rounded to integers somewhere
  • Use the profiler to check for spikes.
  • Use object pools to avoid dynamic memory allocations. Garbage collection can cause spikes
  • Read the official Unity eBook about performance optimization

Unfortunately, jitters are particularly noticeable on fast scrolling backgrounds.

If it’s any consolation, I can’t see it at all. :slight_smile:

you’re not the only one :smile: