- Home /
Aligning two game objects position/rotation
I have two empty game objects (with different parents) at different positions and with different rotations. I want to align them such that the second game object is placed in the same position as the first game object and with its z axis (i.e. the editor blue arrow) facing in the complete opposite direction to the first game objects z axis. The second game objects original z rotation should, however, be retained.
I tried doing this by:
Storing the second game objects local z rotation
Setting the second game objects position/rotation to the same as the first game object
Rotating the second game objects 180 degrees on the x axis so the "blue arrows" of the objects are facing in opposing directions
Finally I set the second game objects local z rotation back to it's previous z rotation.
The problem is this doesn't work as i'd expect, the rotations are often wrong. The only reason I can think of is because of the way euler angles work where you can have several different euler values for one quaternion (so resetting the second game objects local euler to its previous value won't work).
Does anyone know how I might solve this problem with quaternions?
Answer by vvolfster · Nov 10, 2016 at 08:13 AM
I did something very similar to this. I know this is a year late but I just started Unity and maybe this will be helpful to someone else. I was aligning my two objects on their blue axis in local mode. You can use the forward vector for this. If there's some other axis you want to align on, use up or right vectors of the transform.
Quaternion.FromToRotation (transformA.forward, transformB.forward * -1);
This will give you how much A needs to rotate to align itself with B. You also need to set their transform.positions to be the same afterwards.
Answer by GeorgesAbitbol · Apr 11, 2014 at 12:03 AM
Did you try those ?
Transform.LookAt (using reversed object 1's transform.forward + transform.position)
Unfortunately I have no problem setting the second objects rotation to the first objects rotation, my real problem is setting the z rotation back to it's previous value :)
If you use rota$$anonymous$$round the Y axis, you should preserve the Z rotation.
I dont really understand what are the constraints in your project, so im not sure what the solution should be. Could you explain what you are trying to do with an explicit example ? It sounds like your are trying to plug two objects together, is it like power plugs ?
So the problem with "RotateAround" is what angle do I put into it? The y and z axis values of my objects sometimes "flip" because of how Euler angles work so I couldn't simply work out the difference between object 1 and 2s y rotation for example.
As for what I'm doing, I have two planes, each with the blue arrow facing perpendicular (or up) to the plane. I want to align the planes such that they are flat against one another whilst preserving their individual rotations about the blue arrow. They could be placed at any location, so long as they are aligned in this way.
Thanks for your continued support, I've been trying to get to grips with this for days!
Answer by Vylax · Nov 10, 2016 at 11:06 AM
Try to use this:
var pos2Keep : float = 10; //the position (x or y or z) that u wanna keep for the object u're going to align
var obj1 : Transform; //object u're aligning the other one to
var obj2 : Transform; //object u align
function start () {
pos2Keep = obj2.position.x; //or y or z
obj2.position = Vector3(pos2Keep, obj1.postion.y, obj1.position.z);
obj2.rotation = Vector3(obj1.rotation.x, obj1.rotation.y, obj1.rotation.z);
}