Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by earhod · May 05, 2019 at 09:25 PM · rotationanimator controllerinterpolationinverse kinematicroot bone

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)


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galactichyperstar · May 05, 2019 at 09:32 PM 0
Share

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.

alt text

Then use Animator.SetBool method to change the value of the bool via script.

avatar image earhod galactichyperstar · May 05, 2019 at 10:06 PM 0
Share

@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!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image earhod · May 06, 2019 at 01:16 AM 0
Share

Thanks for your help!

I'm changing the local rotation by using SetBoneLocalRotationlink 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!

avatar image highpockets earhod · May 06, 2019 at 07:24 AM 0
Share

Glad that helped! Cheers

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges