Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 saldavonschwartz · Nov 24, 2014 at 05:50 AM · animationrigidbodyanimator controller

How to move kinematic rigid body in addition to animator state.

I have the following setup:

  1. a kinematic rigidbody (which btw, doesn't have a collider nor mesh -- I'm only using it to establish a joint between it and a child rigid body / collider / mesh representing the visible game object in my scene).

  2. An animator component attached to this rigid body. The animator controller has a single default state, with an animation clip recorded via the animation window; it's a bobbing / sinusoidal motion along the Y axis.

  3. A "player input" script which, on FixedUpdate basically gets the vertical axis and does something along the lines of: transform.position = transform.forward * 5f * verticalAxisValue

So the idea is:

  1. The rigidbody is kinematic, so I can translate it via its transform.

  2. Again, the rigidbody is kinematic, so it should also be ok to animate it via curves.

But the problem is that, the minute I enable that animator, the bobbing animation plays and it seems to completely override my attempt at manually setting the transform via code (step 3 above). That is, if the animator is playing, any attempts to additionally further modify the transform in code are ignored, regardless of the animator's update mode or root motion option.

The one workaround I found so far is to enclose the above setup in an empty parent, and instead apply the programatic transform changes (step 3) to the empty, so that I translate that transform, while the rigid body animates according to the animator state within its local space. But I'm wondering if there is a way to do this without the extra empty?

I also tried creating 2 animator layers, leaving the base one with no states, while putting the bobbing state in the second one and giving it a weight of .5 and blend mode of additive. I was hopping this would "add" the animation to any programatic transform changes, but it didn't work.

Also, kind of related, I'd assume the animator clip is modifying the transform / rigid body in local space, relative to the empty parent, but if I rotate the parent, the animation seems to still apply to global Y, regardless of the rigid body having globally rotated as a consequence of it being dependent on its parent's coordinate space.

Comment
Add comment
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

2 Replies

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

Answer by hexagonius · Nov 26, 2014 at 05:00 PM

The problem you've gotten yourself into might be that you first recorded your animation and then corrected the setup. You most likely animated the "position" of your object first, not your "localposition". If you do that it should move relative to it's parent.

Comment
Add comment · Show 3 · 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 saldavonschwartz · Nov 27, 2014 at 12:03 AM 0
Share

Thanks for the tip! What you mention was indeed why If I'd rotate the rigid body, it would still animate along its global axis. But unfortunately, even when recording the animation over the rigid body as a child of an empty, if I then bring the rigid body out of the empty I can still not translate it :/. It seems like adding an animator to the rigidbody completely overrides any attempt to set its transform from code.

avatar image hexagonius · Nov 27, 2014 at 08:06 AM 0
Share

Well, if not childed to anything anymore it's childed to the world, is it? :) So it's animated relative to that and you can't move the world, can you? $$anonymous$$aybe you should make the empty your player by means of adding a rigidbody to it and keep your childed animation with the collider. There's no other way if you if you want to animation something and controller it's position. That has to be a parent and a child object

avatar image saldavonschwartz · Dec 01, 2014 at 04:11 AM 0
Share

I see your point. And your solution of parenting to an empty and ins$$anonymous$$d translating the empty will work. However, it also has the side effect of making the rigid body not be aware of any translation in world space, because you'd be moving the empty parent transform. Depending on the use case this might be fine, but I'm trying to also have the rigid body impart forces based on movement to a child of it via a a spring. Also, it'd be nice if this stuff was in the Unity documentation, namely: that animations completely override the transform as opposed to being additive. I found a similar question here: http://forum.unity3d.com/threads/playing-animation-local-to-position-in-unity.53832/.

avatar image
0

Answer by saldavonschwartz · Dec 01, 2014 at 04:20 AM

Based on what I read here: http://forum.unity3d.com/threads/playing-animation-local-to-position-in-unity.53832/ I'm adding another solution to the one proposed by hexagonius.

You can add your transformations to the values already set by the animator in LateUpdate.

That is:

      // rest of your class...
      void FixedUpdate() {
        this.PositionFromPlayerInput = <physics stuff, Time.deltaTime, etc...>
       }
 
       void LateUpdate() {
         this.transform.position = this.transform.rotation * this.transform.position;
         this.transform.position += this.PositionFromPlayerInput;
       }

      // rest of your class...
 

For the above to work though, you have to make sure that when you initially animate your body it's centered in world space (or an empty); otherwise you'd already have an implicit offset in your transform.

I'm going to go for this second approach because I'm also trying for the translation to the rigid body to affect a child rigid body of it which is attached thru a spring. But otherwise both solutions work.

Comment
Add comment · Show 1 · 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 tanoshimi · Dec 01, 2014 at 06:52 AM 0
Share

Was only going to add that this is similar to the solution I ended up with for a controller that blends scripted movement from an animator with reactionary movement from a (non-kinematic, in my case) rigidbody (essentially combining animation with ragdoll movement). LateUpdate() is also where Unity call SetLookAtPosition() to override the head position of a character in the $$anonymous$$ecanim tutorial, and you can see it called after the "Internal Animation Update" event in the flowchart at the bottom of http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html.

As you point point out, simply having the animator component enabled adjusts all affected bones, and the only way to get their "un-$$anonymous$$ecanimed" state is to cache and then restore it in LateUpdate(). I have a related problem that I'd like to be able to look ahead for what the $$anonymous$$ecanimed state will be at some future frame of the animation - if you encounter a solution to that on your travels, please let me know!

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

27 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

Related Questions

Quaternion issue: how can I fix this? 0 Answers

How to stop an animation on collision 0 Answers

What is the proper way to wait for an Animator Controller to update? 1 Answer

Animator Override Controller Layer Issue 1 Answer

Unity 2d Animation from spritesheet: How can I move the character according to the animation? 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