Pixel jitter/flicker while camera movement (2D)
While our player camera is moving the pixels of the sprites and tiles seem to flicker (like in the example below). We use rounding routines that reduces this effect a bit but not completely.
(The flickering is stronger for showing purpose)
Orthographic Size Rounding Routine (zoomStrength is an integer multiplier, ppu is 200)
private float _RoundToNearestPixel_OrtographicSize(float _zoomStrength)
{
_zoomStrength = _zoomStrength * Screen.height / (pixelsPerUnit * 2f);
return _zoomStrength;
}
Pixel Rounding Routine
private Vector3 _RoundToNearestPixel_Position(Vector3 currentPosition)
{
// Variant A
float screenPixelsPerUnit = Screen.height / (camera_IndexLayer.orthographicSize * 2f);
currentPosition.y = Mathf.Round(currentPosition.y * screenPixelsPerUnit) / screenPixelsPerUnit;
currentPosition.x = Mathf.Round(currentPosition.x * screenPixelsPerUnit) / screenPixelsPerUnit;
// Variant B
//currentPosition.y = _RoundToNearestPixel_Helper(currentPosition.y, camera_IndexLayer);
//currentPosition.x = _RoundToNearestPixel_Helper(currentPosition.x, camera_IndexLayer);
return currentPosition;
}
private float _RoundToNearestPixel_Helper(float unityUnits, Camera viewingCamera)
{
float valueInPixels = (Screen.height / (viewingCamera.orthographicSize * 2)) * unityUnits;
valueInPixels = Mathf.Round(valueInPixels);
float adjustedUnityUnits = valueInPixels / (Screen.height / (viewingCamera.orthographicSize * 2));
return adjustedUnityUnits;
}
More informations:
No lag spikes in the profiler
the game runs with constant 120 fps
Camera runs in Update()
Tile size is 256px, PPU is 200
(Quality Settings)
(Camera Settings)
Do you have any idea?
Answer by Cody-Rauh · Apr 16, 2020 at 07:19 PM
Did you ever get help with this, get a solution? I am having the exact same problem.
Answer by insoluzioni · Aug 18, 2020 at 03:34 AM
Hello, it looks like activating Pixel Perfect solves this problem. But I'm looking for a solution without it, because it drops my FPS to 30-40 while moving / changing the ortographic size.