Why not OnMouseDown?

Is there a reason not to use OnMouseDown() instaed of the IPointerClickHandler interface?

It seems to me that the interface requires many more steps than OnMouseDown?

Hi there,
OnMouseDown is triggered when the mouse is clicked anywhere on the screen. IPointerClickHandler is designed for UI elements, and triggers only when you click on the visible portion of that specific GameObject.

If you use OnMouseDown, you probably also need to use a Raycast to figure out what you clicked on and then check for physics objects. This is probably the best method for a more complex game with a lot of different objects in visuals.

In this case IPointerClickHandler is just a shortcut to save time raycasting etc.

OnMouseDown is called on objects that have a collider where the mouse is located during the call. It is propably possible to click on several objects at once if they overlap from the cameras perspective though?

We are using a Physics.Raycast when the mouse is pressed (OnMouseDown) to find objects with colliders. Raycast general just returns the first collider it hits. If you wished to return multiple objects, there is a method called Physics.RaycastAll. This returns an array of anything the raycast passes through.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms