How to Y-sort player with 3/4 view TileMapLayer?

I’m creating a game with a 3/4 view top-down 3x3 tilemap. I want the player sprite to appear in front of background walls while going partly behind foreground walls.

Here is my tilemap:

tileset01_angled_walls

I used Godot Autotile Texture Templater to create the template to allow for TileMapLayer terrain creation, created the art, added to Godot and created my terrain using which is using corners and sides matching:

So far, so good. Terrain editing works exactly as it should.

The problem is that I want my player to be able to go behind foreground walls while staying in fron of background walls. This isn’t a problem on tiles with only a foreground wall or only a background wall, but is a problem in tiles with both, like this:

problem_tile

There is no fixed y-sort value for the tile that will satisfy both.

I could break up my 32x32 tiles in to something smaller (they are formed from 8x8 sub-tiles), however I think I’d lose the ability to use the terrain tools if i did this. Is there any way to create 32x32 tiles, but with more granular y-sorting?

Another idea I had: dynamically adjust the y-sort offset of my player sprite depending on what part of the TileMapLayer tile it’s overlapping (the player sprite is not tall, so will never have to be simultaneously behind and in front of different walls within one tile, though that may not be the case for other games I make).

Any suggestions?

So I came up with a solution in my case. I set the floors to z_index=0, walls to z_index=2, and set the player to z_index 1 or 3 based on the player’s coordinates within the local tilemap cell:

@export var tilemap_walls: TileMapLayer
@onready var tile_size: int = tilemap_walls.get_tile_set().get_tile_size().y
...
func _process(delta: float) -> void:
	if int(position.y) % tile_size < 23:
		z_index = 3
	else:
		z_index = 1

This works in my case, but would appreciate feedback on the more general problem where a sprite is tall enough to overlapboth foreground and background walls in a tile simultaneously.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.