Finding the rotation between two gameObjects

I’m working on a a similar mechanic to portals and I’m having some issues when the portals are not in the same angle. Basicly, once an object enters through the first portal, a copy is created in the second portal giving the illusion that is passes through. I’m having some issues when the portals have a different rotation. Here’s a quick picture of the issue.

Clearly The Quaternion i use to make the rotation in the mouvement is wrong, the Quaternion i use right now is this :

Quaternion portalRotation =
 Quaternion.FromToRotation(LinkedPortal.transform.parent.rotation.eulerAngles,
 transform.parent.rotation.eulerAngles);

This code is on the portal itself and the LinkedPortal is the other portal that the objects are coming out of.

Here’s the full code of the translation for anyone interested, though I’m pretty sure the issue is up there:

		Quaternion portalRotation = Quaternion.FromToRotation(LinkedPortal.transform.parent.rotation.eulerAngles, transform.parent.rotation.eulerAngles); // Rotation between portals
		Quaternion normalRotation = Quaternion.Euler(0,180,0); // Default Rotation of 180 degrees 

		//Update Position
		Vector3 pos = transform.position - player.transform.position; 
		pos = portalRotation * pos;
		pos = normalRotation * pos;
		pos = Vector3.Reflect(pos, (transform.up)); // Reflection to put clone on ground
		pos += LinkedPortal.transform.position;

		duplicate.transform.position = pos; //Assign Position
		duplicate.transform.localEulerAngles = rot; //Assign Rotation
		Debug.Log ("Quaternion:" + portalRotation);
		Debug.Log ("result:" + portalRotation * new Vector3 (0,0,1));

With questions like these, theirs never enough information because objects can be built in a variety of ways. Let’s assume that this is 3D with the portals on the XZ plane, and that the blue side of each portal is the front side of the object (though my solution should work even if it is not). So the rotation should be:

Quaternion q = Quaternion.FromToRotation(-LinkedPortal.transform.parent.forward, transform.parent.forward); 

You need to apply this rotation to your character’s rotation (not assign it):

character.transform.rotation = q * character.transform.rotation;

Note this assignment preserves the entry angle. That is, if the player enters at an angle, they will come out the new portal at the same angle.

The simplest way to do this would be to express the angle as a vector local to the old portal’s local space, and then transform that vector by the new portal’s transform matrix to get the new direction vector in world space