Hello,
I’d like to know which sorting layer or which sprite is visible at a certain position, is this possible?
My sprite does’nt have specific depth and colliders (so raycast2D does not works)
Hello,
I’d like to know which sorting layer or which sprite is visible at a certain position, is this possible?
My sprite does’nt have specific depth and colliders (so raycast2D does not works)
There isn’t a built-in or dynamic way of knowing this, no. Why do you need this information? I feel like there might be a way to achieve what you’re interested in accomplishing in another way. But if you really wanted to create this feature, one possible solution I roughly outline below. Disclaimer: don’t implement this. It would be non-performant and cursed.
You could have a collection of relevant SpriteRenderers for the area you are interested in. By enumerating through this collection you could compare their Sorting Layers, Order in Layer, and Z positions to determine which are visible.
If you need to find this information but can’t pre-build a collection of SpriteRenderers (i.e. you need this to be dynamic based on, say, the player’s position at a given time) and you didn’t want to use Colliders/Raycasts, you could write a small class deriving from MonoBehaviour with the attribute [RequireComponent(typeof(SpriteRenderer))]. You would attach this to every GameObject with a SpriteRenderer that you are interested in in your entire game. These classes could pass their SpriteRenderers to a different static class in their Awake methods. This static class could keep a collection of all these SpriteRenderers. Then you could query this class, passing it the position and sprite you’re interested in. It could enumerate through its SpriteRenderer collection and compare each SpriteRenderer.transform.position value to the passed position, determining via vector distance math which are within the area you’re interested in. If it finds the SpriteRenderer to be in a relevant position, it could then check the Sorting Layers, etc. You could create a new collection as you enumerate of only the SpriteRenderers that are obscuring your sprite in question, or simply increment a counter to determine if there are any.
2D Physics has no idea about render sorting. It’s also 2D so gives you XY position information so it wouldn’t give you Z information if it new.