Here we go, if someone can help me explain how to do it in front of and behind the object: script: http://pastebin.com/85CD5WBf problem: http://i.imgur.com/AJP8JCF.gifv
Depending on how your sprites are set up you could have a script adjust the z position based on the y position. A z position closer to the camera will renderer in front and vice versa. An easy way to organise it is probably to have an empty game object containing any objects/characters the player will interact with in this way. You could then either have a z position manager attached to the parent empty object or a script for each one.
Assuming y positions are around zero, the simplest way would be to set the relevant objects z position to equal the y in LateUpdate(). Characters moving up would then also move further away from the camera. Objects that don't ever move could either have there z position set manually in the inspector or have a script run through them all when initialising to set z = y. A script would be most reliable. Try creating an empty game object called environment (or whatever you want), remembering to reset the transform. Move all the game objects that the player can move around in to this environment object (probably everything except the floor and walls). Then attach a new script to those objects with the following as the last entry in LateUpdate(). Code (CSharp): Vector3 newPoz = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.y); transform.localPosition = newPoz; Last thing to do would be to adjust the parent 'environment' objects z position so well clear of the floor and walls. You'll want to refine this to suit however you're moving the character but should at least get it working. In theory any x or y movements that happen in fixedupdate or update 'should' ™ propagate to lateupdate and not cause any ill effect.
If any one can fix it will be great: https://www.dropbox.com/sh/efekdepadgs8949/AACTz-JkBjKqP87Fr1ciM3Fya?dl=0 open main scene in folder scenes
If they're in different sorting layers you're not going to easily effect rendering order between them. Changing Z position wouldn't work in that instance because the layer orders take precedence. Layers should be for grouping similar objects and then you would use the 'order in' value to change what gets rendered first. Put them both in the default sorting layer and then try what I previously posted.
I try put all default and it dont work and also I try set oreder in layer 1,2,3 and player still cant go around. Can you get project from dropbox? http://www.coatbros.com/blog/2015/03/ http://www.codingwithunity.com/2016/09/sprite-depth-for-top-down-2d.html Maybe one of the those two script can help me in this situation?
You could do something like: Code (CSharp): //Add this field... public float YOffSet=0f; // Update is called once per frame void Update() { //Add this line to your Update.. GetComponent<SpriteRenderer>().sortingOrder = -Mathf.RoundToInt((transform.position.y + YOffSet) / 0.07f); //Rest of the update... playerMoving = false; //..... Then add colliders to the lower parts of Grandma and the table.. I tested it with -5f as offset and it works ok but needs some twitches.. (Sorry for the shaking, did it with my cell) EDIT: Also! put all the elements but the BG in the same sorting layer.. otherwise sorting order won't do the trick..
It will work. Read up on the sprite renderer. The sorting layers are for creating separate groups of related sprites. The order in layer is used to determine the order in which each of the sprites in that layer are rendered. You don't want to create a layer for single objects because, well, it's fairly pointless. Everything the player can move around like the table, mother, or whatever else is planned should be in the same layer. Only then will you be able to effect the rendering order by setting an order in value. Layers, ordering and z depth can be used together. Thing to remember is sorting order takes priority over z position and layers take priority over both. That's why those sprites need to be in the same layer.
Yes, remember to put player, grandma and table in the player SortingLayer (or any other you want, they just need to be in the same for "SortingOrder" to work) EDIT: And set a value for the Yoffset (-5) works fine
I tried the code to make it work in my project, it works but my Y Offset is "42" is this method still useful for doing this with multiple objects? especially in an open-world. is it possible to have a box collider with a trigger and setting a script so that on the player entering the area of the trigger it changes the sorting layer or order temperarily while the player is in the area? as with the y offset code isn't it going to put me behind anything on that Y axis?
Is this method still useful for doing this with multiple objects? especially in an open-world. The "method" is fairly simple, it only calculates a sorting order acordding to your Y possition plus some offset to addapt the player's "pivot". I quote this because it's not acctualy a pivot, but the place in the player's body where the sorting order changes (not sure how to explain this better). I'm not sure about the open world scenario's performance.. If several objects are adjusting it's sorting order constantly it may harm the performance, but I'm not sure of this.. is it possible to have a box collider with a trigger and setting a script so that on the player entering the area of the trigger it changes the sorting layer or order temperarily while the player is in the area? You can access the sorting order with SpriteRenderer.sortingOrder and you can edit that field programmatically, so I believe your approach is pretty much doable. You'll need to get the other object's SortingOrder and add 1 (or more). Remember that sorting order works only when you're working with objects inside the same SortingLayer (which can also be modified via script). I'm pretty sure your approach has to work.. It may require a lot of work adjusting colliders (the triggers) as with the y offset code isn't it going to put me behind anything on that Y axis? As i tried to explain before, Imagine the world as a set of parallel horizontal lines where each line has an assigned SortingOrder, your character ocuppies several lines in this imaginary field so the Y offset is just to center the part of the sprite that is centered with the imaginary line.. I can make an image explaining better if you want..