# 2d beat em up

**URL:** https://discussions.unity.com/t/2d-beat-em-up/658033
**Category:** 2D
**Tags:** 2D
**Created:** [March 12, 2017, 12:00pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033 "2017-03-12T12:00:47Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 12, 2017, 12:00pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/1 "2017-03-12T12:00:47Z")

</div>

Here we go, if someone can help me explain how to do it in front of and behind the object:

script:

> **[using UnityEngine;using System.Collections;public class PlayerController -...](https://pastebin.com/85CD5WBf)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

problem:

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [March 12, 2017, 6:32pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/2 "2017-03-12T18:32:24Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 13, 2017, 4:50pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/3 "2017-03-13T16:50:52Z")

</div>

I try use [http://www.coatbros.com/blog/2015/03/](http://www.coatbros.com/blog/2015/03/) but it dont work.I dont know why.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 14, 2017, 11:08am UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/4 "2017-03-14T11:08:41Z")

</div>

Come on [Twitch](https://www.twitch.tv/rasetocsgo) to help me.

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [March 14, 2017, 3:37pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/5 "2017-03-14T15:37:01Z")

</div>

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().

```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.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 17, 2017, 8:13pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/6 "2017-03-17T20:13:19Z")

</div>

My new code is:

> **[using UnityEngine;using System.Collections;public class PlayerController : -...](https://pastebin.com/qtwQqZtj)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

**Script isnt completed.**

Look:

[![](https://europe1.discourse-cdn.com/unity/original/4X/0/0/b/00b476185da1ef1cf3c700d7e478749b2a45b935.jpeg "Player go hide on z -1") ](https://www.youtube.com/watch?v=ojHhZi8G7rQ)

Code need debug.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 18, 2017, 8:20am UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/7 "2017-03-18T08:20:30Z")

</div>

If any one can fix it will be great:

[https://www.dropbox.com/sh/efekdepadgs8949/AACTz-JkBjKqP87Fr1ciM3Fya?dl=0](https://www.dropbox.com/sh/efekdepadgs8949/AACTz-JkBjKqP87Fr1ciM3Fya?dl=0)

open main scene in folder scenes

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [March 18, 2017, 12:51pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/8 "2017-03-18T12:51:39Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 18, 2017, 1:37pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/9 "2017-03-18T13:37:24Z")

</div>

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.  
 ![](https://europe1.discourse-cdn.com/unity/original/4X/8/2/7/827e5d49e9de86844fdc6bd53f8f0a3f19393427.png)

Can you get project from dropbox?

[http://www.coatbros.com/blog/2015/03/](http://www.coatbros.com/blog/2015/03/)  
[http://www.codingwithunity.com/2016/09/sprite-depth-for-top-down-2d.html](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?

---

<div class="post-metadata">

### Author: ![webox](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/webox/32/177099_2.png) [@webox](https://discussions.unity.com/u/webox)
#### Post date: [March 18, 2017, 7:30pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/10 "2017-03-18T19:30:32Z")

</div>

You could do something like:

```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)

![](https://europe1.discourse-cdn.com/unity/original/4X/c/2/8/c28ef87de2bcbaf7c061358a419d1c5a40f1f588.gif)

EDIT: Also! put all the elements but the BG in the same sorting layer… otherwise sorting order won’t do the trick…

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [March 18, 2017, 10:32pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/11 "2017-03-18T22:32:47Z")

</div>

> [@Nikolasi](#):
>
> 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.  
> ![](http://i.imgur.com/Sl8tMuD.png)

It will work. Read up on the [sprite renderer](https://docs.unity3d.com/Manual/class-SpriteRenderer.html). 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.

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 18, 2017, 10:48pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/12 "2017-03-18T22:48:19Z")

</div>

> [@webox](#):
>
> You could do something like:
> 
> ```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)
> 
> ![](http://i.imgur.com/Qgomlr1.gif)
> 
> EDIT: Also! put all the elements but the BG in the same sorting layer… otherwise sorting order won’t do the trick…

Can you tell me did I insert your the code right?  
[http://pastebin.com/YMi00NC8](http://pastebin.com/YMi00NC8)

---

<div class="post-metadata">

### Author: ![webox](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/webox/32/177099_2.png) [@webox](https://discussions.unity.com/u/webox)
#### Post date: [March 18, 2017, 10:49pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/13 "2017-03-18T22:49:49Z")

</div>

> [@Nikolasi](#):
>
> Can you tell me did I insert your the code right?  
> [http://pastebin.com/YMi00NC8](http://pastebin.com/YMi00NC8)

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

---

<div class="post-metadata">

### Author: ![Nikolasi](https://avatars.discourse-cdn.com/v4/letter/n/e5b9ba/32.png) [@Nikolasi](https://discussions.unity.com/u/Nikolasi)
#### Post date: [March 18, 2017, 11:04pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/14 "2017-03-18T23:04:06Z")

</div>

> [@webox](#):
>
> 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

Yes now it works and I understand how it works.  
Thanks

---

<div class="post-metadata">

### Author: ![webox](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/webox/32/177099_2.png) [@webox](https://discussions.unity.com/u/webox)
#### Post date: [March 18, 2017, 11:07pm UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/15 "2017-03-18T23:07:03Z")

</div>

> [@Nikolasi](#):
>
> Yes now it works and I understand how it works.  
> Thanks

No problem man! good luck with McKids!

---

<div class="post-metadata">

### Author: ![x01kirbyx](https://avatars.discourse-cdn.com/v4/letter/x/3ec8ea/32.png) [@x01kirbyx](https://discussions.unity.com/u/x01kirbyx)
#### Post date: [March 23, 2017, 12:36am UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/16 "2017-03-23T00:36:21Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![webox](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/webox/32/177099_2.png) [@webox](https://discussions.unity.com/u/webox)
#### Post date: [March 23, 2017, 2:24am UTC](https://discussions.unity.com/t/2d-beat-em-up/658033/17 "2017-03-23T02:24:55Z")

</div>

> [@x01kirbyx](#):
>
> 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…
