How do I rotate an object to align to a specific point?

I am trying to procedurally place dungeon “modules” that have been created before runtime. I wrote a small Editor utility to add connection points and their facing directions (in local space).

alt text

Ideally, I would like to pick one of these points at random, and rotate the current module to face a point on the previous module. Most of the logic works pretty well.

alt text

But when I try to rotate one object towards another, the rotation is always wrong. I have tried a few ways, most notably:

Vector3 direction = (currentConnection.WorldSpace(currentModule.transform) - lastConnection.WorldSpace(lastModule.transform)).normalized; 
currentModule.transform.rotation = Quaternion.LookRotation(direction);

and:

float angle = Vector3.Angle(currentConnection.WorldDirection(currentModule.transform), lastConnection.WorldDirection(lastModule.transform));
currentModule.transform.transform.eulerAngles = new Vector3(0,angle,0);

The last method seemed to work the best, but in some cases, the rotation is the opposite of the one I need. I have tried simply inverting the angle, but that causes the rotation to be incorrect if I am placing pieces in the opposite direction. What am I doing wrong?

alt text

You could use the properties transform.forward and transform.right. If you want to align the forward direction of moduleA to the right side of moduleB, for instance, do the following:

moduleA.transform.forward = -moduleB.transform.right;

transform.left and transform.back don’t exist, thus you must think a little (not so little) in order to get all possible alignments:

Align front A to left B: A.transform.forward = B.transform.right;
Align left A to left B: A.transform.right = -B.transform.right;
Align back A to back B: A.transform.forward = -B.transform.forward;
Align back A to front B: A.transform.forward = B.transform.forward;

and so on.

I’m not going to lie I could have read this WAY more thoroughly. However it seems to me you’re after vertex snapping (although based on what was said first I could be way off) which is described towards the bottom of the page I linked below :slight_smile: