- Home /
Copy localEulerAngles.x from one object to another FAIL
I am trying to get the rotation from one object to another.
They both are childs of different groups, but oriented the same way...
Anyway, i want to copy the rotation only for X axis, but when i rotate the referenced object more than 90 degrees, the object, that should get the X rotation starts rotating back...
This script is applied to the object, that should get the rotation from the referenced Transform
public class TestRotation_CS : MonoBehaviour
{
public Transform getRotation;
Vector3 getVector;
void Start()
{
getVector = transform.localEulerAngles;
}
void Update()
{
getVector.x = getRotation.localEulerAngles.x; // Get the X rotation of the referenced object
transform.localEulerAngles = getVector; // Set the rotation back to this.transform
}
}
Weird, that this happens only if i copy the X axis. If i copy the Y or Z it doesn't make any problems and copy them correctly...
Have you tried getting some debug output? It is possible that the X coordinate is negative, but converting to positive on being set. You could always try this.
getVector = getRotation.localEulerAngles;
getVector.y = transform.localEulerAngles.y;
getVector.z = transform.localEulerAngles.z;
Answer by Kishotta · May 06, 2017 at 05:41 PM
You might be better off using Quaternions as that's what Unity actually uses to handle rotations. The XYZ angles are just displayed because most of us have trouble visualizing 4-D complex numbers. ;)
transform.rotation = Quaternion.Euler (target.localEulerAngles.x, transform.localEulerAngles.y, transform.localEulerAngles.z);
Thanks, but that doesn't work.
I want to take only the X axis and if i try to use that you are suggesting the object, that should take the rotation, is rotating only to 90 \ -90 and goes back. It doesn't spin like the target...
Not to mention, that you are applying localEulerAngle to rotation, which is global. Have you tested, your suggestion??
Also i think it's worth mentioning, that i will also need (sometimes) to take half of the X rotation, so if the target rotates 90 degrees, my transfrom should rotate 45.
I apologize, I had not tested that solution. I have tested this this though and it seems to be what you're after:
transform.rotation = Quaternion.Euler (target.rotation.eulerAngles.x, 0, 0); // $$anonymous$$ultiply target.rotation.eulerAngle.x by whatever scale factor you need
It only rotates this
transform on its (and the global) x axis to match the targets global x rotation.
The object, that i am rotating is the target. The other one is gettting the rotation via code like yours. Doesn't this happen in your scene??
It rotates from -90 to 90, like it's clamped in these values...
Answer by gaiastellar · Jun 03, 2019 at 11:20 PM
Hi. Did you ever get to the bottom of this. I'm having exactly the same trouble.
Paul