- Home /
How to align an object's rotation to another GameObject's rotation (without parenting the Objects)
If this question has been answered before, please let me know, as I do not know how to properly word this problem. But I will give it a try.
What I want to do is to align the rotation of a gameobject by reading in the rotation data of another gameobject. The reason for this is to make a mini-map type of camera that has a bird's eye view of the player's position, but parenting would make the camera display very awkward (especially with an orthographic view) due to the player also leaning forward/backward and left/right.
So I settled for an idea of a rotation solution where I would need to set the object's rotation in a way to rotate with the player, but I ran into problems when trying different rotation and quaternion functions. Here are the things I have tried so far:
//myTransform.rotation = new Quaternion(transformToAlignTo.rotation.x, myTransform.rotation.y, myTransform.rotation.z, myTransform.rotation.w);
//myTransform.localEulerAngles = new Vector3(transformToAlignTo.rotation.x, myTransform.localEulerAngles.y, myTransform.localEulerAngles.z);
//myTransform.rotation = Quaternion.FromToRotation(Vector3.up * debugSpeed, transform.forward * debugSpeed);
//myTransform.rotation = Quaternion.FromToRotation(Vector3.up * debugSpeed, transform.forward * debugSpeed);
//myTransform.Rotate(Vector3.right, debugSpeed * Time.deltaTime);
//myTransform.Rotate(Vector3.right, transformToAlignTo.rotation.x);
//myTransform.Rotate(0, transformToAlignTo.rotation.y, 0);
//myTransform.rotation = Quaternion.LookRotation(transformToAlignTo.position);
//myTransform.rotation = Quaternion.LookRotation(new Vector3(0, 1, 0));
//Vector3 v = myTransform.position - Vector3.up;
//myTransform.rotation = Quaternion.FromToRotation(transform.up, v);
//myTransform.LookAt(transformToAlignTo.position, Vector3.forward);
So my question is, how can I make the bird's eye view mini-map camera object become aligned with the player object rotation? (As in, When a player turns left or right, the mini-map camera also rotates with up being always the player's forward, if that makes any sense)
Any and all help is appreciated.
Answer by IgorAherne · Jun 13, 2017 at 01:56 AM
Neither of these has solved this issue unfortunately... and I will explain my reason. The first one makes sense as I tried it, but it gets a little wonky when it tries to do it's calculations and makes this camera go two places as it tries to realign (making this ghosting as if it were two cameras). It could be my lack of knowledge when it comes down to vector calculations, but it did not work for me. As for the second one (with the Transform.LookAt), it failed due to one thing, it did not rotate with the player rotation, but it did do its job at keeping it facing downward at the player. It is just sad that it locks in that rotation and wont rotate while looking at the player as the player rotates.