OnMouseEnter and OnMouseExit in new InputSystem

I did a quick google search and it seems there is a way to implement OnMouseEnter and OnMouseExit using the new InputSystem.

All you need to do is to have a PhysicsRaycaster on your camera and to implement IPointerEnterHandler and IPointerExitHandler interfaces in the HealthDisplay class.

The code than looks very similar, except it does not explicitly mention mouse anywhere:

public class HealthDisplay : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField] private Health _health;
    [SerializeField] private GameObject _healthBarParent;
    [SerializeField] private Image _healthBarImage;

    (...)

    public void OnPointerEnter(PointerEventData eventData)
    {
        _healthBarParent.SetActive(true);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        _healthBarParent.SetActive(false);
    }
}
7 Likes

Works like a charm, thank you :wink:

Yes great… Much cleaner.

Privacy & Terms