x


Raycast retuning info of hit object

Hi guys, I believe this is a very simplistic question, but I havent managed to find an answer to this one (at least one that I would understand).

My code:

  bool bIsButtonDown = Input.GetButton("Fire1");
           if (bIsButtonDown)
           {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit rch;// = new RaycastHit();

           }

After hitting an object I would like to get info (tag, or more prefferably name) of the object. My goal is very simple - three boxes in the scene, and I want something to happen when one of them is hit. After getting info it is probably just a simple "switch" but I can`t seem to understand how to get that info.

Being a noob, it will help me even if you can at least point me in the right direction. :)

more ▼

asked Mar 24 '12 at 03:35 PM

wolis gravatar image

wolis
24 5 8 9

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

1 answer: sort voted first

To do a raycast you can use Physics.Raycast() method:

bool bIsButtonDown = Input.GetButton("Fire1");
if (bIsButtonDown)
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit rch;

    if(Physics.Raycast(ray, out rch))
    {
       // do something
       // rch variable contains all the info about the object that is hit
    }
}
more ▼

answered Mar 24 '12 at 03:38 PM

farooqaaa gravatar image

farooqaaa
416 2 5 8

Thank you! I didn`t know that, now it works like a charm! :) In case anyone else needs the same thing I just used this line:

name = rch.collider.name; Debug.Log(name);

Mar 24 '12 at 03:47 PM wolis
(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:

x1528
x193
x151
x11

asked: Mar 24 '12 at 03:35 PM

Seen: 860 times

Last Updated: Mar 24 '12 at 03:47 PM