Detecting OnPointerEnter/OnPointerExit on stacked sprites

Hi,

I am facing a problem in our game. We have rooms and furniture and character in the rooms, and our main mouse interaction is with the characters and the furniture, but we would like to be aware also when the player enters or exits a room with the mouse cursor.

Using the traditional onpointerenter/exit approach though, whenever the mouse, while still inside the room, hovers one of the characters or furniture, it detects an onpointerexit regarding the room collider instead.

They are on separate layers, but a (a bit blindly done just to see if it worked) attempt with 2 Physics2DRaycasters on the Camera, each set to mask different layers, had no results. Separately the two raycasters work as intended, the moment they are both activated, the OnPointerEnter and OnPointerExit scripts on the characters, furniture and rooms, have again the same problem.

Using a manual raycast 2d in code to detect the enter and exit in the room, feels a bit overkill as i should get at every call GetComponent of the room script, cache it, compare it, etc.

Anybody has any other idea. I’d love to stick to the use of OnPointerEnter and Exit if that were possible.

Thanks for your time and cheers
Dr.H

This is a very small price to pay.

Raycast, get the objects it hits in a particular layer or layers (Character, Interactable, Room), order those objects by the proximity to bottom of screen (or a more complex sort)… exclude the Room where it doesn’t make sense. Cache the clickable Character and Interactables as required.

Present interactivity UI to player as appropriate when the most-foregrounded interactable changes.

Display “highlighted” effect as appropriate for the object.

Raise OnMouseRoomChanged event when room changes, etc.

Mhm the fact that keeps me dubious is that in order to detect with a decent reactivity the mouse over the room and mouse out I would need to raycast at every frame or nearly at every frame.

A raycast per frame would maybe be overall acceptable, but having to retrieve the component of crossed objects at every frame is to my experience quite heavy, no? (our game is a tycoon simulation, and despite a lot of work for optimization, it has A LOT going on in the background already)

Isn’t there any alternative solution, anything that uses the traditional OnPointerEnter and OnPointerExit that might cover this case?

Maybe make a trigger physics object you reposition kinematically with mouse position? It has a particular layer and you setup the matrix to receive collisions with just the rooms? There may be a setting for enabling trigger/trigger collisions that needs to be turned on if your rooms are trigger-based. That’s not a performance neutral change and I can’t say how it would affect your project, but unless you’re targeting low-end mobile then just do a quick perf comparison on a lower-end target device.

GetComponent isn’t that bad, how many objects could be stacked, even?

IEnumerator RaycastOccasionally()
{
   while (true)
  {
      DoCheck();
      yield return null;
      yield return null;
  } 
}

Why dont you create a box collider over the room, and then reference it’s bounds? you could then just check if the pointer is within those bounds. (I know you said you wanted to keep using OnPointerEnter and exit)