How to Sort Sprites of Different Objects on the Same Layer Ignoring Y-Axis in a Top-Down Game

I am currently developing a top-down shooter game and have run into a bit of a puzzle. While sorting by the Y-axis works perfectly for all objects, I want enemy bullets to appear above the player character, regardless of their Y position. However, since there are walls on the map, I cannot directly modify the bullet’s Sort Order.

an example, Here is what it looks like now

That’s what I wanted

Additionally, using a Sorting Group doesn’t seem to solve the problem either. Objects outside the Sorting Group cannot be sorted by Y-axis relative to objects inside the Sorting Group. Since the player character is outside the Sorting Group, I am unable to adjust its order in relation to the bullets within the group.
Thank you in advance for any help or suggestions!

Hmm have you tried using a sorting group for everything, including the player?

The only issue might be the walls if the player can be both approach it from the top as in your screenshot, or from the bottom where the player should be in front of the wall.

If possible, this could be solved by never having walls that are only a single tile high, and using different sorting groups for the “bottom” and “top” walls.

Nevertheless I’m curious myself because it’s a problem I’m going to steer myself into rather soon. Didn’t see anything obvious in the manual. And the only alternative I can think of is to use the Graphics API to draw your sprites in the order that best fits the game.

1 Like

Thank you for your reply!
I’ve tried putting all the objects that should be obscured by walls into the same Sorting Group (with the same Layer Order as the walls). However, when these objects have the same Layer Order, they are only sorted by their Object Layer.

If I add a separate Sorting Group for each individual object, the effect is similar to directly modifying the Sprite’s order.

Or do I need to use a large Sorting Group and then modify each object’s order via code?

Three suggestions:

A. if the bullet is very proximal to the player, raise its sorting order by script. Might be an issue if you have lots of walls in the middle of your levels. Evaluate for how often it pops errantly and adjust the proximity.

B. just accept the bullet being in a higher sorting order and destroy it early. Add bullet-specific outer colliders to your walls, target a bullet Layer and destroy the bullet early.

C. you could also just not care about this momentary sorting thing, I doubt it’ll be very noticeable once the game has polish

2 Likes

Thank you so much for your suggestions!
I went with option B and added bullet-specific outer colliders to the walls to destroy the bullets early.This method works great!