How to align rotation and position of blocks

2D Representation of what I’m trying to solve

Screen shot of the objects, the white points are the test join spots.

I’m trying to build a system where blocks (think toy blocks with specific join points) can be connected together as part of the game.
I’ve got a system where the blocks can be joined together in the GameObject Hierarchy.

But I’ve got stuck with aligning the rotation of the objects. This is being done in space and as such I can’t rely on the world space up aligning with the objects local space up.

I’m looking for a solution where the objects, after colliding, will snap in to an aligned rotation.

*Edit - I’ve uploaded the picture above to help describe what I am trying to do. The diagram is 2d but I need to work out who to do this in 3D.

Any help or ideas appreciated.

1 Answer

1

Use a collider event and look at the normal of the collider. That vector is perpendicular to the surface you hit. Depending on your shape, use quaternions to choose an appropriate orientation. For example, if you have a square and it’s aligned regularly, you can use Quaternion.LookRotation to the normal of your collider.

These are just rough ideas and won’t, for example, solve the required offset to get a flush, face-to-face alignment. Thought this might help get you started, though.

Thanks. I'm using a collider event but I don't quite understand how the normal of the collider helps because I can't predict the orientation of the other object - or if it's aligned regularly. I think I need to find a way to get the smallest amount of rotation for the two objects to be flush and then apply that rotation to the parent object?

–

I just ran into a good Q&A with examples of some math to solve a similar problem: [walking on walls][1] [1]: http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html Maybe you can extrapolate something from this that works for your scenario.

–