- Home /
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));
Answer by robertbu · May 06, 2014 at 06:30 PM
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.
Answer by Jeff-Kesselman · May 06, 2014 at 04:41 PM
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
It is what I'm trying to do. I'm trying to build that quaternion that I'm gonna transform my vector with. I've tried a few things and it doesn't work, what I have right now is this :
Quaternion.FromToRotation(newPortal.rotation, OldPortal.rotation)
Your answer
![](https://koobas.hobune.stream/wayback/20220613144405im_/https://answers.unity.com/themes/thub/images/avi.jpg)