I am trying to make a system that allows me to do raycasting in ANOTHER Thread, so I will able to do tons of raycast without impacting game thread, on static objects.

All of my colliders are only BOX collider and all of them never moves.


So currently What I did is, I have created a list of all the bounds of colliders.
I found ‘Unity - Scripting API: Bounds.IntersectRay’ however this applies for single bound.

So how can I make this work?


[I wrote something like, each time when I raycast, I loop all the bounds and check if it hits any of them If there are any hits, I return closest one, however, I am raycasting about 100.000+ time per frame and I a have 1600 object at least, that takes at least +500~ ms so it’s not a really good way to do that]


I searched for how actually unity did it, however, I couldn’t find any documentation about it.


Thank you!

I am not sure how exactly Unity does it internally, so i’m not going to comment on that.


What i can tell you is that you are kind of reinventing the wheel here. Granted, you have a pretty custom usecase here, because everything is static. Still you probably won’t be able to outperform the built-in raycasting.


If you still want to go the custom route, then i highly suggest you take a look at space partitioning algorithms. For your static objects, an Octree will probably be the fastest and it is pretty easy to implement. There are many resources out there regarding octrees.
I can guarantee that this will improve your custom raycasting speed by orders of magnitude.

First, I don’t understand why did you make a list of the bounds.
Second, no you can’t. And why? In Unity to put something in another thread you must create a job. There are a lot of thing you can’t do in a job lile Raycasting, Instantiation etc… But actually Unity can handle a relatively large amount of raycasts.