- Home /
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.
Hello same here. I got several cube with onmousenter script to display gameobject above them. With old system, was working fine. With new one not.
I tried to swap for IPointerEnterHandler.OnPointerEnter IPointerExitHandler.OnPointerExit
But that doesnt work, even a simple debug inside those method did not prompt. and i also need to reimplement On$$anonymous$$ouseDown as it do not work too with new system.
I tried migrating to the new input system last week, and I'm having the exact same problems with no answers. I can't get any On$$anonymous$$ouse events to work. IPointers only work for me on UI images with the Raycast Target set to true. Using 2019.3.15f1.
Answer by KevinDW · Jul 29, 2020 at 09:22 AM
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");
}
}
Answer by KanyeWestknight · Oct 30, 2021 at 08:54 AM
@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.
Your answer
Follow this Question
Related Questions
OnMouseEnter/Exit not working when pressing button to advance scene 2 Answers
The OnMouseOver and OnMouseEnter functions cease to work when screen.lockcursor = true 0 Answers
OnMouseExit fires every frame when the mouse is down? 2 Answers
Instantiate object OnMouseOver, then delete the instantiated object OnMouseExit. 1 Answer
Change cursor on OnTrigger 0 Answers