I know I’ve seen threads about this before, but I can’t seem to turn them up with search, so here goes.
I need my mouse cursor to be able to interact with objects in my scene. So, when the mouse hovers over an object, it’ll detect that, and when the mouse clicks on an object, it’ll detect that as well. Just to be clear, these are actual 3D scene objects, not GUI elements. I’m thinking I can use WorldToScreen or ScreenToWorld to help me do this, but in reading on those, there were a couple questions I had.
First, if I want the game to register anytime the mouse is over any part of my game object, how would I do that? I know you can use the object’s transform with WorldToScreen for this, but it only returns the object’s center. So, if the mouse is over an outer edge of the object, it wouldn’t register as being over the object.
Second, ScreenToWorld, I think, needs to know the distance from the camera for the z component. Is that right? I have to give it the distance, but if it is supposed to detect when it is over an object dynamically, and the distance may be variable, this function seems useless.
Third, I was thinking a raycast might help. Maybe not. My thought was to cast a ray in a given direction, using ScreenToWorld to set the direction vector, and that would return if it hit an object collider, which would handle the issue about the mouse being over any part of the object (assuming the collider completely encompasses the object). However, to detect any time the mouse is over an object, that would require a raycast every frame, which can’t be an efficient sollution.
Can anyone point me to a thread that may help, or offer some suggestions?