Search Unity

Need help with 2D Depth

Discussion in '2D' started by El3aab, Aug 25, 2015.

  1. El3aab

    El3aab

    Joined:
    Aug 25, 2015
    Posts:
    2
    A friend and I are trying to make a 2D game with depth.





    Essentially we'd like it so that if the Player were to walk behind the wall, the wall will render on top of the Player and if the Player were to walk in front of the wall, the wall would render behind the Player.

    Here's a GIF to show how it is now:



    Thanks guys!
     
  2. topaz7

    topaz7

    Joined:
    Dec 15, 2012
    Posts:
    76
    I am working on this project with him and basically what I want to know is how to have a dynamic draw order between walls.
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could try changing the sprite renderer sorting order by script
     
  4. RowanLea

    RowanLea

    Joined:
    May 30, 2014
    Posts:
    2
    might be a pain to do but you could always have the wall as two separate images? one for the top and one for the bottom.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    see isometric sorting:
    Code (CSharp):
    1. https://youtube.com/watch?v=rMCLWt1DuqI&feature=youtu.be&t=24m37s
    (example script download in description)
    It just adjusts sprite sorting order based on Y coordinate..

    or ready asset with more features:
    http://forum.unity3d.com/threads/released-easy-isometric-sorting.322157/

    *had to post youtube link as code, otherwise it embeds it..
     
    El3aab likes this.
  6. topaz7

    topaz7

    Joined:
    Dec 15, 2012
    Posts:
    76
    I have made this. It sometimes works but sometimes, the z order is messed up if I move to a different area. Thanks for the help.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Depth : MonoBehaviour
    5. {
    6.     private SpriteRenderer spriteRenderer;
    7.    
    8.     private GameObject shadow;
    9.     private SpriteRenderer shadowSpriteRenderer;
    10.  
    11.     public void Start()
    12.     {
    13.         spriteRenderer = GetComponent<SpriteRenderer>();
    14.         shadow = GameObject.Find("Shadow");
    15.         shadowSpriteRenderer = shadow.GetComponent<SpriteRenderer>();
    16.     }
    17.  
    18.     public void Update()
    19.     {
    20.         spriteRenderer.sortingOrder = (int)transform.position.y * -1;
    21.      
    22.         shadowSpriteRenderer.sortingOrder = (int)transform.position.y * -1;
    23.     }
    24. }
    25.  
     
  7. topaz7

    topaz7

    Joined:
    Dec 15, 2012
    Posts:
    76
    I forgot to mention that I use Tiled for Unity.
     
  8. El3aab

    El3aab

    Joined:
    Aug 25, 2015
    Posts:
    2
    Thank you for this video, his method seems to work. The only issue is I'd have to make the Level through Unity itself rather than Tiled but that's ok with me as long as the method works haha