Introducing deliberate hitches is a common technique to test how your game handles performance issues like lag spikes, frame drops, stuttering, or degraded UI responsiveness under stress.
The following is a simple MonoBehaviour script that you can attach to any GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary in your sceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary. It causes the main thread to hang at regular intervals, simulating a hitch or spike in frame time. You can easily adjust the frequency and duration of the hitches for your testing.
using UnityEngine;
using Unity.Profiling;
public class HitchSimulator : MonoBehaviour
{
static readonly ProfilerMarker k_HitchSimulatorMarker = new ProfilerMarker("[HitchSimulator] CauseHitch() introducing artificial hitches");
[Tooltip("How often (in seconds) to cause a hitch.")]
public float hitchInterval = 5f;
[Tooltip("How long (in milliseconds) each hitch should last.")]
public int hitchDurationMs = 200;
private float _nextHitchTime = 0f;
void Update()
{
if (Time.time >= _nextHitchTime)
{
Debug.LogWarning($"[HitchSimulator] Causing a hitch for {hitchDurationMs} ms at {Time.time:F2}s");
CauseHitch(hitchDurationMs);
_nextHitchTime = Time.time + hitchInterval;
}
}
void CauseHitch(int durationMs)
{
// Adding a clarifying profiler marker so no one accidentally confuses this for an actual issue.
k_HitchSimulatorMarker.Auto();
float start = Time.realtimeSinceStartup;
// Busy wait for the specified duration
while ((Time.realtimeSinceStartup - start) < durationMs / 1000f)
{
// Just spin
}
}
}
To use the script:
HitchSimulator.cs
.hitchInterval
: How often to hitch. Default is 5 seconds.hitchDurationMs
: How long each hitch lasts. Default is 200 millisconds.Note: This script blocks the main thread. It simulates a real lag spike from heavy computation, not just an artificial slowdown. This can make the Editor unresponsive if you use very large values. You can also trigger hitches via keypresses or random intervals for more robust testing.