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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating the illusion of depth in 2D space

Discussion in '2D' started by McBugman, Jun 1, 2018.

  1. McBugman

    McBugman

    Joined:
    Nov 18, 2016
    Posts:
    4
    I'll start out with I know Unity has a great 3D engine and I could achieve this in that realm, I'm playing around with that as well. The truth is I like the simplicity of the 2D engine for this application and would prefer to make this work here, if possible and not too much of a headache.

    I'm looking to fix the perspective issue shown below. Basically the greater the Y-axis value of an object, the higher in the rendering order of the graphic so things in front will block those behind them and there'll be an illusion of one object passing behind the other. Does anyone know of any Unity trick for this?
    upload_2018-6-1_12-41-28.png
     
  2. 1Piotrek1

    1Piotrek1

    Joined:
    Mar 14, 2014
    Posts:
    130
    Hey, it's pretty simple.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class YSort : MonoBehaviour {
    4.  
    5.     Renderer rend;
    6.  
    7.     void Start () {
    8.         rend = GetComponent<Renderer>();
    9.     }
    10.  
    11.     void Update () {
    12.         rend.sortingOrder = -(int)transform.position.y;
    13.     }
    14. }
     
    McBugman likes this.
  3. McBugman

    McBugman

    Joined:
    Nov 18, 2016
    Posts:
    4
    Thank you thank you! I figured it would be.