• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by wolis · Mar 24, 2012 at 03:35 PM · raycastrayhitinfo

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by farooqaaa · Mar 24, 2012 at 03:38 PM

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
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wolis · Mar 24, 2012 at 03:47 PM 0
Share

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);

avatar image Gerflukon · Feb 17, 2014 at 05:01 PM 0
Share

Is there a way of only doing something if the raycast hits a specific object? If so, how do you confirm that the object was hit by the ray:

 if(Physics.Raycast(ray, out rch))
 {
     if(*ray hits specific object*)
     {
         // do something
     }
 }

I know this question was asked two years ago and that this is a long shot but hey! :P

avatar image Clonkex · Feb 15, 2015 at 02:14 PM 0
Share

@Gerflukon lol almost exactly a year later :P Use RaycastHit.transform.gameObject, as per this page:

http://answers.unity3d.com/questions/141529/getting-gameobject-from-raycast-hit.html

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect if a raycast ray stop hitting an object 1 Answer

Trouble with Ray & Raycasting and AddForce() 1 Answer

How to get information from raycast? 2 Answers

Raycast isn't working on small distances 1 Answer

Fall collision force with help of raycast 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges