Using Physics.overlapsphere to detect mouse

For those who profile a lot you know that raycasting and sphere collisions are slow…is it possible to have a physics.overlapsphere be a trigger for the mouse?

Have you looked into IPointerEnterHandler?

1 Like

I believe that won’t work on mobile will it? It would need to be cross platform compatible.

Also are you saying I should turn my navmeshagents into a canvas? Isn’t this a UI event feature?

No…The IPointer handlers work on 3D objects as well. They should work on mobile, although of course IPointerEnter won’t because there’s not exactly a pointer moving about. IPointerClickHandler appears to be what you want. You could also just have OnMouseUp, but I prefer the Interface-based option.

You’d simply add a PhysicsRaycaster to your camera. That one allows to have 3D objects with a collider to be an event receiver, or 2D objects respectively when using PhysicsRaycaster2D.
The behaviours which should receive the desired event either implement the appropriate interface or you attach some EventTrigger components to your GameObject, which allow you to directly hook up event handlers in the inspector.

That kind of interaction could also be self-implemented using GetComponent and raycasting, but that’s what the engine already does for you (with various options) under the hood when you use these interfaces/EventTriggers and the raycasters. The UI works just the same, except that it’s using a GraphicsRaycaster instead (which you usually find on a canvas).

actually the thing is…i want to remove colliders all together. Raycasting my mouse is actually the only reason i even have a collider…this is why i was asking if physics.overlapsphere(for performance reasons…)can be a replacement somehow…will this event trigger work with it?

You can simply use OnMouseEnter, OnMouseOver, OnMouseExit and / or OnMouseDown in a script attached to the gameobject with the collider. It will work for mobile.