So, inspired by this YouTube video I made a collision based building snapping system in blueprint.
The ingame handling is pretty smooth and the results are looking good so far.
As you can see the configuration of these can become quite messy. All in all they are fairly easy to use though. Every box has an Enum category set that only allows one building type to be snapped there. The box’s world location and rotation is used for the basic snapping calculations. There are Booleans you can switch to limit the different rotations an object can have after snapping (like walls on a foundation can only be forward or backward to the snap box). As the boxes have their pivot point in the center, a local offset was needed so the snapping works correctly.
The box’s extent can be tweaked in order to control where the player has to look in order to trigger snapping. Boxes get traced by multi line traces from the camera (and as I made this for a top down view, through the HitResultUnderCursor and a bit further in that direction). I used a custom collision trace channel for them.
There are quite a few collision boxes for each actor so I don’t really know about the performance hit. With 512 foundations placed, I dropped to about 18 fps in the editor and was at solid 40 fps in the game (depends on the setup of course).
Maybe this could be improved with having less colliders or even only having colliders on the object that has to be placed. The advantage when having it this way is that you can aim somewhere in the air and still hit a collider to trigger snapping. If you don’t have them you’d rely on the player aiming directly at the building he wants to snap to and also have to calculate which snap point he prefers, probably based on lowest distance of the trace hit to all the possible snap points.
I tried something similar before with a sphere trace from the cursor hit location that scanned for building parts, took the closest snap point of wanted category and snapped to that. This was not collision based, the snap point was a child class of Arrow components actually. It worked, but the snapping was kinda jerky as the player had to aim at least somewhere near the target block in order to trigger snapping, and it would always default to the closest point. This was sometimes not desirable eg. when you have roofs away from the ground and your hit locations are basically limited to aiming directly at it. Like when trying to switch between placing a wall on top or below said roof.
Other techniques use socket points for the snapping, but I also wanted something more intuitively controllable from within the blueprint.
The system as it is works pretty well and I might even make it public so people can use it (there are not many good tutorials on this and I don’t think people should have to pay for something as simple as this).
As you can see however, it is not yet perfect, so I welcome any suggestion, advice, opinions or maybe this is even complete bull***t and you can tell me how things like this are actually done
If there is demand and I got the time, I’ll upload the project on github and link you there.
Thanks and cheers!