The method OnMouseDown() is called once when the mouse presses my box 2d collider.

Is there a method that keeps firing while the mouse is still holding the button down inside the collider? Something like WhileMouseDown().

I could use Input.GetMouseButton(0) in the Update() method. However, this does not check if the mouse is pressing the collider.


You could use a combination of both OnMouseDown and OnMouseOver:

OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.

You might want to set something like bool mouseIsBeingHeld = true; to track this and change it when OnMouseUp or OnMouseExit happens.

If you'd ever also want to just use Input.GetMouseButton(0), the other way to see if a Collider2D is being hovered over is via Collider2D.OverlapPoint.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.