Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by IddyBiddyBen · Sep 08, 2016 at 04:23 AM · raycastphysics2d

RaycastNonAlloc return value ordering?

If I do RaycastNonAlloc (Physics2D if it makes a difference) and the provided array isn't big enough to hold all of the results, does it prioritize the results closer to the origin? RaycastAll explicitly says the ordering is not guaranteed, but NonAlloc doesn't say anything at all about what it does about overflowed results. I don't expect to get more than a couple so I can just make sure the array is more than big enough if that's what is expected, but it'd be nice to just be able to pass in a single item array since I only care about the first hit.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MelvMay · Sep 08, 2016 at 07:04 AM

All Raycast are sorted by distance (closest to the start first). For the NonAlloc call, the first N (the size of the provided results array) are returned so they'll be the closest.

You could easily check this out yourself with a simple test though.

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
avatar image
1

Answer by slake_it · Jan 28, 2019 at 01:28 PM

This answer by @chriscode in the comments is great

"Bit belated, but if anyone else sees this and is confused. The 2D are sorted, the 3D are not.

2D https://docs.unity3d.com/ScriptReference/Physics2D.RaycastAll.html "The colliders in the array are sorted in order of distance from the origin point"

3D https://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html "Note that order is not guaranteed."

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
avatar image
0

Answer by Netuddmeg · Mar 21, 2018 at 08:00 AM

I'm note sure. According to my observations the result is not ordered.

Comment
Add comment · Show 6 · 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 MelvMay ♦♦ · Mar 21, 2018 at 08:23 AM 0
Share

@Netuddmeg All raycast methods use a single internal function to perform the sort. This single raycast method performs a sort based upon hit-fraction (normalized distance from start).

avatar image zaneeverettMRI · Jun 25, 2018 at 06:05 PM 0
Share

@$$anonymous$$elv$$anonymous$$ay This is not true. Doing some debugging on RaycastNonAlloc. $$anonymous$$y first entry in the results array has a distance of ~30 units. The second entry has a distance of ~8 units. I'm including triggers in the parameters and excluding anything on the IgnoreRaycast layer. I'll have to do my own distance sorting on this.

avatar image MelvMay ♦♦ zaneeverettMRI · Jun 25, 2018 at 07:18 PM 0
Share

It is true. Results are sorted by distance before being returned. I have no idea how you can have $$anonymous$$us values, distances are zero and above.

avatar image zaneeverettMRI MelvMay ♦♦ · Jun 27, 2018 at 01:19 PM 0
Share

Those are not $$anonymous$$us signs. They are tildes, meaning about. The only thing I know of that uses ~ as a negative operator is Standard $$anonymous$$L, which has nothing to do with Unity. In all other mathematical and computational uses when placed in front of a numerical value, it either means a logical not (which wouldn't make contextual sense in this case) or approximately. I am expressing that I am removing all the decimal points from my statement. Because you are right, the properties of the distance formula prevent a negative value being returned.

The 30 value is first in the array. The 8 value is next. Clearly, the values are not sorted by distance. It may be the case that they are sorted if you use only one layer and don't include triggers in the calculation. But, that is not what I am doing, and they are not sorted, and people should be aware that the values are not sorted in this case. $$anonymous$$y best hypothesis is that colliders are sorted and entered into the array and then triggers are, but I have not investigated this further.

Show more comments

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

8 People are following this question.

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

Related Questions

How can I get the rotation of Handles.DrawLine to match up with an Objects forward? 0 Answers

Character Controller 2D physics or raycast ?? 1 Answer

why Raycast2D doesn't work properly? 0 Answers

Physics2D.Raycast returns incorrect normal when casting from inside collider 1 Answer

2D Raycasting to check if grounded not working 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