- Home /
Question by
Intensity116 · Oct 28, 2010 at 10:04 PM ·
raycastpositionvector3mouse
2d Position to 3d Position
How can you tell what gameobject the player's mouse is pointing at?
Comment
Best Answer
Answer by · Oct 29, 2010 at 01:16 AM
If you want to use a raycast (or only check on a particular frame, rather than needing to attach that script to every object), you can use this simple script (reposting from another answer):
function Update ()
{
if ( Input.GetMouseButtonDown(0) )
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100.0))
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
Answer by Atnas1010 · Oct 28, 2010 at 11:20 PM
You should be able to use the OnMouseOver event.
From the documentation:
function OnMouseOver () : void Description
OnMouseOver is called every frame while the mouse is over the GUIElement or Collider.
Have this function present in a script that is attached to every gameobject that you want to know if it is pointing at.