How to snap certain parts of objects to each other in certain ways?

I want to create a build menu for one of my games. In this menu, I want the player to be able to take objects, and snap them together to make things, kind of like Lego, with a few differences. I want to be able to snap certain parts of objects to certain parts of other objects, such as the inside of a cog along an axle, but not the side of the cog to a wall. Basically, I want to have parts objects only snap to specific parts of certain other objects, and have multiple parts that snap to different things. I found a site: How to Snap objects in-game in Unity that tells how to snap objects together in game, which would work for most of it, however, I would like to snap to faces, and also have each face be able to snap to different things. If anyone has any ideas for how I could make this work, it would be greatly appreciated.

You somehow need to define which faces an object has and the rules that define which pairs of faces can snap together.

One approach could be to add “snap point” transforms to each object. The transform can have a script where you can set which other “snap points” these can connect to. When snapping two objects together, you can check which points of the objects can go together and then align the chosen point’s positions and rotations.

While snapping objects together sounds conceptually simple, be warned that it’s not trivial at all. Managing the positions and rotations, rules and overlaps of objects is tricky and can get very complex quickly. Depending on your skill level, I’d suggest simplifying the design. E.g. only snapping two given objects in one single way, which allows you to easily pre-define their relative positions and rotations.

2 Likes

That makes a lot of sense. I’ll let you know if I need more help! :slight_smile:

I also need this, if you have a solution of some kind figured out it would be a big help, as I also need something like this. Any help or pointers would be helpful.

This two-year-old thread has zero details and zero solutions. I suggest you begin with any one of the thousands of Unity “connect two objects” together tutorials.

The answer will be completely different in 2D, 3D, VR, whatever your game happens to be.

Once you can connect two objects in your actual game context, then you can begin to reason about ways you can specify “hey, these two specific parts of my objects must be aligned before the objects can actually join.”

“aligned” in this case might mean “be close enough” or “be aligned in the same direction enough,” or even perhaps “gotta be close and gotta be aligned.” Remember with floating point values, nothing will ever be EXACTLY aligned, so your task as a game designer is to decide what “I can be connected” heuristic to use.

Again, all of this will be different based on your game.

Most jigsaw puzzle tutorials will actually have a lot of what you need.

Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

1 Like

Most solutions do rely on one transform or more on the objects that should go together.

If you only want it to snap to a single point than one transform on each object is enough. If you want something more complex like a rail on a firearm you will need more transform to define the ruleset.

We use transforms in our game to snap attachments to firearms. Using transforms to define the boundaries and then a cofigurable joint to execute the action. Finally we parent the attachment to the firearm.

Hope that can give you some ideas, good luck!

1 Like