- Home /
How do you set a local rotation for 1 axis but keep the other axis world space untouched?,How do you set the local rotation of 1 axis without effecting the world rotation of others?
I have an inverse kinematics rig with the animation rigging package and I need the forearm to rotate on the y axis (along the arm) locally. However, Unity doesn't seem to have any functions for only setting the local axis. They have Transform.Rotate which is close, that rotates around the local axis according to a CHANGE but I need to SET it.
Another similar function I saw was Quaternion.AngleAxis but that doesn't seem to work either since it wants to completely reset my rotation and my arm points straight up, removing the work of the inverse kinematics.
Perhaps it's my lack of knowledge in Quaternions, if anyone has an answer that would be great!
Thank you,I have an inverse kinematics setup for my VR rig and I need to keep the rotations for my forearm all the same besides the local Y axis (down the arm) to make it rotate depending on how your hand is facing.
There's not very many functions available for Quaternions that I could see for this and I can't use something like Transform.Rotate since this would result in it being a change in value compared to setting the value.
Really lost on what to do now so if anyone could help me with the math that would be great. Maybe I'm just not understanding Quaternions since they are rather complicated.
Have you tried just a simple
transform.localRotation = Quaternion.Euler(0, yAngle, 0);
@unity_ek98vnTRplGj8Q wouldn't that just zero out my X and Z values though?
Yes you're right, my bad, but you can use Quaternion.Euler to apply an additive rotation to your default rotation every frame.
What I would do is set up your arm to its default rotation and capture that rotation on start up. Then you can use a Quaternion.Eulers to create an additional rotation about the arm's local y axis, and then apply that on top of the default rotation every frame.
Quaternion defaultLocalRotation;
void Start(){
defaultLocalRotation = transform.localRotation;
}
void Update(){
Quaternion yRotation = Quaternion.Euler(0, yRotationAngle, 0);
localRotation = defaultLocalRotation * yRotation;
}
@unity_ek98vnTRplGj8Q Thanks this worked! However, I still am having some troubles with my inverse kinematics because of the constraints it has. Looking like I'm gonna have to write it all myself manually (rotations). Having a hard time wrapping my head around quaternions especially converting between eulers, degrees, and some other things like localrotation vs rotation (not just saying it's local vs world). Do you know of any tutorials or articles I can look into?
Thanks!
Unfortunately I don't have any tutorials off the top of my head, but there are tons of questions on this forum that have pretty good explanations of quaternions and rotations
Answer by logicandchaos · Mar 17, 2021 at 12:58 PM
You simply plug the original rotational values back into it like this:
transform.localRotation = Quaternion.Euler(transform.localRotation.x, yAngle, transform.localRotation.z);
Your answer
Follow this Question
Related Questions
transform.localRotation resets after rotation 1 Answer
How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer
Write a C# script to record GameObject rotation? 4 Answers
NullReferenceException by localRotation And Quaternion.Euler!?? 1 Answer
Use a single rotation axis of a freely rotating object 1 Answer