Best way to handle collisions

Siedge posted this 16 January 2016

Okay so I drew a blank and have been trying to wrap my head around this for sometime.. I don't want to have to add colliers to the object but I would like to be able to raycast and get the first block my ray comes into contact with. How would I do that without adding a mesh collider to the object? Is it possible?

Order By: Standard | Newest | Votes
GarethIW posted this 16 January 2016


Yes, it is possible. The PicaVoxel Volume component has the method GetVoxelAtWorldPosition(Vector3 pos) which returns any voxel in the volume at an arbitrary point in world space.

If you are only interested in checking collisions on one volume, then it is a simple matter of iterating along a ray and checking if there is a voxel at each step.

If you want to check for collisions against all Volumes in the scene, then you will need to call GetVoxelAtWorldPosition on each Volume in the scene at each step along the ray. Take a look at PicaVoxelDemos/PicaIslands/Scripts/IslandsClickBoom.cs which uses this exact method with fully commented code.

However, this may not be the most performant way of doing per-voxel collision, especially if you are doing a lot of tests per frame.

Instead, you could use the hitbox that is part of each Volume to do a hybrid of ray/box testing followed by ray/voxel testing. I've written a little code snippet to show how this can be done:

Our 132 members have posted 493 times in 119 discussions