- Home /
Making an objects y Rotation match another objects Z
Hi! I've been trying to figure this out for a few days now with no real progress.
I have an object that rotates on the Y axis. It has a child that billboards at the camera, and that object has a child that I need to rotate on it's Z axis.
Main>BillBoard>RotationObject
I want the Z of this RotationObject child to match the Main parent's Y, but keep it's x and y axis at 0.
But I just can't seem to get it to work. When it comes to Quaternions, I tend to get confused rather quickly. I've been trying LookAt and Euler Angles stuff, but it always gives me the wrong results.
Any help would be greatly appreciated, as I'm starting to lose a ton of time to this small feature.
Heres the Billboard script the second object is using:
`void Update ()
{
Vector3 v = Camera.main.transform.position - transform.position;
v.y = v.z = 0.0f;
transform.LookAt(Camera.main.transform.position - v);
}
`
Answer by rutter · Jul 26, 2014 at 12:47 AM
If you're thinking in terms of rotation around the X, Y, and Z axes, that refers to euler angles.
Many people find it easier to use euler angles, but they're not quite the same thing as quaternions (which Unity uses internally). Luckily, Unity provides some code hooks for going back and forth between the two:
Each quaternion can give its eulerAngles property
Given some euler angles, you can get a quaternion using the Quaternion.Euler function.
Each GameObject has a transform component, which keeps track of its position, rotation, and scale, among other things.
So, we need to do three things:
Grab the target's rotation, in euler angles
Transpose the angles to use (0,0,y)
Assign new rotation to our object
Something like the following JavaScript would do what you're looking for:
var target : Transform; //set this in the inspector!
function Update() {
var euler = target.rotation.eulerAngles; //get target's rotation
var rot = Quaternion.Euler(0, 0, euler.y); //transpose values
transform.rotation = rot; //set my rotation
}
Thanks for the reply! This is a lot like one of my more successful earlier trials, but the problem I ran into, (and currently run into) with this is that even though we set the angles to (0,0,y) In the script they change to non-0 numbers in engine.
For example, the top level object is at -50 for the Euler Y.
The rotation object copies that -50 to it's Z just fine, but with this code the Y changes to 180 and -180 while the x varies between -50 and -65 for some odd reason.
Vector3 euler = thingToCheck.rotation.eulerAngles;
Quaternion rot = Quaternion.Euler(0, 0, euler.y);
transform.rotation = rot;
paused in engine the values of the thingToCheck object are : 0,-50,0
The values of the rotation object are: -65.18262,180,-50
(and in case it matters, the billboard object has a rotation of -65.3132,-130,0)
Figured it out. Was as simple as changing transform.rotation to transform.localRotation. Thanks a ton!
Answer by LtKelleyUSMC · Nov 13, 2017 at 02:56 AM
Just thought I would share this I have an Aircraft flying along (SampleAssets from Unity), and a RadarCamera 4000 units above the Aircraft. Thanks to your example, I have used your calculations, but, in order to have the camera change, according to the Aircrafts rotation, I have had to do the following:
void Update()
The logic here is: The RadarCamera (empty GameObject), with a Camera attached to it, is set up using the normal X, Y, and Z axis'. So, then the camera is rotated down 90 degrees, so now the Z axis is pointing down, and the Y axis is pointing forward, so by rotating the Z axis in the reverse, solves my Radar issue. Thanks so much for YOUR logic.!!! (Just thought I would add...) The EDITOR in this webpage, does not make it simple to add CODE...
{
var euler = Aircraft.transform.rotation.eulerAngles;
var rot = Quaternion.Euler( 90, 0, -(euler.y));
RadarCamera.transform.localRotation = rot;
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Transition between rotations 0 Answers
Distribute terrain in zones 3 Answers
Limit Rotation Issues 1 Answer
Why is my transform.rotatearound reset only working on positive Y? 0 Answers