Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 Bokaii · Aug 04, 2014 at 07:28 AM · raycastmultiplayerraycastall

[CLOSED]RaycastAll find closest hit

I have a fps controller and I want to send a RaycastAll which returns the closest hit that is not us.

I know that:

1) a Raycast doesn't stop at the first hit point 2) how to send a raycast

I just don't understand how to loop through all of the hit points and find the closest one that is not us.

I've already asked this question once, but they said that I didn't give a specific question.

I've checked the docs, but that is really hard to understand. So if someone could translate it into kindergarten language that would be great! :)

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
1

Answer by robertbu · Aug 04, 2014 at 07:48 AM

If you just want the object hit first, then just use Raycast(). It is RaycastAll() that collects all the hits and where you cannot depend on the order of the hits in the returns array. Raycast() stops with the first/front collider hit. Unless you have things setup strangely, a Raycast() should not hit the collider it originates from. Colliders are one-sided, and a Raycast() is typically from the inside of a collider (and therefore does not see see the collider). Here is a basic Raycast that might be used on a FPS:

 var hit : RaycastHit;        
 if (Physics.Raycast (transform.position, transform.forward, hit)) {
     Debug.Log("The ray hit something");
 }  

If you need to process a number of objects and use RaycastAll() give me the context and I (or someone) can give you a bit of sample code to help you.

Comment
Add comment · Show 2 · 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 dsada · Aug 04, 2014 at 07:51 AM 0
Share

I am not sure that is deter$$anonymous$$istic that which objects that single raycast will give back if it can hit more than one. Are you?

Im just suspicious because the doc is writing that RaycastAll is not giving back the elements in the right order

avatar image Bokaii · Aug 04, 2014 at 08:18 AM 0
Share

Raycast stops at the first collider hit. But that could be myself with the standard fps controller, because the main camera is inside the controller. If you look down you hit yourself. I want to make sure that it is not possible to hit yourself by using raycastAll, this is not only because of the game I want to make, but also because I want to learn it!

And no, RaycastAll doesn't return the hit points in the right order, it mixes them up.

avatar image
0

Answer by dsada · Aug 04, 2014 at 07:47 AM

Okay, so i edited for the asker's request and put in robertbu's nice and right remark

The RaycastAll function will give back an array of RaycastHit objects.

 RaycastHit[] hits;
             hits = Physics.RaycastAll(yourPosition, YourDirection, yourMaxDistance);
 //"shoot" a ray from the 1st parameter in the direction of the 2nd parameter and check for maximum the 3rd parameter distance. Objects that are further than the 3rd parameter are not going to be recognized

   
 

Then you can iterate through this array and find the min distance. Of course you cant do this if your hits array hasnt got any element so check it before the min search

 if(hits.Length > 0) //if no object was found there is no minimum
 {
   if(!(hits.Length == 1 && hits[0].transform == gameObject.transform)) //if we found only 1 and that is the player object there is also no minimum. This can be written in a simplified version but this is more understandable i think.
   {
     float min = hits[0].distance; //lets assume that the minimum is at the 0th place
     int minIndex = 0; //store the index of the minimum because thats hoow we can find our object
     
     for(int i = 1; i < hits.Length; ++i)// iterate from the 1st element to the last.(Note that we ignore the 0th element)
     {
        if(hits[i].transform != gameObject.transform && hits[i].distance < min) //if we found smaller distance and its not the player we got a new minimum
        {
            min = hits[i].distance; //refresh the minimum distance value
            minIndex = i; //refresh the distance
        }
     }
   }
 }

And you got your gameObject at this point by hits[minIndex].collider.gameObject; //get our gameObject with the help of the index.

Comment
Add comment · Show 4 · 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 robertbu · Aug 04, 2014 at 07:51 AM 0
Share

@$$anonymous$$r$$anonymous$$elonPie - you asked about eli$$anonymous$$ating yourself from this list. I don't believe the 'you' will generate a hit, but if it does, you can change @dsada's example by changing line 6 to:

 if(hits[i].transform. != transform && hits[i].distance < $$anonymous$$)
avatar image Bokaii · Aug 04, 2014 at 08:15 AM 0
Share

Thanks for the answer both of you! This was really useful @dsada But can you explain a bit more of what you are actually doing? Like maybe in this code comment it out, saying what you are doing in this and this line? that would really be great!

@robertbu, if I have a fps controller, the standard one that comes with unity asset, When you ask its main camera to send a raycast forward, then if you look down, you will hit your own collider.

avatar image robertbu · Aug 04, 2014 at 02:11 PM 0
Share

What kind of collider do you have on your FPS? Typically a capsule collider is used, and I've never heard of a capsule collier getting hit from a raycast from the pivot of the capsule. Do a:

  Debug.Log(hit.name+", "+hit.tag);

...inside the raycast to verify what is getting hit. If you have a more complex mesh on the player, or if the player is holding something that the raycast might hit, then there are alternate solutions to solve the problem. One solution is to put your player on its own layer, and use a layer mask to eli$$anonymous$$ate the player. Be careful doing your research with layer masks. They are bitsets, not integers, and it is easy to get them wrong. I$$anonymous$$HO RaycastAll() is not the right solution to fix the probelm. There are other potential solutions to the raycast hitting the player, but I'd need to know what is being hit, its relationship to the player, and how the part that is getting hit interacts with the environment.

avatar image Leoo · Jul 07, 2015 at 12:02 PM 1
Share

Please, avoid that check by ignoring the player layer on the raycast! better performance and less code!

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[CLOSED]RaycastAll Help 1 Answer

Bullet Effect (RaycastAll Question) 1 Answer

Check if RaycastAll hits[i].point == null 0 Answers

RaycastAll Detect if nothing hit 1 Answer

Null reference exception on Screenpointtoray (Multiplayer) 1 Answer


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