• 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 /
  • Help Room /
avatar image
0
Question by stjimmy96 · Mar 31, 2016 at 10:11 PM · raycastfpsraycasthitinput.mouseposition

Raycast to multiple objects

Hello everyone, I'm here because I've been stuck on a problem for the whole day. My problem is simple (I guess) but I cannot figure it out: I'm in a 3d enviroment and I have an amount of objects (simple cubes with a Box Collider attached on them), I need to check if the cursor is pointing to one of those cubes and, if so, which of them. I need to retrieve the GameObject of the collider which has been hit by the Raycast so I can perform various operations on it.

In the first place I tried using an array of cubes and using the collider's Raycast method to check which cube was hit, like the following code:

 foreach(GameObject cube in carCubes) {
        RaycastHit cubeHit;
        BoxCollider box = cube.GetComponent<BoxCollider>();
        if (box.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out cubeHit,100f)) {
         
                //cube was hit
         
         }
 }

But then I figured out this wasn't the solution since this algorithm does not consider the order of appearance of those cubes and so if, from the camera's point of view, there was multiple cubes overlapped, it could find as the first hit a cube on the back.

So I moved to the following code:

 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit, 100f) {
 //hit
 }


But now I cannot figure out which cube was hit by the raycast and that information is crucial because I need to know which face of the cube has been hit with the cursor. So the problem is: how can I get which face of the cube and which cube has been hit ?

I know the problem may be very easy to solve, but I'm still a beginner and I couldn't find out a working solution.

Thank

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Mar 31, 2016 at 11:52 PM

using Physics.RaycastAll instead might help you out http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html

it returns an Array of everything hit so you can loop through for whatever you want.

Comment
Add comment · Show 1 · 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 stjimmy96 · Apr 01, 2016 at 06:19 AM 0
Share

Thank you for your answer, but I still have a doubt: if the array it returns contains all the the object the raycast has hit, how can I know which RaycastHit was the first one to be hit from the camera's point of view ? For example excluding all the objects behind the first one in order of appearance.

avatar image
0

Answer by stjimmy96 · Apr 01, 2016 at 12:26 PM

I figured out a way to do it. I didn't notice RaycastHit has a propriety called Transform which returns the transform of the Collider that has been hit. So I used RaycastHit hit;

 //r is a Ray from the mouse position
 if (Physics.Raycast(r,out hit, 100f) {
     Transform cube = hit.transform; //cube is the object
     
     // Other stuff...
 }

I used RaycastHit.normal to calculate which face was hit.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't my ray working? 1 Answer

Disable Damage Raycast? 1 Answer

Why am I getting a Null Reference Exception? 0 Answers

Raycasts stop working after ~3 seconds 1 Answer

Help with photon being used to make a mulitplayer fps 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