Forum rules - please read before posting.

Sorting Order Highlight

Hi!
I have a 2D scene where I use the y-axis to sort the sprites in the same layer order. (general settings in URP) So the sorting is fine except that the AC hotspot highlight ignores this sorting method. It seems the AC hotspot highlight works with physics raycast so I would have to adjust the z value. Isn't there an easier way to do that?

Comments

  • Welcome to the community, @poink.

    Are you relying on the mouse to detect Hotspots? Indeed, raycasting is necessary here - it's not possible to detect them through visibility, but must instead rely on colliders and raycasts.

    Is this a case of having multiple Hotspots overlapping? If the scene layout is fixed, you can typically avoid this by relying on a Polygon Collider 2D component to describe a Hotspot's interactive shape instead of the default Box Collider 2D. This lets you use the Hotspot's exact shape - see the Tree Hotspot in the 2D Demo's Park scene as an example.

    If you can share screenshots that illustrate the scenario, I can try to offer more specific advice.

  • Thanks for the quick reply! I don't understand how to embed a png here. So here is the link to the two scenarios I mean (in this scenario I show how it should be):

    https://imgur.com/a/NRhu6EA

    I have added a hotspot component for the player and when I walk around, sometimes I am in front of the NPC and sometimes behind. Or sometimes the NPC moves, or there is a big object behind a smaller one.... In a normal 2D environment z is always 0, so sorting for raycast doesn't work. Is there a solution without changing the z value? (like sorting by y, which I do for visual sorting).

  • Thanks for the details.

    Currently, z-sorting is necessary for 2D characters that can move around the scene. This can be automated by the Sorting Map component, but not if you're opting to rely on Unity's Transparency Sort Axis instead.

    You raise an important point, however, and I will look to see if it is possible to add an option to account for object Y-positions when 2D Hotspots overlap like this.

    In the meantime, you should be able to make do with a temporary script - attached to each character's sprite child - that syncs the Z-position with Y by a small amount:

    using UnityEngine;
    
    public class MapYToZ : MonoBehaviour
    {
    
        const float scalar = -0.01f;
    
        void LateUpdate ()
        {
            Vector3 position = transform.position;
            position.z = position.y * scalar;
            transform.position = position;
        }
    
    }
    

    This shouldn't affect the sorting if you're relying purely on the Y-axis - but hopefully this should only be temporary while I investigate an official option.

  • Thanks. I had a solution in mind with raycastAll instead of raycast and then sorting of y. but maybe you have a better idea.

  • It'll be along those lines. I can confirm that this will be available as a new option in the upcoming v1.77.0 release.

  • Awesome, thanks!

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.