- Home /
Sorting Order in 2d top down/isometric game view
Hi! I was working on the movement of my character in a 2d Zelda like top down game and I have some difficulties to maje my character go around an object, like a tree for example. First, I created a single tree and used the y position of the box colider of this tree to dertemine wether my character should pass in front of the object or behind, I would then change the layer order of the tree to be higher or smaller then the layer order of the player . Everything worked well with a single tree so I instanciated it so I could create teste with more trees. The original one work juste fine, but the other two I created from the prefab are not reacting to the script.... The order in layer is suposed to be 1 (the character is lower on the y axe) or 3 (the caracter is higher on the y axe). I imagine I am doing something wrong, anyone would know where I messed up? haha Here is the script I made to chager the Layer Order, i is supposed to be attached to the tree.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class objectLayering : MonoBehaviour {
[SerializeField]
private BoxCollider2D playerBox;
private BoxCollider2D objectBox;
private SpriteRenderer objetcRenderer;
// Use this for initialization
void Start () {
objectBox = GetComponent<BoxCollider2D>();
objetcRenderer = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void FixedUpdate () {
if (playerBox.bounds.center.y < objectBox.bounds.center.y)
objetcRenderer.sortingOrder = 1;
else
objetcRenderer.sortingOrder = 3;
}
}
Thanks
EDIT: Okay, I maked it work but it is kind of a headache, what was happening was that my prefab would get the BoxColider component of my prefab player instead of my actual player one. So I manually put my player's box colider inside each instance of my trees. I imagine there is a better way to do so hahaha, I need help t.t
Your answer
Follow this Question
Related Questions
Layering multiple 2d images on one texture 3 Answers
Can I hide/show objects by layer or tags? 3 Answers
Cannot rename new layer 2 Answers
Multi camera lighting for object with layer no selected 0 Answers
Sorting Layer alternative for 4.2.2 0 Answers