Best way to handle collisions

0
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?

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

Hi!

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:

https://gist.github.com/GarethIW/207bf07e76fa77fca020


Our 123 members have posted 456 times in 110 discussions