I have an NPC (parent) carrying sprites (children), they are all at sorting layer 6 and all using the default sorting layer. This works well, allowing my player to walk behind it. But when the NPC is walking backwards and forwards (up and down the screen) and effectively changing its sorting layer, the child sprites flicker like they are changing sorting layer at different times.
My NPC has the NPC script.
Child of NPC has animator, sprite renderer, box collider and follow sorting map script.
Follow Sorting Map
Follow default - TICKED
Offset original order - UNTICKED
Also affect children - TICKED
Edit-mode preview - TICKED
The NPC is an 8 frame animation consisting of 21kb frames, the child sprites are less than a kb each (pixelart game)
I am using 10 pixels per unit to bring my sprites more in-line with Unity units, this solves pathfinding etc. Doubt this has any impact, just wanted to give as much detail as possible to help troubleshoot.
Comments
Have you tried pausing the game to confirm what you suspect, i.e. that each sprite is moved to a new layer on different frames? When Unity's Editor is paused, you can advance frame-by-frame - if you haven't already, use this to confirm that this is indeed the cause of the flickering effect.
The component ought to be affecting each child renderer at exactly the same time, so I can't see how there'd be a delay between them - especially since the data is so small as you've said.
From your description, I'm assuming the SortingMap affects the Sorting Layer. As a test, try instead having it affect the Order In Layer, and using the Offset original order? checkbox to have the child sprites displaying in the correct order. You'll need to have each area of your Sorting Map set on orders at intervals large enough (e.g. 0, 10, 20) for each child sprite to fit within it. But again, this'll just be a test.
Currently using...
Sorting Map
Affect sprite's: Order In Layer
Affect Character scale?: TICKED
Affect Character speed?: TICKED
I have paused and gone frame by frame, and the Order in Layer stays constant on the parent and the children.
I tried with and without 'Offset original Order?:' ticked. It made no difference to this issue.
If you're changing the order instead, then you should definitiely have 'Offset original order?' checked - but you'll also need to give each sub-sprite a different sorting order (e.g. -1) to the base sprite. When the "official" order changes to e.g. 10, these sub-sprites will take their original order into account (so -1 would then be 9).
If all sprites are on exactly the same order/layer, then flickering will occur naturally because their's no definite order that they should be displayed in.
It also makes use of the "Offset original order" feature so that the shadow sub-sprite is always displayed underneat the main player sprite. It may be a more simple example than your own case, but it's the same principle.
I don't understand how the demo sub sprite is set to 5. I can disable Also affect children, change it, and when I tick also affect children it then changes the sub sprite (shadow) to 5. What is telling the shadow to change to 5? and how can I apply similar to my sub sprites?
As both the Offset original Order? and Also affect children? options are checked, these initial values will be added onto what the Sorting Map dictates. For example, when the Sorting Map wants to change them to 6, the main sprite is set to 6+0=6, while the shadow is set to 6+(-1)=5.
This means that both sprites will be changed as they move around the scene, but they will always have the same relative order between them - so the shadow will always be displayed under the main sprite.