Search Unity

Question 2D Cinemachine Parallax Jitter

Discussion in 'Cinemachine' started by Vitiik, Nov 13, 2021.

  1. Vitiik

    Vitiik

    Joined:
    Sep 27, 2018
    Posts:
    2
    I am working on a 2D Train game and for a certain time, I can't get rid of this jitter. The jitter can be most seen in the clouds but sometimes it can be seen on trains too. I have tried Unity PixelPerfectCamera, PixelRounding, changing update methods on Cinemachine Brain, setting Rigidbody2D to interpolate or extrapolate and still can't figure it out.

    Video: https://drive.google.com/file/d/1-0eGR6CPGMre1KcsIrjkTU7KaUU2pABP/view

    Photos of settings:
    Camera:
    upload_2021-11-13_10-4-8.png
    CM VCAM1:
    upload_2021-11-13_10-5-42.png

    Player:
    upload_2021-11-13_10-4-36.png

    Parallax Code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Parallax : MonoBehaviour
    7. {
    8.     private float length, startpos;
    9.     GameObject cam;
    10.     public float parallaxEffect;
    11.  
    12.     Rigidbody2D rb;
    13.  
    14.     void Start()
    15.     {
    16.         startpos = transform.position.x;
    17.         length = GetComponent<SpriteRenderer>().bounds.size.x;
    18.         cam = GameObject.FindGameObjectWithTag("MainCamera");
    19.         rb = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.        float temp = (cam.transform.position.x * (1-parallaxEffect));
    25.        float dist = (cam.transform.position.x * parallaxEffect);
    26.  
    27.        rb.position = new Vector3(startpos + dist, transform.position.y,transform.position.z);
    28.  
    29.         if(temp > startpos + length) startpos += length;
    30.         else if(temp < startpos - length) startpos -= length;
    31.     }
    32. }
    33.  
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Why would the Rigidbody2D interpolate? You're explicitly setting its position instantly each FixedUpdate. No idea what body type this is set to but if you want it to move to an explicit position AND this is a Kinematic body-type then use Rigidbody2D.MovePosition, this also work with interpolation. If it's Dynamic that you should use forces or set its velocity explicitly.
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    And also you have a script ordering problem: you're positioning the parallax layers in FixedUpdate, using the camera position as input. However, at this time the camera position has not been updated yet by CinemachineBrain, so you're calculating the layer position based on a stale camera position from the previous frame.

    Instead, hook into CinemachineCore.CameraUpdatedEvent, and run your parallax code from there. It will get called immediately after the camera has been positioned.
     
  4. Vitiik

    Vitiik

    Joined:
    Sep 27, 2018
    Posts:
    2
    Gregoryl I love you <3 It works. And thanks also to MelvMay for regarding the Rigidbody2D trash :)
     
    Gregoryl likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    Harsh, but fair. ;)
     
    Gregoryl likes this.