Playable build – updated controls
Wow, it’s been a long time since my last update! I’ve been locked away working on the building mechanic for the last couple of months, but now I finally have something new to share. After the construction freezing stuff that I posted about before, the next step was to work on how the player positions, aligns and attaches new parts to a frozen construction. The implementation I had in place for this (dating right back to the beginning of the project) was clunky and difficult to use, and was in need of a major overhaul.
Over the past two months I have been prototyping many different ideas for alternative solutions. It has been a challenging and frankly frustrating experience, because every method I tried seemed to have its own pros and cons, and no single idea jumped out as the “correct” way to go. It’s not been an easy problem to solve, but I have now settled on what is hopefully the least worst solution! I think with further refinement it can be made to work quite nicely, certainly it’s already much better than what I had before.
Original method
The original implementation I had for alignment and attachment was done just using physics collider contacts. You had to position your selection, so that it touched the frozen construction roughly where you wanted it to attach, and it would snap in place based on that. The problem was that you had to be very precise with the positioning in 3D space, otherwise things might not contact where you wanted.
Rejects and epic fails
To try and help positioning the selection, I tried various ways of aligning it to a plane oriented relative to the frozen construction. This helped a bit, but it was clear that manoeuvring stuff in 3D space with high precision was still gonna be too fiddly. I decided that more of a screen-space approach was needed with smarter, more automated alignment.
Unity’s physics system can sweep a rigidbody through the world, determining which colliders it would contact. I tried using this to sweep test your selection away from you in the direction you’re looking, to see where it might contact a frozen construction. But because the test is done in world space, not the perspective space of your POV, it felt weird and I couldn’t really find a way to make it work.
So then I tried raycasting from your view position through every attachment point of your selection, to find intersections with a frozen construction, and use these results to determine where to position / align for attachment. This had potential to work well, but simply required way too many raycasts for reasonable CPU performance.
One last raycast
In the end for the final solution I decided to keep it simple, I now do a single raycast through the screen centre to find an attachment point on a frozen construction. The selection point (position of the cursor relative to the selected construction) is used to find an attachment point on the selected construction. These two attachment points are then used to snap the selection into alignment. The effect of this is that the point where alignment is happening is always right under your cursor at screen centre which is quite nice from a usability perspective.
This approach had a problem however – you could only align relative to a single point on your selection! Rather than having to drop it, and pick it up again at a different point, I wanted the player to be able to move their selection underneath the cursor. So now when aligning a selection to a frozen construction, by holding shift and using the directional keys (i.e. WASD), you can do exactly that.
Next steps
After all the prototyping, some of the code has ended up a bit of a mess, so first I need to clean that up. After that there’s a bunch of further work to the construction mechanic that I need to do:-
- Selection movement improvements – as I mentioned, you can now move a selection relative to the cursor using shift + WASD, but it’s still a bit clunky and needs some work.
- UI / visualization – now that the core mechanic is close to being done, I need to add some visual feedback during construction, as (unless you’ve memorized the controls ahead of time!) it’s hard to figure out how it all works.
- Part multi-select on a frozen construction, selected parts can then be picked up for re-positioning, cloning, or deleting.
- Ability to flip block to axle connections between rigid and hinged, post attachment.
- Various bugs and other minor issues.
Once I have some basic UI / visual feedback elements in place, I’ll put together a video to demonstrate the new building mechanic. In the meantime feel free to try it out in the build!