- Home /
Other
Understanding Raycast How Actually works in Unity [As Algorithm]
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 'https://docs.unity3d.com/ScriptReference/Bounds.IntersectRay.html' 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!
just a question, but what are you raycasting for if your colliders you raycast against never move? You should be able to $$anonymous$$NOW where you're going to hit given that situation
Colliders never move, but the start point of raycast and direction changes.
Answer by DaDonik · Nov 20, 2018 at 10:15 AM
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.
Yes Actually I didn't know there was something like 'octrees' :P After this question I found it.
Thank you ^^
Answer by Nocktion · Nov 20, 2018 at 07:19 AM
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.
Follow this Question
Related Questions
Mouse Click + Raycast + Colliders 2 Answers
Colliders vs Callbacks: Which is better for performance in my case? 0 Answers
Call physics api from parallel thread 1 Answer
Can't Make Raycast Target Nearest Character 1 Answer
Held cube clips through wall 0 Answers