- Home /
How to smoothly switch from script controlled rotation to animator controlled rotation (and the other way around)
I'm rotating the head of my animated character by script using Animator.SetBoneLocalRotation.
animator.SetBoneLocalRotation(HumanBodyBones.Head, newHeadRot);
Everything works as expected but there's one problem I really have no idea how to solve: when I stop setting head local rotation by script, the animator takes back control of the head, changing the head local rotation abruptly. (which is expected but NOT desired)
my question is: How can I smoothly switch from my custom rotation to the animator one?
video reference: https://www.youtube.com/watch?v=VLf2VmUxqag&feature=youtu.be
0:08 --> right after the charachter starts running: the script slowly changes the assigned rotation to Quaternion.Identity
and when it reaches that state, it stops to set head local rotation to a new value
0:10 --> right after the character stops: the script (re)start assigning new rotations to the head local rotation
note: the problem occurs when the control of head rotation switches from animator to script (and viceversa)
You can disable the Animator component, or set a bool for the Animator. When it's false it will not animate the head, and when it's true, it will animate the head. $$anonymous$$ake sure to set the state with the correct parameters.
Then use Animator.SetBool method to change the value of the bool via script.
@galactichyperstar Thanks for answering me! I tried your suggestions but: - Stopping the animator causes the animation to stop entirely - Stopping only the head ( I suppose you mean by changing the avatar in the referred layer ?) is not solving the problem since animator should keep animating the head ins$$anonymous$$d of ignoring it.
It's not about (virtually) take/release control of animations played by the animator but rather about interpolating between the custom local rotation achieved by script and the local rotation consequent to the animation.
I'm gonna edit the post to include a ref video for the case.
Thanks again for your feed!
Answer by highpockets · May 05, 2019 at 10:11 PM
I would assume that you are doing this in LateUpdate(). That is where I do it. To smoothly move between the 2 I use masks for some things and gradually set the weight back to 0.0f when I want to give control back to the base layer or another layer. If you can’t or don’t want to use masks for whatever reason, you will have to do the same, but with slerp for example. When I’m using my custom rotation, I save my lastRot every frame and and slerp to my target and when I want to give control back to the animator, I still save the lastRot, but my target rotation is the actually rotation before masking/customRotation is applied. So it looks something like this:
float timer = 0.0f;
LateUpdate()
{
targetRot = transform.rotation;
transform.rotation = Quaternion.Slerp(lastRot, targetRot, timer);
timer = timer + Time.deltaTime;
lastRot = transform.rotation;
}
Hope that helps
Thanks for your help!
I'm changing the local rotation by using SetBoneLocalRotation
link which can be called only inside OnAnimatorI$$anonymous$$(int layer). Unfortunately, I didn't manage to make the rotation change to be affected by weight on the target layer: after the new rotation is applied with the above method, changing the weight of the target layer has no effect on the actual rotation and the new rotation is still affecting the player. (I need to check the docs again more carefully but I guess it has something to do with how the I$$anonymous$$ pass works). So I can't currently make any use of masks now. :-(
I used the LateUpdate approach before for other I$$anonymous$$ stuff and it actually worked just fine. Now that you pointed that out, I tried moving the update of lastRot
(which previously was on the call of OnAnimatorI$$anonymous$$(lastLayer)
) into the LateUpdate method (like you have in your example) while keeping everything as it was before in OnAnimIk call... and for some reason that I honestly ignore it worked! :-)
Now everything works smoothly and flawlessly. THAN$$anonymous$$ YOU SO $$anonymous$$UCH!
Your answer
![](https://koobas.hobune.stream/wayback/20220612200758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
IK for hands 0 Answers
Flip over an object (smooth transition) 3 Answers
How to Aim Animator.SetIKRotation() to direction WRT hand bone rotation offset 0 Answers
ik target isn't being reset 0 Answers
Any idea why my animation isn't working? 0 Answers