Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Pixel Perfect Camera Jitters

Discussion in '2D' started by V_Raven, May 22, 2024.

  1. V_Raven

    V_Raven

    Joined:
    Aug 26, 2022
    Posts:
    3
    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:
    1. How can I eliminate jittering in my pixel-perfect setup?
    2. 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.
     

    Attached Files:

    Last edited: May 22, 2024
    atsrml and MeniaGames like this.
  2. MeniaGames

    MeniaGames

    Joined:
    Jan 30, 2024
    Posts:
    1
    I am experiencing the same situation. Please find a solution to this problem.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    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: https://forum.unity.com/forums/cinemachine.136/

    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

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    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.
     
  4. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    243
    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:
    1. disable everything related to cinemachine in the test scene
    2. make the camera's parent null
    3. add the PixelPerfectPlayerFollower script to the camera
    4. 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
    5. 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.
    6. Make sure your game view is in 1x and actual integer multiple of base resolution is selected
    7. 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

    *https://forum.unity.com/threads/uni...ter-player-stops-moving.1546532/#post-9641324
     
  5. V_Raven

    V_Raven

    Joined:
    Aug 26, 2022
    Posts:
    3
    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:
    1. The Cinemachine camera moves to sub-pixel positions due to damping values.
    2. 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:

    Code (CSharp):
    1.     private Vector2 SnapToGrid(Vector2 position, float ppu) {
    2.         float gridUnit = 1f / ppu;
    3.         float x = Mathf.Round(position.x / gridUnit) * gridUnit;
    4.         float y = Mathf.Round(position.y / gridUnit) * gridUnit;
    5.         return new Vector2(x, y);
    6.     }
    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.
     
    Last edited: May 22, 2024
    MeniaGames likes this.
  6. V_Raven

    V_Raven

    Joined:
    Aug 26, 2022
    Posts:
    3
    Thanks for suggestions. I will be trying them as soon as possible.
     
    MeniaGames likes this.