Hi! basic question - how do you click on an object in a 3d scene with your mouse so that something happens?
Use OnMouseDown() (object must have a collider).
–Eric
what about to use OnMouseDown() inside the OnGUI function???
no, it should be in a script attached to the object with collider
Its not nessicary for any OnMouseDown to be attached to any specific object. You can just as easily have a GameControler(GameObject) that handles all of your selection, targeting and scoring. I am not a fan of pushing code on every object.
using a Physics.Raycast with the below code should get you what you have your mouse over. Or, what you clicked on.
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Searching the web, i found this, i want my player aims to an object (3d) when i press mouse button, i have used OnMouseStay, but looks like is not working fine on 3.4 ?.. depending the camera position and orientation, the collider works and does not work from other view… i´m not sure if this is a bug…
Is there a way to get return a name or a tag with the misterbig above code?.. because it returns vector3 origin and screen coordinates…
thanks.
Hi,
Once you have the ray, use Physics.Raycast() to get the hit collider, then you can use the normal stuff…collider.name, collider.transform, collider.gameObject, etc.
Do this in one “global” script…one which you put on an empty game object (or some other object which will always be in your scene). It may be helpful to compare the hit object to a layer ID as well, to limit what can and cannot be selected. You can also notify the object which is clicked by using SendMessage() or BroadcastMessage() and you can make the receiver option, which means you can send the message and not care if the receiver actually has a function by that name in it. I use the name “OnSelected()” to be clear this is an event which is triggered when something is selected.
You WILL need a mouse event to know when something is tapped, unless you want a mouse-over to trigger things (not sure why, but might be needed for some games - just don’t use SendMedssage() if you are spamming it constantly!). I use EZGUI, so I just tie my selection script in to the pointer it gives me.
More advanced, and not always necessary:
Note that Physics.Raycast() will stop when it hits an object. If you want to test everything along the ray you can use RayCastAll() then do a foreach loop to process the array of colliders that returns. Generally you only want to select the first item hit though, and not objects occluded by other objects (you don’t want to click a wall and selected an enemy on the other side, for example.) Still, if you want to do LayerMask filtering you will need this because then YOU will choose when to stop looking. In other words, you can hit everything, then check each item against a mask and stop when you hit the first one which is in a layer you want to select.
I did something kind of tricky for our game. I recursively move up the hierarchy from the selected collider until I find a layer, then I test against that layer (and stop regardless of what layer it has). Doing it this way allows me to have more colliders in one object, as long as the GameObject with the collider isn’t in a Layer, my script checks its parent, and then its parent, etc. Just food for thought, and again, not necessary for many games.
thanks … ! i will try it…