x


2d Position to 3d Position

How can you tell what gameobject the player's mouse is pointing at?

more ▼

asked Oct 28 '10 at 10:04 PM

Intensity116 gravatar image

Intensity116
39 7 8 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
      }
   }
}
more ▼

answered Oct 29 '10 at 01:16 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 28 '10 at 11:20 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1535
x985
x887
x580

asked: Oct 28 '10 at 10:04 PM

Seen: 1552 times

Last Updated: Oct 28 '10 at 10:04 PM