Search Unity

2D Camera Flicker/Jitter

Discussion in '2D' started by Plopikoosy, Apr 4, 2015.

  1. Plopikoosy

    Plopikoosy

    Joined:
    Dec 20, 2014
    Posts:
    11
    Hi, I'm currently building a 2D platformer for Android. I have a camera script that makes it follow the character between user entered screen bounds. The problem is that when I go to play the game and the camera starts following the character, the screen starts flickering and looks really bad. I think its either the sprites that cause this or the player.

    I have read up a lot about this and people are saying to use FixedUpdate functions and LateUpdate, mess with Rigidbody Interpolate, change frame rate. None of these seem to work in the camera script or the player controller. I can't pinpoint the exact problem :/ Any help would be greatly appreciated :)

    Thanks
     
  2. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    How are you moving the camera? I know you mentioned this already but make sure any rigidbody2D calls are made in FixedUpdate() in your player script.

    not sure if this will help but this is what im using :

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class CameraController : MonoBehaviour
    3. {
    4.     public bool IsFollowing { get; set; }
    5.     public Transform ChaseTarget;
    6.     public Vector2 Margin;
    7.     public Vector2 Smoothing;
    8.  
    9.     private Vector3 shakeForce;
    10.     private float shakeStrength;
    11.     private float shakeDuration;
    12.  
    13.     private Camera cameraComponent;
    14.  
    15.     public void Start()
    16.     {
    17.         IsFollowing = true;
    18.         cameraComponent = GetComponent<Camera>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         // set the desired aspect ratio (the values in this example are
    24.         // hard-coded for 16:9, but you could make them into public
    25.         // variables instead so you can set them at design time)
    26.         float targetaspect = 16.0f / 9.0f;
    27.  
    28.         // determine the game window's current aspect ratio
    29.         float windowaspect = (float)Screen.width / (float)Screen.height;
    30.  
    31.         // current viewport height should be scaled by this amount
    32.         float scaleheight = windowaspect / targetaspect;
    33.  
    34.         // obtain camera component so we can modify its viewport
    35.         // if scaled height is less than current height, add letterbox
    36.         if (scaleheight < 1.0f)
    37.         {
    38.             Rect rect = cameraComponent.rect;
    39.  
    40.             rect.width = 1.0f;
    41.             rect.height = scaleheight;
    42.             rect.x = 0;
    43.             rect.y = (1.0f - scaleheight) / 2.0f;
    44.  
    45.             cameraComponent.rect = rect;
    46.         }
    47.         else // add pillarbox
    48.         {
    49.             float scalewidth = 1.0f / scaleheight;
    50.  
    51.             Rect rect = cameraComponent.rect;
    52.  
    53.             rect.width = scalewidth;
    54.             rect.height = 1.0f;
    55.             rect.x = (1.0f - scalewidth) / 2.0f;
    56.             rect.y = 0;
    57.  
    58.             cameraComponent.rect = rect;
    59.         }
    60.  
    61.         if (shakeDuration > 0)
    62.         {
    63.             shakeForce = new Vector3(Random.Range(-shakeStrength, shakeStrength), Random.Range(-shakeStrength, shakeStrength), 0);
    64.             transform.position = transform.position + shakeForce;
    65.  
    66.             shakeDuration -= Time.deltaTime;
    67.             if (shakeDuration < 0)
    68.             {
    69.                 shakeDuration = 0;
    70.             }
    71.         }
    72.     }
    73.     void FixedUpdate()
    74.     {
    75.         float x = transform.position.x;
    76.         float y = transform.position.y;
    77.  
    78.         if (IsFollowing && ChaseTarget != null)
    79.         {
    80.             if (Mathf.Abs(x - ChaseTarget.position.x) > Margin.x)
    81.             {
    82.                 x = Mathf.Lerp(x, ChaseTarget.position.x, Smoothing.x * Time.deltaTime);
    83.             }
    84.             if (Mathf.Abs(y - ChaseTarget.position.y) > Margin.y)
    85.             {
    86.                 y = Mathf.Lerp(y, ChaseTarget.position.y, Smoothing.y * Time.deltaTime);
    87.             }
    88.         }
    89.  
    90.         transform.position = new Vector3(x, y, transform.position.z);
    91.     }
    92.  
    93.     public void SetShake(float duration, float maxStrength)
    94.     {
    95.         shakeDuration = duration;
    96.         shakeStrength = maxStrength;
    97.     }
    98. }
     
  3. Plopikoosy

    Plopikoosy

    Joined:
    Dec 20, 2014
    Posts:
    11
    Thanks a lot for the reply, I had actually managed to fix it. It was something wrong with the textures tint from the mesh i was using from Tiled. It was a strange issue and I thought it was the camera flickering.

    Thanks.