Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Animator IK Catch 22 - It is impossible to make your character hold a weapon correcty

Discussion in 'Animation' started by Zergling103, Mar 28, 2014.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    So I found myself in a bit of a sticky situation: I've determined that it is impossible to get your character to hold a weapon perfectly with the IK solution Unity provides. Either the gun will lag behind the character, or the hands will lag behind the gun:
    1. The hands move to where they need to be on the gun: Right hand to the pistol grip, and the left hand to the hand grip.
    2. The character moves via animation.
    3. The gun moves to where it needs to be on the character: Butt of the gun to the character's right shoulder.
    Of course, if you did things in this order, you'd find that the gun would be animated correctly, but the hands would drift around aimlessly unless you stood perfectly still.

    The other option might be the following:
    1. The gun moves to where it needs to be on the character.
    2. The hands move to where they need to be on the gun.
    3. The character moves via animation.
    But in this scenario, your hands may be correctly positioned on the gun, but the gun itself will wander around.

    Ideally, you'd want this scenario:

    1. The character moves via animation.
    2. The gun moves to where it needs to be on the character.
    3. The hands move to where they need to be on the gun.
    But unfortunately, this is not possible. Why?

    Unity's design internally dictates that you MUST set the goal positions of the IK immediately before the character is actually animated. Unity simply doesn't allow 3. to happen after 1., thus 2. cannot be done at the correct time.

    Thus the IK positions will always, necessarily, by design, lag 1 frame behind your character.

    If Unity animated the character BEFORE OnAnimatorIK() and then applied the IK AFTER, there wouldn't be a problem since the gun could be positioned correctly within OnAnimatorIK().

    It's a very simple problem that seems like a grievous oversight by the unity guys. It makes it necessary to use 3rd party IK solutions that work within LateUpdate.
     
    Last edited: Mar 28, 2014
  2. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,548
    OnAnimatorIK gets called before LateUpdate, but the character has still been already moved and animated for that frame.
    It must be the something fishy about the way you are positioning the gun. It should be done in OnAnimatorIK before you update the IKPosition, or just parent it to a bone or the root.

    Cheers,
    Pärtel