- Home /
How to set a single-axis rotation and not change the others axes
First off, I've spent the last three days researching how to solve this issue. I've looked at this video on quaternions, dozens of posts on setting quaternions with Euler angles (and not), avoiding gimbal lock, and how they're problematic when used to alter one axis and maintain the same rotation on the other axes (or something like that).
Here's what I'm trying to do:.
2) With that calculation handy, I get (what should be) the new x-axis angle.1) I need to get the z-axis angle of my object and do some calculations with it. It shouldn't change unless specified by the object's code or done so manually.
4) I set the rotation of my object via Quaternion.Euler(NewXAngle, PrevYAngle, PrevZAngle)3) I get the object's localRotation.eulerAngles, and place them in PrevYAngle and PrevZAngle.
The next time around, somehow the Y and Z axes have changed. This throws off the calculation. Needless to say, that's a problem. One thing I should point out: The Y and Z axes seem to be proportionate. When I change the values with the calculation (based on the position of another object, and a few other things), they stay the same value at the same position. I.e, if I move the object, the values change. But if I move it back, the values go back to how they were when the object was at that position..
.
Here are the basics in code of what I'm trying to do:
Vector3 oldAngles = child.localEulerAngles;
float angle = child.eulerAngles.z;
// in the case of my test, (child) is parented by an object who's Euler rotation is (0, 0, 20). The child isn't rotated separately atm, so it should be (20).
// do the calculation. Use it, among some trigonometry and other things, to get a proportion of -1 to 1 and set to myProportion.
float XAngleProp = Mathf.Abs(myProportion - 1);
// absolute - 1 to get the value between 0 and 2.
float rotationAngle = Mathf.floor(90 * XAngleProp);
Quaternion rot = Quaternion.Euler(rotationAngle, oldAngles.y, oldAngles.z);
child.localRotation = rot;
The child rotates correctly (as in it doesn't spasm), but my calculation is thrown off by the changing z-axis value. I also don't quite understand quite how the z-axis value is changing, but the child does not appear to actually be rotating on the z-axis at all. I have an idea that it may be changing in relation to the other axes, but that seems like it wouldn't be intuitive.
If someone can provide me a way to change the x-axis of my child object while maintaining the set value of its y and z axes (or explain to my why this is happening and how I can achieve my goal), I'd be very much appreciative..
(Holy crap. I had to edit this thing for 10mins to get it formatted properly.).
Answer by GeekOverdrive · Mar 05, 2019 at 09:10 AM
Added child's local eulerAngle.z to the parent's local eulerAngle.z. Works perfectly.
// Keep this in mind: If you need to get a child's global euler angle (where it's facing compared to the world, and not the parent), use parent.transform.eulerAngles.z + child.localEulerAngles.z.
You'd think child.eulerAngles.z would be the same thing, but apparently it is not.
I spent three days to come to this conclusion. Reap the rewards of my trial and error.
Hello! Can you detail more your answer? I have same problem, and I can´t understand this solution. Thanks
@xocoalt Hey!
I looked through my old code and found this note:
// Keep this in $$anonymous$$d: If you need to get a child's global euler angle (where it's facing compared to the world, and not the parent),
// use parent.transform.eulerAngles.z + child.localEulerAngles.z. You'd think child.eulerAngles.z would be the same thing, but apparently it is not.
// I spent three days to come to this conclusion. Reap the rewards of my trial and error.
So essentially, ins$$anonymous$$d of this:
float angle = child.eulerAngles.z;
Do this:
float angle = parent.transform.eulerAngles.z + child.localEulerAngles.z
Hope this helps.
Your answer
Follow this Question
Related Questions
Rotating a model with increments 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotation jumping to a different rotation after rotating a certain amount 0 Answers
Use a single rotation axis of a freely rotating object 1 Answer
Why is my object's local x rotation not going past 90 degrees? 1 Answer