Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Testing mouse collisions with new input package

Discussion in 'Scripting' started by Dirkinzs, Oct 9, 2020.

  1. Dirkinzs

    Dirkinzs

    Joined:
    Sep 18, 2020
    Posts:
    2
    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.
    Code (CSharp):
    1.     void OnMouseOver()
    2.     {
    3.         Debug.Log("mouse entered");
    4.     }

    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?)

    Thanks,
    Jaye
     
  2. Meishin

    Meishin

    Joined:
    Apr 12, 2019
    Posts:
    26
    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);
    Code (CSharp):
    1. Ray ray = playerCamera.ScreenPointToRay(m_Controls.player.mousePosition.ReadValue<Vector2>()); // Input.mousePosition
    2. RaycastHit hit;
    3. if (Physics.Raycast(ray, out hit, 50))
    4.   {
    5.       // Check if we hit an interactable
    6.       IInteraction interaction = hit.collider.GetComponent<IInteraction>();
    7.       if (interation != null)
    8.       {
    9.           playerMethods.Focus.SetFocus(interaction);
    10.       }
    11.       else
    12.       {
    13.            playerMethods.Focus.RemoveFocus();
    14.        }
    15. }
    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
     
    Last edited: Oct 9, 2020
  3. Dirkinzs

    Dirkinzs

    Joined:
    Sep 18, 2020
    Posts:
    2
    Thanks so much, Meishin. That is exactly what I was looking for. I'm used to 2D game dev and ray casting has been a bit out of my experience so far.
     
  4. Cpstester

    Cpstester

    Joined:
    Sep 17, 2021
    Posts:
    3
    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.
     
    Last edited: Oct 2, 2021