I have a number of objects in my game where OnMouseOver is displays a GUI.Label with the name when near enough. All of this works just fine until I get ‘too close’ to the object.
For example - I have a small item that rolls, when it is within ‘4’ (I have a constant Debug.Log printing the distance between Player and Object) the label shows as expected. When the object is closer than about 2.5 (sometimes as high as 3) the label stops showing on mouseOver.
I’ve checked my code about 16 times to ensure I’m not doing something stupid like >= or == rather than <= :
void OnMouseOver() {
if(Vector3.Distance(gameObject.transform.position, cam.transform.position) <= 4) {
}
}
Not exactly sure (as I’m quickly skimming this), but did you check the clipping planes on your camera? You can try to decrease the “Near” value to see if that helps.
I have tried this, yes, and am afraid it causes no difference Near was set to 0.1 as standard, tried values of 0.01 and 0 but no difference - I even tried changing during gameplay. The object stays visible, its just the OnMouseOver (or the if(Vector3.Distance) ) fails to call.
Edit: Just checked - its OnMouseOver() which fails at shorter distances.
Hmmm. I’d seen raycasts mentioned when looking but assumed that related to the distance part of my function rather than the onmouseover. I’ll do some digging, thanks.
You’ll have to do raycasts from the mouse position using the player’s/camera’s (probably latter) forward direction. You can also limit the raycast length as an optional parameter right into the raycast statement.
However, raycast itself isn’t a function like OnMouseOver(). You’ll have to call it somehow, and that can depend on you. Do you want to call it every frame/fixedUpdate? Do you want to set a timer (like 0.5 seconds) to fire it? Or do you want some function to actually occur? It’s all your choice.
You probably can’t run the detection when the distance is lesser then 2-3. as MDragon said i’d suggest using raycasts to determine a mouse over. Or you can do something like , checking if the mouse pointer is inside game objects transform yourself and create a MouseOver event.
I’m not sure how does the OnMouseOver works , but i think it just checks if your mouse pointer is inside gameObject’s transform and raising an event.
Just reset my player character camera to that which comes with the standard asset FPC camera and the problem has gone away - looks like setting the near clipping to 0.01 (as low as I could) was the problem, set to 0.3 works like a charm.
Thanks for the suggestions though - I feel I will require the use of Raycasts in the near future.