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.

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.