How to align & rotate objects to each other at run time?

I’m working on my first Unity project, and it is a simple pipe-based puzzle game. The idea is that you pick up sections of pipe to build a pipeline. If you have a curved piece, the player can rotate it to make the pipe go left, right, up, or down. To accomplish this, I have two child objects of my pipe that act as simple colliders - one for each end.

This snapping and rotation is the part I’m having trouble with. If the two object’s natural origin aligns, then snapping works fine. If the origins do NOT align, however, things go crazy and the pipe will snap backwards, or inside the previous pipe, or sometimes even vertically.

Here is the code I’m using for snapping:

void OnTriggerEnter(Collider other)
{
	// NOTE: rb is the transform.parent's RigidBody component, set in Start() (not shown)
	rb.isKinematic = true;


	// This is doesn't work.
	//parent.transform.position = other.transform.parent.position;
	//parent.transform.rotation = other.transform.parent.rotation;


	// Figure out our rotation offset.
// TODO: What if we want to rotate our pipe UP/LEFT/RIGHT/DOWN?
	Vector3 rotvector = other.transform.eulerAngles;

	// I think I need to do something with these, but can't figure it out
	//float direction = Mathf.Sign(transform.forward.z);
	//float pitch = Mathf.Sign(transform.forward.y);
	//float spin = Mathf.Sign(transform.forward.x);
//Debug.Log("Direction: " + direction + " | Spin: " + spin);
//Debug.Log("Forward: " + transform.forward);
	//Quaternion rotoffset = Quaternion.Euler(rotvector.x * direction, rotvector.y * pitch, rotvector.z * spin);

	Quaternion rotoffset = Quaternion.Euler(rotvector.x, rotvector.y, rotvector.z);
	parent.transform.rotation = rotoffset;

	// To figure out how much we need to move ourselves, we calculate the offset between our two snap points!
	// Always do this *after* our rotation, because otherwise our endpoints may move!
	Vector3 offset = transform.position - other.transform.position;
	parent.transform.position -= offset;
}

I’ve tried several different combinations of using forward vectors and other things, but I just can’t seem to figure this one out.

If you connect them AND their natural origins happen to line up, everything is fine:
92029-snap-good.png

In other cases, the snapping goes bad:

Can anyone point me in the right direction as to how I am handling this wrong?

I ended up figuring out what I did wrong and fixing it! Mostly, I just threw away everything I had before and used a different approach. Instead of taking the player’s current rotation and trying to make it fit, I just don’t snap it unless it is already valid; it is up to the player to make any fine adjustments. (I do still snap to within 45 degrees, which feels about right and gives a healthy margin of error).

Here’s the new and improved code:

	void OnTriggerEnter(Collider other)
	{
		// Snap our rotation to SNAP_AMOUNT degree increments
		Vector3 snapVector = Snap(parent.transform.rotation.eulerAngles, SNAP_AMOUNT);
		Quaternion snapRotate = Quaternion.Euler(snapVector);
		parent.transform.rotation = snapRotate;

		// To figure out how much we need to move ourselves, we calculate the offset between our two snap points!
		// Always do this *after* our rotation, because otherwise our endpoints may move!
		Vector3 offset = transform.position - other.transform.position;
		parent.transform.position -= offset;

		// If we are okay to make our snap permanent, highlight it in green and SNAP IT!
		Vector3 validSnapVector = Snap(parent.transform.rotation.eulerAngles, 90.0f);
		if (snapVector == validSnapVector)
		{
			Debug.Log("TODO: GREEN");
			pc.isSnapped = true;
		}
		else
		{
			Debug.Log("TODO: RED");
			return;
		}

	// More game specific logic here...
	}

	float Snap(float angle, float snapAmount)
	{
		return Mathf.Round(angle / snapAmount) * snapAmount;
	}

	Vector3 Snap(Vector3 vector, float snapAmount)
	{
		vector.x = Snap(vector.x, snapAmount);
		vector.y = Snap(vector.y, snapAmount);
		vector.z = Snap(vector.z, snapAmount);

		return vector;
	}