Search Unity

Question How to animate a non-kinematic Rigidbody

Discussion in 'Animation' started by Fressbrett, Dec 14, 2022.

  1. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    97
    Hello,
    I am wondering what the best way is to animate a Rigidbody that is not set to kinematic.

    Use case:
    My character is animated through Animations. When my character performs attacks, I want to activate and move certain Rigidbody colliders during these attacks. These can't be kinematic, since I want to respect the masses of objects that I push (i.e. push some objects further than others), which seems to not be happening when I push something with a kinematic rigidbody, since they are considered immovable objects with infinite mass.

    What I want:
    An efficient and intuitive way to animate non-kinematic Rigidbodies, without breaking physics and/or collisions (i.e. not moving them through transform.position).

    What I am currently doing:
    I got kinematic Rigidbodies sitting on the bones of my animated character that get aniamted with it. I then have non-kinematic Rigidbodies with Colliders outside of my animated character, which are tied to the kinematic Rigidbodies on my bones through FixedJoints.

    Drawbacks:
    • They seem to interact with other colliders poorly - collisions are not always detected. This is probably because the fixed joint "lags behind" in this setup
    • They are always where my bone is

    What I will try next:
    Using custom tweens (DOTween or custom Coroutines) to enable and animate my non-kinematic Rigidbodies whenever the attack starts.

    Drawbacks:
    • Timing the character animation and the Rigidbody tween is pretty tedious, since you can't really "overlay" and preview both things at once without writing a custom Editor Window.
    Upsides:
    • I maintain my rigidbody velocities and get accurate physics interaction, since my tweens can use Rigidbody.MovePosition(), which respects the physics calculations
    • My attack colliders don't have to always be where the bones are, I can animate them separately.


    What is in your opinion the best approach? Am I missing something, does this work with default Unity Animations somehow already? Is there a nice asset that does this for me? How did you solve this (this can't be a problem only I am having)?

    Thanks for any help on this,
    Fressbrett
     
    Last edited: Dec 14, 2022
  2. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    127
    If it's a 3D Model, then this is my way to setup an (to be) attack rigidbody for a character:

    since rigidbody can have multiple child colliders, I just put collider objects on the bones(without rigidbody), and only 1 rigidbody on the root of the model.
    I don't want them to move, so freeze positions and rotations, and gravity off. Also, I would like the animator take care of the collider moving and collision resolving, so animator's update mode is Animate Physics.
    My_Way_To_Setup_Character_Rigidbody_For_Attack.png

    There are some cool tools in asset store to auto setup colliders for a 3d model (both humanoid or generic).
    I hope this match your needs.

    edit: for better collision detection, maybe "continuous speculative" is better. (has some side effects)
     
    Last edited: Dec 14, 2022
    Fressbrett likes this.
  3. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    97
    Thanks for the answer, I didn't think about only animating the colliders, this has some big benefits!

    The cool thing with your solution is that one could animate the colliders separately from the character during each attack, using additive synced layers within the Animator with Animations that only move the Colliders.

    Although this setup won't allow me to have different masses for each collider, I reckon I could change the mass of the main Rigidbody at the start of each attack to some desired mass. The only limitation would be that all Colliders would "punch" with the same mass during an attack, but in my case that should be fine!

    I will try this out tomorrow :)
     
    Last edited: Dec 14, 2022
  4. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    97
    Update: I tried it out, and it's working!

    By the way, I am currently experimenting with chatGPT from OpenAI, and this was the response it had to my question about this exact problem - absolutely insane:
     
    Yuchen_Chang likes this.