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
0
Question by D43DB33F · Jul 22, 2017 at 01:48 AM · unity 5raycastthreadsmultithreading

Performing ray casts from multiple threads within a FixedUpdate call

I have a script which simulates realistic drag forces by casting rays at the collider of an object in the opposite direction of its velocity. All of this is done once for every FixedUpdate call.

However it seems to me like casting a ray and checking whether it hits the collider should be a readonly operation. Thus it should be doable from multiple thread at a time, shouldn't it ?

I would like to divide the set of ray casts I have to perform by as many logical cores are available in order to improve the performance of this.

I don't know what is thread safe and what is not in Unity, but all of this would happen within a single FixedUpdate call. I could have N threads waiting to be resumed, and resume them when FixedUpdate is called, let them do the job, then suspend them again once they are finished and let FixedUpdate return. Since the main thread would wait for all the threads to finish their work, it would be a lit like if everything was still done on the main thread. It's not like I was trying to ray cast from a background thread while the main thread is processing physics, which would obviously be a problem.

Is that possible ? I could try of course but even if it works several times it's not a guarantee that it will always work since the problems this could cause are sort of random.

Are Collider.Raycast and Physics.Raycast thread-safe ? Thank you.

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 tanoshimi · Jul 22, 2017 at 06:43 AM

Nothing in the Unity API is thread-safe. You can of course despatch worker threads to do various "offline" data-processing tasks (image processing, terrain heightmap generation etc), but anything that interacts with the scene must be called from the main thread.

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 D43DB33F · Jul 22, 2017 at 02:55 PM 0
Share

Ok thanks for your help. I was thinking that, maybe, with "read only" operations such as ray casts , and when the main thread is waiting, it would have been possible. Too bad :(

avatar image Bunny83 D43DB33F · Jul 23, 2017 at 10:09 AM 0
Share

No, Unity actively prevents any other thread from using the Unity API by throwing an exception if used from any other thread than the main thread.

Though i've seen proof-of-concept projects that did a something like 10k raycast in a 360° circle in a 2d project to calculate realtime lighting / "field of view" and was still smooth. However it of course depends on the scene complexity, the hardware you run on and what other heavy things you do.

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

155 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 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 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

Unity Threading Issue: Not able to delegate task 0 Answers

[C#] Line Renderer beam not going straight 1 Answer

How to find colider steepness? 1 Answer

Camera go through the wall. Please look at my script and help !!! 0 Answers

How does unity thread works? 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