Search Unity

Camera movement with pixel perfect extension

Discussion in 'Cinemachine' started by Zephus, Sep 18, 2020.

  1. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    I'm trying to get my camera to move smoothly in my pixel art game. I've added the Pixel Perfect Camera and the Pixel Perfect extension to Cinemachine. It works when standing still, but I'm having problems when the camera is actually moving, since the smooth camera motion leads to uneven numbers that interfere with my pixel perfect player movement.

    I'm rounding the player position to increments of 0.0625 (1/16), so that it's always on a position that can be rendered perfectly to the screen. But when Cinemachine is following or zooming I get numbers like 0.012371. This means that I get some kind of jittering, or even worse - seems between my tiles when zooming.

    I'm looking for a way to tell Cinemachine to only move on set increments as well. My PPU is set to 16, so just like the player, I need the camera to only move in steps of 0.0625. Is there some way to do this?
     
  2. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    A video or a simple repro project would be helpful here.
     
    Gregoryl likes this.
  3. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    So even with video this is hard to describe, but I'll try. This is the Scene without Cinemachine, using a Pixel Perfect Camera as a child of the player:


    The motion is as smooth as I could get it. The player moves with velocity, but that is then rounded to the nearest multiple of 1/16 (since my PPU is 16). So I get nice numbers like these:
    upload_2020-9-24_23-44-21.png

    Without the rounding there would be jittering, as the character's position would be in between pixels, but the camera then rounds it to be pixel perfect. That creates very uneven movement, which is why I'm directly rounding the movement, so the character's position is never outside of the 'grid'.

    Now I want the features of Cinemachine, but keep this pixel perfect movement. This is with Cinemachine and its Pixel Perfect extension enabled:


    It might be subtle, but when the camera is smoothing between the rest positions the character appears to be jittering. You can especially see this at 0:02 when you're focussing on the frog's head. This would get a lot worse if I were to add deadzones and so on.

    The reason for this is that Cinemachine does not adhere to the 'grid' and reaches positions such as this one:
    upload_2020-9-24_23-44-9.png

    While the Pixel Perfect camera makes sure everything is rendered correctly, now the camera has the same problem the character had when I didn't round his position. For Cinemachine to not only render pixel perfect, but also move without jittering, it can never have any position that isn't a multiple of 1/16.

    And this is what I'm looking for. A way to round Cinemachine's position to the nearest multiple of 1/16, just like the character. Because, as far as I can see, Cinemachine can never be truly 'pixel perfect' if there's not rounding depending on the PPU.
     
  4. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    What happens if you don't do any rounding at all? Because internally the Pixel Perfect Camera rounds the position of both the camera and Sprite Renderers. It does this rounding deep on the render pipeline level, so the Transform positions remain unchanged.
     
  5. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    Sorry, I completely forgot about this thread.

    If I don't do any rounding then I completely lose the pixel perfectness while the camera is moving. Which leads to basically everything jiterring:


    The frog is moving with velocity, the Camera has a Pixel Perfect Camera that is working and a CinemachineBrain, and the CM Virtual Cam has the Pixel Perfect Extension.
     
  6. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    Is it possible for you to share a simple repro project with me?
     
  7. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    I can't post it here, but I've sent you a simple project in a private message.
     
  8. AlexGone

    AlexGone

    Joined:
    Feb 11, 2022
    Posts:
    5
    Hey Zephus,
    i know im 2 years late.
    Could you figure out what was the problem? Did you find a solution in the end?
    I would really love to know your approach on this and how you setup your code to only move by grid.
     
    dembidy likes this.