Forum rules - please read before posting.

Switching SortingMaps depending on the player's position

Hi there!

I have a complex (2D) scene that requires me to change the active SortingMap depending on where in the scene the player is standing.
So far I tried two strategies (none of which have worked reliably):

Option 1
I tried using two separate Triggers placed close to each other, both set to update the active SortingMap once the player enters them.

This works unless the player moves in small increments in which case it sometimes ignores one of the triggers (possibly because they fire so close to one-another?).

Option 2
I also tried using one GameObject with two Trigger components, one set to change SortingMap OnEnter and one set to change SortingMap on OnExit.

This seems to be triggered more reliable, but if the player is placed right on the edge of the trigger’s area, the player get’s “stuck” in infinite switching between the two SortingMaps. This seems to be because the newly activated SortingMap caused the player sprite to “grow” which pushed him out of the trigger area, causing the OnExit SortingMap to be activated which in turn “shrinks” the player sprite back to the trigger area.

Am I going about this in the wrong way? Is there a more reliable way to change the active SortingMap based on player location rather than using triggers?

Thanks a bunch!

Comments

  • Could you share a screenshot showing the layout of the scene? This is a visual problem so it would be best to see the actual scenario you have.

    Normally I would recommend Option 2, but I suspect your hunch is right. A character's root will scale with the Sorting Map, even if it's the sprite child that has the Follow Sorting Map component.

    With Option 1, I'm not actually sure what would cause such a problem - but you may find improvement by disabling a Trigger once it's run, and enabling the other. This can be done with the Object: Send message Action.

    Otherwise, if you needed absolute precision, you could do it with a simple script, i.e.

    public float xThreshold;
    public SortingMap leftSortingMap;
    public SortingMap rightSortingMap;
    private bool onLeft;
    
    void Start ()
    {
        onLeft = (KickStarter.player.transform.position.x < xThreshold);
    }
    
    void Update ()
    {
        if (KickStarter.player.transform.position.x < xThreshold)
        {
            if (!onLeft)
            {
                KickStarter.player.followSortingMap.SetSortingMap (leftSortingMap);
                onLeft = true;
            }
        }
        else
        {
            if (onLeft)
            {
                KickStarter.player.followSortingMap.SetSortingMap (rightSortingMap);
                onLeft = false;
            }
        }
    }
    

    On another note, if you're using the Scene: Change setting Action to change the scene's default Sorting Map, this'll affect any NPCs in your scene as well. To change the Sorting Map that a specific character relies on, use the Character: Change rendering Action.

  • edited January 2019

    Thanks,
    Ah, yes I now use the "Character: Change rendering" instead - good point!

    I think I finally reached an OK solution using option 2:

    While running the game I located the "tipping point" of the trigger by carefully modifying the transform position of the character. I then carefully synced the character scaling size of the two SortingMaps so that the difference isn't large enough to cause the character's root to push him over the edge.

    While this seems to work it was very tedious and I fear that the magic spot might even differ depending on screen resolution and ratio. It would be awesome if there was a way to stop the character's root from scaling up when a character is scaled up (due to a new SortingMap being assigned) - maybe this should even be the default?

  • It would be awesome if there was a way to stop the character's root from scaling up when a character is scaled up

    The scaling up of the root is absolutely intentional - colliders etc placed on the root must also scale with the character. For absolute precision, use the code I provided.

  • I see. The code you provided doesn't quite work since the bounds of the trigger isn't that simple, it's actually needs to be a Polygon Collider.

    I don't mean to press to hard on this, I might be missing something. But if using triggers in this fashion is the recommended way to switch the active Sorting Map on a character, not having an option to specify whether the character's root should be affected by the character scaling means for potential dead spots that are extremely hard to miss and to reproduce. Leaving it to the players of the game to get stuck on them.

    Perhaps it would be worth considering an option for this on the SortingMap itself, or on the Trigger, or letting us specify this on the action that sets/resets the active SortingMap on a character, or on the actual character itself.

    JM2C :)

  • If you'd like to send me an example scene/character in a .unitypackage file that I can use to see the issue for myself, I'll gladly look.

  • I just sent you an inbox-message containing a full repro of the problem :)

  • edited January 2019

    Try this updated script, attaching it to a 2D Collider that replaces the Trigger:
    http://pasteall.org/1458734/csharp

  • Thanks, inspired by your code I came up with something simple that seems to work great (even with NPCs during player switching etc.):
    http://pasteall.org/1460042

  • edited January 2019

    My previous post doesn't solve the dead spot issue though - here's a stab at it:
    http://pasteall.org/1460323/csharp

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.