Search Unity

2d beat em up

Discussion in '2D' started by Nikolasi, Mar 12, 2017.

  1. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
  2. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    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.
     
  3. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
  4. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
  5. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    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):
    1. Vector3 newPoz = new Vector3(transform.localPosition.x, transform.localPosition.y, transform.localPosition.y);
    2.         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.
     
  6. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
  7. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
    Last edited: Mar 18, 2017
  8. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    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.
     
  9. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
    Last edited: Mar 18, 2017
  10. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    You could do something like:

    Code (CSharp):
    1.  
    2.  
    3. //Add this field...
    4. public float YOffSet=0f;
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         //Add this line to your  Update..
    10.         GetComponent<SpriteRenderer>().sortingOrder = -Mathf.RoundToInt((transform.position.y + YOffSet) / 0.07f);
    11.  
    12.         //Rest of the update...    
    13.         playerMoving = false;
    14.         //.....
    15.  
    16.  
    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..
     
  11. jackhearts

    jackhearts

    Joined:
    Apr 5, 2013
    Posts:
    103
    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.
     
  12. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
    Can you tell me did I insert your the code right?
    http://pastebin.com/YMi00NC8
     
  13. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    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
     
  14. Nikolasi

    Nikolasi

    Joined:
    Mar 6, 2017
    Posts:
    10
    Yes now it works and I understand how it works.
    Thanks
     
  15. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    No problem man! good luck with McKids!
     
  16. x01kirbyx

    x01kirbyx

    Joined:
    Mar 21, 2017
    Posts:
    7
    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?
     
  17. webox

    webox

    Joined:
    Aug 27, 2015
    Posts:
    72
    • 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..