I’m working on a Top-Down 2D game, and I’m trying to get a WorldSpace-Canvas that’s a part of my “Enemy”-object to adhere to the same Sorting Order as the rest of the object.
However, the Canvas does not seem to respect the sorting order of the Sorting Group that’s a parent of the Canvas. This is causing my Canvas (a HealthBar) to be positioned behind other sprites when they are overlapping:
Is there any way to have this Canvas adhere to the same Sorting Order as the sprites for the GameObject?
Hierarchy:
Enemy (top-level GameObject)
- Graphics (Has SortingGroup-Component)
- EnemySprite (SpriteRenderer for Enemy)
- HealthCanvas (WorldSpace-Canvas)
- Background (Healthbar-bg)
- HealthValue (HealthBar-FillImage)
I have tried setting both the Position & Pivot for the Canvas to the ‘bottom’ of the Object (in the same position as the SortingGroup-Object), but this did not make any difference. It appears that the Canvas-Objects are directly using their WorldSpace-Position for their Sorting Order?
See image below: The HealthBar-Image does not get positioned in front of the ‘Player-Sprite’ until its Y-position is below it. (This is the top-most Y-position at which the HealthBar-Image gets positioned in front)
Expected/Wanted Result:
The WorldSpace-Canvas uses the same sorting order as the Sorting Group the Canvas is a child of, such that an Enemy’s HealthBar is always on the same sorting order layer as the SpriteRenderer(s) for the Enemy
Edit: Moved Hierarchy into a Code-Block so that indentation works.