Pixel Perfect Camera Jitters

Hello,

I’m having trouble understanding the pixel perfect camera in Unity. Although there are many threads about pixel-perfect implementation and jittering issues, none have fully clarified my questions.

My goal is to achieve pixel-perfect movement, where the actor moves smoothly within the pixel grid. I’ve set up a pixel-perfect camera based on this article, but during playtesting, I noticed that my camera or sprite suffers from jittering. I even tested the setup in a new Unity project using assets found online, but the jittering issue persisted.

Video that shows jittering. Pixel perfect on
Video that shows non jittery movement. Pixel perfect off

My specific questions are:

  • How can I eliminate jittering in my pixel-perfect setup?
  • How can I handle smooth acceleration and deceleration while ensuring my character snaps to the pixel grid?

I appreciate any help or guidance on this issue.

Thank you.



9849198--1417830--Snap Settings.png
9849198--1417842--Sprite Renderer Settings.png

2 Likes

I am experiencing the same situation. Please find a solution to this problem.

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

I mean if you are getting a pretty good result without using pixel perfect camera, why bother(apart from automatically calculating the orthographic scale etc)?

Cinemachine was not pixel perfect and/or smooth in my case, so I made my own camera follow script for 2d

Anyways, if everything else in your scene is set up properly* you can use this pixel perfect player follow script or adjust it to your liking:
https://gist.github.com/venediklee/1437f3c908cc135be10c4ddb2f23bec9

here is everything you need to do step by step:

  • disable everything related to cinemachine in the test scene
  • make the camera’s parent null
  • add the PixelPerfectPlayerFollower script to the camera
  • if your player(the one that moves) isn’t the only gameobject that has the player tag, make the _player field of PixelPerfectPlayerFollower public or [SerializeField], and assign the player in the editor to PixelPerfectPlayerFollower component of the camera
  • change the other exposed fields of the camera such that it fits your project. For example if your project’s ppu is 128, set the ppu field of the PixelPerfectPlayerFollower to 128 etc.
  • Make sure your game view is in 1x and actual integer multiple of base resolution is selected
  • enter play mode, move the player(either with your code or dragging in scene); observe the camera smoothly following the player

You can follow a similar logic with the script above, you’ll store and manipulate the actual player position when you get input. Then, in late update you’ll snap the player to the closest pixel perfect position. Be careful to NOT calculating anything based on player’s pixel perfect position; only calculate stuff based on player’s actual position

Hey Kurt,

I’m not trying to rotate the camera. All three rotation axes are maintained at “0” during play mode, so I don’t believe the issue lies with camera rotation. Here are some thoughts on the problem:

  • The Cinemachine camera moves to sub-pixel positions due to damping values.
  • The actor sprite does not completely snap to the grid because of acceleration and deceleration values.

Initially, I set the Cinemachine camera’s damping value to zero, thinking damping was the issue. However, I realized that the Cinemachine camera still jitters along with the actor. Here is a video demonstrating the issue. I’m not sure if my investigation is correct or if I’m entirely off track.

I also tried snapping the actor to the grid by rounding the actor’s position to the grid unit using the following code:

    private Vector2 SnapToGrid(Vector2 position, float ppu) {
        float gridUnit = 1f / ppu;
        float x = Mathf.Round(position.x / gridUnit) * gridUnit;
        float y = Mathf.Round(position.y / gridUnit) * gridUnit;
        return new Vector2(x, y);
    }

But this didn’t make any sense to me since pixel perfect camera already shifts the sprites to the pixel grid why would i try to shift the shifted position?

Although thanks for your reply.

1 Like

Thanks for suggestions. I will be trying them as soon as possible.

1 Like