Forum rules - please read before posting.

Sorting of diagonal objects?

Hi there,

I have this problem...

...which I can imagine must be a very common issue in many 2D games.

Now, in normal circumstances, when the Player walks past the left of the wall, I would tell it and the urinary objects to switch to a much higher layer, so the character always keeps behind it.

But I'm planning to add NPCs that will walk around there and use the urinaries both on the right and left. So I can't depend on the Player's position to tell the wall where to put itself. What could be fine for the player could be very wrong for an NPC on the other side.

So, I'm thinking of using two SortingMaps instead. One on the left, and another on the right, to which all characters would switch to according to their individual X positions.

Or is there an AC built-in way to do this in an easier way that I'm missing?

Thanks.

Comments

  • Sorting Maps can be used to affect either a character sprite's Order In Layer or Sorting Layer value based on their position in the scene - but not both.

    This means, however, that you can have AC control one field, with another controlled by custom script.

    The easiest way to calculate which side of a diagonal object another sprite is on is to draw a line between two opposite corner of the object and see which side of the line the sprite is on.

    What you could try is put objects on the left of the wall on Sorting Layer "A", objects on the right of it on Sorting Layer "C", and have the wall itself on Sorting Layer "B". The background would be behind everything so that the sorting hierarchy would be:

    • Back wall
    • Left side objects / characters
    • Diagonal wall
    • Right side objects / characters

    A script or Trigger can be used to update the character dynamically, but see if manually updating Inspector values gives the intended effect first.

  • Well, I forgot to describe my actual setup... it's terribly more complex than just four sorting layers, because of reasons.

    Indeed I'm using sorting maps for deciding which sorting layer will use the player or NPC. Sorting order is dealt by the Y axis.

    And everything was fine, until now that I need for individual NPCs treating different areas of the same scene in a different way regarding their own render sorting.

    So, today I've added big triggers to change the followed sorting map of the characters, like this:

    Then each trigger has this setup...:

    ...the action list asset looks like this:

    And this is working well:

    Now, the obvious problem is that this just works with the Player character, because I've hard-linked it to the action list asset Change Rendering action.

    So, as it is now, any NPC passing through will change the sorting map of the player, not its own.

    And this is where I get lost, because I've tried this...:


    Adding another gameobject parameter so I can select it for the character GO.

    But when I pick the parameter in the action, this happens:

    That is, the gameobject parameter is not recognized as a Char, so the action doesn't do anything, not even working with the Player anymore.

    This is the final piece of the puzzle for this to work with all my NPCs and the Player object.

    Otherwise I'd need to run the Change Rendering action from a Call Method action in PlayMaker or a script instead.


    And just as I was going to click the "Post comment" button, I've realized I have the Follow SortingMap component to manipulate outside AC action lists, lol.

    Anyway, I would appreciate if you could tell me how to effectively "unlock" the action so I can use the built-in AC triggers instead of my own implementation.

  • Err... okay, it's working now.

    I forgot to change the collider parameter in all the three triggers.

    Also, I disabled "Sync parameter values".

    It's funny how writing a freaking two pages long post makes you find the solution yourself, lol.

  • Oh well, I'm facing another problem now...:

    All refs are lost because all 16 NPC objects are actually on the scene (again for reasons).

    I guess I'll just make my own implementation then.

  • edited November 2021

    If you're going to rely on Triggers to mark the areas of your scene, you can probably do away with Sorting Maps altogether and simply use the Character: Change rendering Action to force-set the character's Sorting Order manually.

    You also don't have to rely on Box Colliders for your Trigger shapes - you can replace them with Polygon Collider 2D components to have them better represent the shape of the room.

    That is, the gameobject parameter is not recognized as a Char, so the action doesn't do anything, not even working with the Player anymore.

    Each character has its own Animation engine settings, which are necessary for the Action to fully display.

    However, the settings in the Action are merely hidden - once the Action is run with a valid character, it'll still make use of the settings.

    All refs are lost because all 16 NPC objects are actually on the scene (again for reasons).

    Did you mean outside the scene, or is it the Trigger that's a prefab?

    The Trigger's Detection method is set to Transform Position, which doesn't currently support the Constant ID system. I believe it should be possible to add this feature - I shall look into it.

    In the meantime, you can switch this setting back to Rigidbody Collision, which features the ability to react to objects by Tag or Component name. Checking for the component "AC._Char" should encompass both the Player and NPCs.

    Though this mode requires a Rigidbody, it need not necessarily be on the detected object - you can attach a kinematic Rigidbody2D to the Trigger itself rather than add one to each character.

  • Hi Chris,

    I ended up adding a PlayMaker FSM on each character object, looking for rigidbody collisions with some polygonal "area" colliders I distributed on the scene, and when they're entered I call the SetSortingMap method on the Follow Sorting Map component of each character.

    By what you explain here I think it's more or less the same logic. I'm using kinematic rigidbodies in the NPCs, while the Player is using a dynamic type.

    I didn't try using the component checking method, but it would have worked as well, I guess.

    Thank you for your support. If it wasn't for all the guidance, trial and error I don't think I'd finally realized I could just use the follow sorting map component.

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.