RaycastCommand is terrible. Maxhits parameter not working but it'is in the API and documented.
The multihit option in ScheduleBatch doesn't work. The correct results returned thanks to a script I created for everybody. Get it for free!While working on my asset WaveMaker, I found myself stuck due to basic problems related to the infamous RaycastCommand class.
The two problems:
- RaycastCommand results cannot be used inside another job afterwards because you can't see if there was a hit because you can't access RaycastHit.collider (it uses classes and calls a method of Collider and that is not allowed in jobs).
- RaycastCommand.ScheduleBatch(...) doesn't return the results of more than one hit, even though it is documented and is available in the API as a parameter. And that is outrageous and has been there since 2018... until now, in version 2021 (and more?).
The solution for the first problem is in another of my blog posts.
Let's talk about the second problem, the multihit support.
JobHandle RaycastCommand.ScheduleBatch( NativeArray<RaycastCommand> commands, NativeArray<RaycastHit> results, batchSsizePerJob, jobHandleIfNeeded);
So I created a fix for this using jobs. It's long to explain, but to do this, I created a process in which you throw RaycastCommands with 1 max hit, and continue doing so several times until all hits have been gathered. It's a slower process, but it is much faster than doing the raycasts by hand without jobs.
The usage is similar:
JobHandle RaycastCommandMultihit.ScheduleBatch(NativeArray<RaycastCommand> commands, NativeArray<RaycastHit> results, int minCommandsPerJob, int maxHits, JobHandle dependsOn = default, float minStep = 0.0001f)
I uploaded the folder to Gumroad and probably make a video tutorial since this problem needs knowledge from many areas in Unity that not many developers are used to or avoid: Burst, Jobs, efficiency, data structs optimization, a little unsafe hack, assemblies, etc.
The script is free but you can optionally support my work with whatever donation you want to make. Thanks!
Hope it helps!