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) :
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.
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;
}
}
}
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