Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

OnMouseDown Equivalent

Discussion in 'Input System' started by Chuubei, Jul 19, 2020.

  1. Chuubei

    Chuubei

    Joined:
    Jul 7, 2019
    Posts:
    3
    Code (CSharp):
    1.     private void OnMouseDown()
    2.     {
    3.         //Do something
    4.     }
    What is the equivalent of this in the new input system?
     
  2. tanayg

    tanayg

    Joined:
    Jul 20, 2020
    Posts:
    4
    Hi Chuubei,

    I think you can achieve that by adding a new Press interaction to your action, and setting the Trigger Behavior to Press Only.
    upload_2020-7-20_21-51-24.png
     
  3. Chuubei

    Chuubei

    Joined:
    Jul 7, 2019
    Posts:
    3
    Thanks for the response tanayg, I tried it out but then if I click the left button anywhere it will fire the code. Sorry I should've provided more details. Basically my intended behavior is to have a game object fire code only when the left mouse button is clicked when the mouse position is over the gameobject, not whenever the left button is clicked in general. Sorry, I hope this clears up what I'm trying to do.
     
  4. softcraftca

    softcraftca

    Joined:
    May 16, 2020
    Posts:
    2
    @Chuubei: As it happened I opened a bug report with Unity to resolve this:

    https://issuetracker.unity3d.com/is...t-handling-is-set-to-input-system-package-new

    Input System and the OnMouse* events are currently incompatible with each other. I'm not sure if Unity intends to make them work, or just deprecate OnMouse*.

    In the meantime, the best replacement I have is:
    1. Detect when the mouse is clicked via Input Actions.
    2. Get the mouse coordinate at click.
    3. Call Camera.main.ScreenPointToRay() to create a ray from the screen coordinates into the world.
    4. Call Physics.Raycast to see if anything was hit.
    5. If so, then the resulting RayCastHit will have the transform of the GameObject that was clicked on.

    This is definitely a bunch more work than just calling OnMouseDown, so I'm hoping that Unity gets OnMouse* working with the Input System. However, this technique does give you a bit more control over what happens; you can detect exactly *where* on the object the mouse was clicked for example.
     
    reinfeldx likes this.
  5. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    117
    Thanks for sharing this, and I voted for that bug. This seems like pretty basic functionality that I'm surprised is not handled better by the new input system.
     
unityunity