Sorting layer switching question.

Hi all,

I’m currently working on a 2D game, and I hope I can get some advice on how to go about with two ideas, and what would be the best way to implement them.

First:
I’m making a 2D sidescroller that’s set in a building. The building has multiple layers, where one is able to enter a door in the hallway, which will then switch the player object to the other layer.

Setup of the scene:
There are two layers in the scene for objects (the hallway/room):

  • Layer01
  • Layer02

And two layers for the player:

  • PlayerLayer01
  • PlayerLayer02

I’ve changed the Phsyics2DSettings, so PlayerLayer01 only interacts with Layer01 (and not with Layer02), and vice versa.

The scene has two cameras, attached to a null object which follows the player:
CameraLayer1:

  • Layer: Layer01
  • Culling Mask: Layer01/PlayerLayer01

CameraLayer2:

  • Layer: Layer02
  • Culling Mask: Layer02/PlayerLayer02

Left image is the hallway (Layer: Layer01).
Right image is the room which connects to the door (Layer: Layer02).
Both have 2D colliders.

On start of the game, Camera1 is enabled, and Camera2 is disabled.

The door in the hallway has a Box Collider 2D, with the following script attached:

using UnityEngine;
using System.Collections;

public class TriggerDoor1to2 : MonoBehaviour
{
    public Camera cameraLayer1;
    public Camera cameraLayer2;
    public GameObject player;

    void Start ()
    {
        player = GameObject.Find("Player");
        cameraLayer1 = GameObject.Find("CameraLayer1").GetComponent<Camera>();
        cameraLayer2 = GameObject.Find("CameraLayer2").GetComponent<Camera>();
    }

    void OnTriggerStay2D (Collider2D other)
    {
        Debug.Log ("You are inside the trigger");

        if (Input.GetKeyDown (KeyCode.Q) && (cameraLayer1.enabled == true || cameraLayer2.enabled == false))
            {
            cameraLayer1.enabled = false;
            cameraLayer2.enabled = true;
            player.layer = LayerMask.NameToLayer("PlayerLayer02");
            }

    }
}

When pressing the Use button (Q), Camera1 is disabled, and Camera2 is enabled: This causes the objects in Layer01 (including the colliders) to become ‘invisible’, and the objects in Layer02 to become ‘visible’.

I wonder if this is the most optimal way to do this changing.

The second question is about the use of an escalator in the scene:

I’m using 5 Sorting Layers here. In order from front to back:

  • Player
  • Building (floor/walls)
  • Escalator (the actual escalator object)
  • BehindEscalator (a layer I’m using to place the player in)
  • EscalatorBackground (the grey background as seen in the above image)

The idea that I have is that the player is able to use the escalator when standing in front of it (either upstairs, or downstairs), and then presses the ‘Use’-button to use the escalator.

I’ve not yet implemented this (as I’m a huge coding noob :slight_smile: ), but before doing so I thought it made sense to ask here if people have any suggestions.

What I’m thinking off at the moment to do would be:

  • Player stands in trigger, and uses the escalator

  • Disable the controls on the player

  • Disable the 2d colliders on the player

  • Animation:

  • Walk towards the escalator

  • Change SortingLayer of the player to BehindEscalator

  • Animation of player moving down the escalator (move along a path? What could be a good solution here?)

  • Once downstairs, change SortingLayer of the player back to Player

  • Walk away from escalator

  • Enable the 2d colliders on the player

  • Enable the controls on the player

I hope the above description made sense, and you survived the TL;DR ; If so, and you have any tips and/or suggestions, please let me know.

Btw, a good reference for what I’m after, might be to look at the old arcade game Elevator Action. The game doesn’t have different layers, but the escalator effect in there is what I’m after.

Thanks in advance for any help, or pointing me into the right direction.

Edit: Added Physics2DSettings.

Unfortunately don’t have resources to dissect the entire post and give detailed recommendations, but in general you seem to be going into the right direction.

I just wanted to say that if you somehow run into performance problems and have to squeeze everything out you can, instead of disable/enable all of your gameobjects/colliders, you can set up the layers side by side in the scene (others hidden from camera). When switching layer, you simply move the camera & player a long way sideways, which is very cheap. HOWEVER I wouldn’t use this trick until you have no other option, because it makes editing your levels much more painful.

1 Like