Wouldnt your last two line of code just wreck havoc on the actual zorder between player and the tree what if for example you got two tree only spaced out on the Y axis by say 40 unit apart just enough room to squeeze between them and by space out I mean at teh tree trunk not the actual size of sprite.
Let call bottom tree A and top Tree B, There is three possible Z axis ordering i need to happen is
One if player goes below treeA the player just need to be Infront of tree A and Tree B
Two if player goes between Tree A and Tree B The player need to be behind Tree A but infront of Tree B
Three if player goes above Tree B it need to be behind them both
this is just the basic what if there is three I can get possiblility 1 and 3 to work but on number 2 possibility ........It either Z order incorrectly such as the player goes through tree A but is infront of tree B
Ive tried pick instance Tree trunk helper sprite nearest to player to do the z order .....it just bugs out when there too many tree nearby. which is why I really need to space them out when spawning the trees.