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 Nanako · Dec 09, 2014 at 10:31 AM · raycastefficiencyinfinity

What's the most efficient raycast distance, and why?

Assuming i'm casting against something specific (to see if the path to it is obstructed);

Is it most efficient to set the cast distance to exactly what i need, and no more Or is it best to use some constant value like infinity?

Also, does raycast performance depend solely on the colliders in its path? Does a raycast extending off infinitely into the distance cause any more overhead?

Comment
Add comment · Show 2
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 EvilTak · Dec 09, 2014 at 10:41 AM 2
Share

Raycasting for a longer distance is of course more CPU expensive since (I$$anonymous$$O) the engine rounds up all the colliders in that radius (so infinite means all colliders) and raycasts against them. So it is better to use the distance that you need ex. 20 meters rather than infinity.

avatar image Nanako · Dec 09, 2014 at 01:22 PM 0
Share

are you sure about that? in that case it sounds like casting for infinity is devastatingly expensive.

i'l see what kind of results i get from calculating a distance

2 Replies

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

Answer by EvilTak · Dec 09, 2014 at 02:05 PM

Raycasting also involves broad phasing, ideally first all objects outside the radius of the distance value are filtered out (thus infinite distance means no colliders are filtered out) . Then the bounds check comes into play. Collider.bounds is used (see Bounds.IntersectRay). Then as @NoseKills said, the actual raycast is done. The remaining colliders are raycasted (closest one first) and if it is a concave collider it can be pretty expensive depending on the complexity of the collider. Primitive colliders usually have better raycast performance than mesh colliders.

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 NoseKills · Dec 09, 2014 at 01:56 PM

Like @Evil Tak says, physics engines usually optimise things by filtering out objects first by some really cheap calculation. For example by checking whether the bounding volume or AABB (in 2D) of the object is in range of the ray.

Only after that they check the remaining objects' geometry to see if the ray actually passes through a hole in the object mesh's geometry or does it really hit it, since this can be a very heavy calculation depending on the object's complexity.

Of course Unity is not Open Source so we can't know for sure... but I'd be very surprised if the ray length didn't matter.

Comment
Add comment · Show 9 · 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 EvilTak · Dec 09, 2014 at 01:57 PM 0
Share

I don't know why everyone misses the space in my username...

avatar image NoseKills · Dec 09, 2014 at 01:59 PM 0
Share

I saw it but still couldn't find a way to write an " @ " reference to it. Do you know how to do it ?

EDIT: @Evil Tak O$$anonymous$$ so it's just the preview that is FUBAR and misleads people

avatar image EvilTak · Dec 09, 2014 at 02:07 PM 0
Share

Sorry I was typing my answer... The name is finally right, thank god! People always do it without a space and it redirects to some @EvilTaco who isn't even active on this site...

avatar image Nanako · Dec 09, 2014 at 02:25 PM 0
Share

So far in my testing, adding a limited range by grabbing the magnitude of my direction vector slows things down massively. Using the sqr$$anonymous$$agnitude seems to have no effect on performance, probably due it being such a large number that everything is within the radius anyway

avatar image EvilTak · Dec 09, 2014 at 02:32 PM 0
Share

How many objects do you have in your scene? Aand what platform are you targeting?

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

27 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

Related Questions

OnMouseOver or Use a Raycast? 1 Answer

Raycast - Input position is Infinity | Point Precision 1 Answer

Trouble targeting GUI with raycast 0 Answers

Quickest most efficient way of instantiating a large amount of GameObjects 1 Answer

Get ground position for Joint 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