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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    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

    kevinyan010

    Joined:
    Jan 8, 2020
    Posts:
    5
    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.     }
    21.  
    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. }
    33.  
    I didn't use the shake extension for cinemachine btw
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    33,642
    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:

    https://forum.unity.com/threads/orthographic-camera-rendering-order.1114078/#post-7167037

    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:

    https://docs.unity3d.com/Manual/2DSorting.html

    And SortingGroups can also be extremely helpful in certain circumstances:

    https://docs.unity3d.com/Manual/class-SortingGroup.html
     
    Gregoryl likes this.