OnMouseDown Equivalent

    private void OnMouseDown()
    {
        //Do something
    }

What is the equivalent of this in the new input system?

1 Like

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.

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.

@Chuubei : As it happened I opened a bug report with Unity to resolve this:

https://issuetracker.unity3d.com/issues/onmouseenter-onmouseover-onmouseexit-callbacks-are-not-called-when-active-input-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.

1 Like

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.

1 Like

I was looking for the same thing. I posted a bit of code that uses InputSystem to return a Collider if you click on one at this link .