Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Camera shake using cinemachine is changing the layers of overlapping objects. (2d)

Discussion in '2D' started by kevinyan010, Feb 21, 2023.

  1. kevinyan010


    Jan 8, 2020
    I posted this in unity answers and got no responses sooo

    An example of the layers changing is when the camera shakes, my gun goes infront of my player even though it was behind it before, and overlapping platforms change layer too, like the floor appeared infront of the wall before but during the shake sometimes it goes behind it. When the camera stops shaking, the layers are back to normal btw. Here's my code:

    Code (CSharp):
    1. using System.Threading;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6. public class CameraShake : MonoBehaviour
    7. {
    8.     public static CameraShake Instance { get; private set;}
    9.     private CinemachineVirtualCamera cam;
    10.     private float shakeTimer;
    11.     void Awake()
    12.     {
    13.         Instance = this;
    14.         cam = GetComponent<CinemachineVirtualCamera>();
    15.     }
    16.     public void Shake(float intensity, float time){
    17.         CinemachineBasicMultiChannelPerlin camPerlin = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    18.         camPerlin.m_AmplitudeGain = intensity;
    19.         shakeTimer = time;
    20.     }
    22.     private void Update()
    23.     {
    24.         if(shakeTimer>0){
    25.             shakeTimer -= Time.deltaTime;
    26.             if (shakeTimer <=0f){
    27.                 CinemachineBasicMultiChannelPerlin camPerlin = cam.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
    28.                 camPerlin.m_AmplitudeGain = 0f;
    29.             }
    30.         }
    31.     }
    32. }
    I didn't use the shake extension for cinemachine btw
  2. Kurt-Dekker


    Mar 16, 2013
    This is almost certainly a result of ambiguous layering (eg, two sprites on the same layer / group) than anything to do with the camera.

    Check the sorting order of your sprites. Here's some more reading:

    Three (3) primary ways that Unity draws / stacks / sorts / layers / overlays stuff:

    In short,

    1. The default 3D Renderers draw stuff according to Z depth - distance from camera.

    2. SpriteRenderers draw according to their Sorting Layer and Sorting Depth properties

    3. UI Canvas Renderers draw in linear transform sequence, like a stack of papers

    If you find that you need to mix and match items using these different ways of rendering, and have them appear in ways they are not initially designed for, you need to:

    - identify what you are using
    - search online for the combination of things you are doing and how to to achieve what you want.

    There may be more than one solution to try.

    Additional reading in the official docs:

    And SortingGroups can also be extremely helpful in certain circumstances:
    Gregoryl likes this.