OnMouse events (e.g. OnMouseEnter) not working with new Input System

Hi,

I just figured out that OnMouse events (OnMouseEnter, etc.) are not working after adding the new input system. I wondered if this is a bug or a feature. If this is in case intended, could someone explain to me, why this is happening?

I tested it in several projects with this simple setup. Empty scene with only a Cube (box collider attached of course :)) and this simple script:

using UnityEngine;

public class Test : MonoBehaviour
{
    private void OnMouseEnter()
    {
        Debug.Log("Test");
    }
}

The result was always the same. It works perfectly before adding the new Input System but not after.

I was strugling with the same problem, but this excellent StackOverflow answer showed me the correct way of doing this. (Solution 6)

First, you need to add a PhysicsRaycaster to your camera. Once you have done that, you can implement the IPointerEnterHandler and IPointerExitHandler (and all the other ones) on any gameobject you like.

You can add a PhysicsRaycaster by adding the following script to your camera:

public class MeshDetector : MonoBehaviour
{
    void Start()
    {
        addPhysicsRaycaster();
    }

    void addPhysicsRaycaster()
    {
        PhysicsRaycaster physicsRaycaster = GameObject.FindObjectOfType<PhysicsRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        }
    }
}

Afterwards, you can use IPointerEnterHandler etc as such:

public class MyGameObject : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("Hello - Mouse Enter");
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("Hello - Mouse Exit");
    }
}

@v1ness

Don’t know if you figured it out but I was searching for this exact topic and there never really was an answer to what to do that I could find. Tried refining searches a bunch but your question was one of the ones at the top of the search for “mouseover unity new input” and a bunch of variations of that including the IPointerEnterHandler search as well.

Here is what I was able to do to get the Vector2 dimensions with ease:


and my current code to grab the Vector2 of the position (Which I am testing but putting it in
FixedUpdate() seems to work well so far:

Vector2 mousePosition = mouseInput.Mouse.MousePosition.ReadValue<Vector2>();

I hope this helps either you or someone else that comes by with the same issues I was having when starting out with the new Input system.