- Home /
Bone rotation in LateUpdate goes back to normal every frame
Hi everyone! I'd like to rotate a bone of a character following the mouseX Input. But the bone slowly goes back to its original position every frame, I'd like it to stay where it was rotated.
The character has a NavMesh moving him in like a cinematic scene where we can move the head.
Here's the code
void Update()
{
rotX = Input.GetAxis("Mouse X") * sensitivity;
(...)
}
void LateUpdate()
{
//headRot is a float, head is the bone's transform
headRot = head.transform.rotation.y+rotX;
head.transform.localRotation = Quaternion.AngleAxis(headRot, new Vector3 (0,1,0));
}
It's not a solution to your problem, but when you want to rotate the head of your character, you could use the "LookAt" function of the Inverse $$anonymous$$inematics https://docs.unity3d.com/$$anonymous$$anual/Inverse$$anonymous$$inematics.html
Hey, thank you. I didn't set up the I$$anonymous$$ system because they warn me that I need a humanoid armature correctly set in $$anonymous$$ecanim. But my Armature is custom made by me, and when I played with I$$anonymous$$ I didn't get any result, I probably just don't know how to set it up correctly...
Answer by mrsev · Aug 25, 2017 at 11:54 AM
Ok got it! It's stupid as F.
Before :
void LateUpdate()
{
//headRot is a float, head is the bone's transform
headRot = head.transform.localEulerAngles.y + rotX;;
head.transform.localRotation = Quaternion.AngleAxis(headRot, new Vector3 (0,1,0));
}
headRot is here taking the rotation as it is after the Update() function, which is the reset value of the default animation! Which is F-ing zero... So I just put that line after the last one in LateUpdate, so it takes the actual modified value. And works perfectly.
void LateUpdate()
{
head.transform.localRotation = Quaternion.AngleAxis(headRot+rotX, new Vector3 (0,1,0));
headRot = head.transform.localEulerAngles.y;
}
Since I can't reward myself on the forum, I'll reward myself with a cookie. Cheers.
Answer by Destolos · Aug 23, 2017 at 06:24 AM
I guess, the problem is this code line headRot = head.transform.rotation.y+rotX;
You use transform.rotation, the result is a Quaternion, but you want the eulerAngles and so you have to write headRot = transform.localEulerAngles.y + rotX;
Thanks mate, it was apparently not the main problem, found it in the answer I post under. I kept the EulerAngles anyway cause it seems more correct.