Forum rules - please read before posting.

Multiple characters and FollowSortingMap

The SortingMap system is quite nice, and I really like how easy it is to tweak order and fake perspective with it in a 2D scene.

However, when multiple NPC's are walking around they don't order properly when overlapping each other.

Is there a way to use SortingMaps and if 2 characters are in the same area, the top one on the Y-axis will always be sorted behind the lower one?

Both characters are 2D and have sprites with child sprites that are offsetted a little bit.

Before I was using AC, I did it something like this:
order = (int)-(Mathf.Round (transform.position.y * 100));
sprite.sortingOrder = order + offset;

My guess is the easiest way is to create a custom version of FollowSortingMap?
I am a lousy coder, so any advice or help would be appreciated! :)

Comments

  • There is already a system in place to account for two characters in the same region - and your own trick is very similar to the way it actually works, i.e. moving the sprites ever so slightly towards the camera.

    However, testing this just now suggests there's a problem with it.  Though the position is changing correctly, it's not affecting the rendering order as it should - it could be that this is due to a change in Unity.  What version are you using?
  • The latest: 5.4.1f1

    I implemented my own code and it seems to work without braking the scale from AC's sortingmap. But the code is a bit messy and it involves adding components to every single sprite in the game. So I'd rather just use AC's system if it'll work :)

    (Here's WIP of my project:http://lukaskrepel.nl/game/ I'm mostly trying some styles/design/learning Unity. I plan to start from scratch once I've got my story written. I would never have gotten this far without AC)
  • Nice work.  Actually I misread your previous comment - the way AC handles it is to literally move the sprite's position along the Z-axis.  I suspect a change within Unity it responsbile for this, so I'll look into a workaround to compensate.
  • Looking into this further, it seems this may not be what I first thought.  I suspect this may in fact be an issue with the Edit-mode preview? option in the FollowSortingMap component.  Try unchecking that option in all your characters, and manually reverting their SpriteRenderer Order In Layer values back to their default (presumably 0).

    Do they then play nicely together?  When two characters are within the same Sorting Map region, you should find that the Z-position of one of them is offset by a very small amount (around 0.001) - is this happening, even if the sprite ordering itself is still wrong?
  • Hmmm... I see the Z-pos change happening, but one of the characters is definately not around 0.001 though, it's 5.611451e-06 actually. The other one is around 0.0001

    When I pause the game, the order suddenly looks right in the preview...

    I also tried a WebGL build to see if it might look right there (as with the hotspot-highlight issue), but it doesn't.

    I can send you the entire project by WeTransfer if it helps.
    • What was the result of my previous suggestion regarding the disabling of Edit-mode preview?
    • What is the Projection of the active camera?
    • When paused, is it correct in the Scene window, or the Camera Preview overlay box in the corner, or both?  Try previewing both the MainCamera and the active GameCamera.
    • Are both sprites of the same sorting order?  If so and you pause the game, try changing the z-order of the sprite at 0.0001 manually and let me know at what value it then looks correct.
    An entire project will be large and cumbersome to receive - if you can create a .unitypackage that contains just the scene, character graphics and relevant prefabs, then I can take a look for you.
  • I feel like I've tried everything and my head is spinning. At some points it even looks like it purposely shifts 1 character on the z-axis, but the wrong one. (So the one that's high on the map is getting pushed in front on the screen).

    I sent a trimmed down file trough WeTransfer. Hope it helps. I'm curious if it's broken or if I'm simply doing it wrong.
  • OK.  The Z-axis shifting is AC's way of correcting the order of two sprites on the same Sorting Map region, so I'm not understanding why things are looking wrong.  What's most likely is that it's down to a change or setting within Unity itself.
  • Ok, thanks for looking into it.
    I'll use my own script for now. It works quite well without breaking the scaling effect of the Sortingmap.

    (A side note: I've not used AC for a long time, but am impressed by your support on the already fantastic product. Keep it up!)
  • It is because you are using a 3D camera for your 2D game.  Choose "GameCamera2D" from the list of prefabs in your Scene Manager and use that instead of the one you currently have.  It should have the GameCamera2D component on it, instead of the GameCamera one.  This may have happened if you did not have your managers correctly assigned when building the scene.

    You will also need to set your character sprite's positions to (0,0,0), so that they are at same position as their root objects.
  • Aaaah, thanks! I completely missed the GameCamera2D.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.