I am creating a custom editor tool that should allow me to snap objects to a static collider (like the ctrl-shift mode of the move tool) but also aligns the object to the surface normal. I can’t figure out how to do the snapping so that the entire collider is always on top of the surface.
Is there any way to see the source code of the move tool so I can learn how to do it?
You can raycast against colliders and get the normal at the point that you hit.
You can get the colliders bounds.
You can access the mathematical properties of primitive colliders.
That should be everything you need, except for some additional basic math and your custom logic and rules/criteria for snapping.
I already have the surface snapping, even with averaging multple casts, to smooth the placement! I just have no idea how I would get the bounds of the selected object (What if there are multiple colliders, or the colliders aren’t on the root node?) and then snap it correctly.
I’d have to somehow get a point that describes the “bottom” of the object somehow.
Not sure what shapes you’re dealing with, but you can always combine the bounds. Just get all bounds of the attached colliders, and use like Bounds.Encapsulate. It takes points as well as other bounds.
There are different bounds you can access. For colliders, there are only axis aligned bounds. Renderers have a bounds property, which return AABB too. Meshes have a bounds property that returns the bounds in local space.
I don’t know your requirements, but most of the time you can build what you want using these bounds.
If bounding boxes aren’t enough, you could build a collider detection system which recognizes all collision / trigger events and forwards them to a controlling component or you could use the physics API in order to execute spatial physics queries and build a similar system.