Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug When a Pixel Perfect Camera is moving the sprites are very jittery.

Discussion in '2D' started by mezocsongor, Aug 12, 2023.

  1. mezocsongor

    mezocsongor

    Joined:
    Nov 5, 2022
    Posts:
    3
    When a camera with pixel perfection enabled is following the player the sprite starts to jitter/shake in a highly visible way.

    I tried pixel-snapping (through code) movement of both the character and the camera, messing around with rigidbody2D settings, and also jittered with non-rigid-body-based movement.

    A video of the problem:


    This is what my camera script looks like:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour
    6. {
    7.     [Header("References")]
    8.     public Player2 player;
    9.  
    10.     [Header("Stats")]
    11.     public float smoothTime;
    12.     public float mouseOffsetMultiplier;
    13.  
    14.     private Vector2 velocity;
    15.  
    16.     private void LateUpdate()
    17.     {
    18.         // player.MouseVector is just a vector from the player to the mouse
    19.         Vector2 target = (Vector2)player.transform.position + player.MouseVector * mouseOffsetMultiplier;
    20.         Vector2 position2D = Vector2.SmoothDamp(transform.position, target, ref velocity, smoothTime);
    21.         transform.position = (Vector3)position2D + new Vector3(0, 0, transform.position.z);
    22.     }
    23. }
    24.  
    This are my Pixel Perfect Camera settings:
    upload_2023-8-12_18-15-7.png
     
  2. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    73
    I wonder have you try doing this.

    Code (CSharp):
    1.     private void LateUpdate()
    2.     {
    3.         Vector2 target = (Vector2)player.transform.position + player.MouseVector * mouseOffsetMultiplier;
    4.         transform.position = new Vector3(target.x, target.y, transform.position.z);
    5.     }
    If still don't work it will be good to provide a small sample of the project.
     
  3. mezocsongor

    mezocsongor

    Joined:
    Nov 5, 2022
    Posts:
    3
    Well it doesn't work, the character is shaking a lot even this way. I actually already solved my problem by creating my own "pixelate" shader instead of using the Pixel Perfect Camera component and it works well.

    The jitter with the Pixel Perfect Camera happens even when I just parent the camera to the player and use no camera script. I assume the Pixel Perfect Camera component might be only designed for still cameras (I have no idea how it works).
     
  4. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    73
    Hi, are you able to provide a small sample of your project for this issue?