- Home /
Sprite Rendering Order in layer based on 'parent' Gameobject y.position in world.
I am building a 2d running game with a fake perspective. Where moving on the y-axis determines your (fake)depth. What this means is that characters can overlap without having to 'touch'. Only if they collide head-on (in fake perspective) they interact. Now this comes with its own set of problems because my characters are gameobjects made from different sprites (limbs, head, etc). The main issue being orderinlayer sorting.
The best ideas i have seen so far have to do with changing the order in layer based on y-transformations, which i tried using and it got most of the job done, and would work perfectly had the character been 1 sprite. The problem is that when i run a script that changes the order in layer based on the y-axis position. it does so from the center of each individual sprite and starts at 0, ignoring my previously set order and messing up the character.
Here is my question:
Is it possible to keep the initial order in layer and increase/decrease it based on the (parent) gameobjects y position in the world? I say world because if the base is set to 0, my spawning enemies on different y-values would all be set to 0 and still render it useless.
Current code below:
float speed = 4.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
GetComponent<SpriteRenderer> ().sortingOrder = -(int)(transform.position.y * 100);
}
}