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

Parallax Background Shakiness with Pixel Perfect Camera

Discussion in '2D' started by adscomics, Feb 1, 2021.

  1. adscomics

    adscomics

    Joined:
    Nov 2, 2017
    Posts:
    13
    So, before anyone says anything, yes, I have done a lot of searching regarding this issue, and I know it's not a recent one. I just can't seem to find something that works.

    I have a multi-layered parallax background. Its parallax is achieved by having 3 quads offset their texture by a different amount, depending on the camera's position in world-space. The issue is that there's a noticeable amount of shakiness when the background moves, especially when it slows down.



    Here's the code snippets that I've got for both the parallax, and the camera movement.

    Parallax Effect. Texture his offset by the position of the camera.
    Code (CSharp):
    1. void FixedUpdate () {
    2.         if (parallaxX){offsetX =  cameraTrans.position.x; } //only show parallax on X axis if this is set to true.
    3.         if (parallaxY){offsetY = cameraTrans.position.y; }// ^^ ditto but with Y axis
    4.         rend.material.SetTextureOffset("_MainTex", new Vector2( offsetX/offsetXAmmount,offsetY/offsetYAmmount));
    5.     }
    Smooth Camera (this snippet is handled in FixedUpdate()). The camera lerps its position to the player to create a smooth movement. The background shake is most prominent when the leap slows down. I used to use a smooth damp, but it had the same problem.
    Code (CSharp):
    1. if (cameraMove){
    2.             if (follow) {
    3.                 posX = Mathf.Lerp(transform.position.x,target.transform.position.x + callPosX, t);
    4.                 posY = Mathf.Lerp(transform.position.y,target.transform.position.y + callPosY, t);
    5.                 transform.position = new Vector3 (posX, posY, transform.position.z);      
    6.             } else if (!follow) {
    7.                 posX = Mathf.Lerp(transform.position.x,staticPosX, t);
    8.                 posY = Mathf.Lerp(transform.position.y,staticPosY, t);
    9.                 transform.position = new Vector3 (posX, posY, transform.position.z);
    10.             }
    11.         }
    I've tried messing around with rounding the offset to the pixels per unit. I've followed the math exactly how it is in other peoples' code, but it just doesn't seem to work for me, and seems to do the exact opposite.
    Here's the settings for what I have for my PixelPerfectCamera component. The background quads are all at the right size to match this.


    Do any of you know of a particular solution to this problem? If possible, I'd like to still be able to see quads for my background images instead of a really wide sprite like I've seen in the videos.
    If you need any other information, please let me know, thanks!
     
  2. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    832
    Hi @adscomics

    Let me help you out here. It's not the pixel perfect camera.

    In general, you'd be safer not using Lerp on large things like backgrounds or cameras in fixed pixel render projects, the smoothing of the values jump pixels out of step with everything else and it creates jittery flickery snapping.

    You're better off changing your Lerps above to scales of movement instead, that don't smooth out over time.

    To get things buttery smooth, move things by divisions of the frame rate.
     
    Last edited: Feb 2, 2021
  3. adscomics

    adscomics

    Joined:
    Nov 2, 2017
    Posts:
    13
    So actually, I managed to remove most of the shakiness by locking the background texture's offset to the pixel grid. The background is still a liiiiiiiiiittle shifty, but it's not nearly as bad as it was before.
     
  4. gomes_Zzz

    gomes_Zzz

    Joined:
    May 19, 2020
    Posts:
    1
    can u tell me how did you do that