I’ve been learning Unity and as I’m just getting into it, I wanted to make sure I take the opportunity to learn the newest features of Unity and so I installed the new input system. However, while the new input system is active, the OnMouse events don’t seem to trigger the same way. I’d like to continue using the new input system but it’s new enough that all the tutorials I find which describe how to test if the mouse is pointing at an object use the OnMouseOver(), OnMouseEnter()… etc. event functions which again doesn’t seem to work the same way with the new input system. For instance, the following code chunk doesn’t log anything.
So, the question is: How should I detect the mouse (or other pointing devices) entering, leaving, hovering, etc. over a 3D object?
(Or is there a newer tutorial that uses the new input system which I should be following?)
Hi @Dirkinzs , i didnt even know there was a OnMouseOver() API x)
The most easy solution is to raycast (from player loop fixedUpdate), example (i’ve defined an Interaction interface that is implemented by Items, NPCs, UI elements, etc);
Ray ray = playerCamera.ScreenPointToRay(m_Controls.player.mousePosition.ReadValue<Vector2>()); // Input.mousePosition
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 50))
{
// Check if we hit an interactable
IInteraction interaction = hit.collider.GetComponent<IInteraction>();
if (interation != null)
{
playerMethods.Focus.SetFocus(interaction);
}
else
{
playerMethods.Focus.RemoveFocus();
}
}
Note that i’m defining max range as 50 since it’s a 3rd person controller but ofc if you’re building a RTS you might probably set it higher
Also you can specify layers in the Raycast so that anything that shouldn’t be interacted with inbetween the cursor and the object will be ignored
I appreciate your kindness, Meishin. Thanks a lot for helping me out. Ray casting is a bit different from what I’m used to since I’m used to 2D game development.