Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Using IK for two-handed items and animations.

Discussion in 'Animation' started by Kev00, Mar 5, 2018.

  1. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    My game has multiple player character types of various sizes and all characters share the same set of animations.

    At the moment, I've created attachment points as child game objects under the hands of each character rig.

    Each item model resides inside an empty game object that maintains its orientation and scale (ignores parent scaling via script). All items (guns, swords, axes, etc) are always the same size and don't scale with the size of the character.

    So far this solution is working very well and it allows me to instantiate an item at run time in the character's hands. I have managed to avoid maintaining a bunch of inactive objects (item models) that are pre-attached and pre-rotated on each character model, which saves me a ton of time.

    The problem I have now, is that the animations expect the item to be scaled. It's not a big deal for weapons that are one handed, but it doesn't work with two handed weapons like rifles all that well.

    Has anyone tried using IK to solve this problem? If so, would it be advisable to create hand locations (empty game objects)on the item model?

    Here are the screenshots, I know the medium sized rifle looks hilarious on the big guy (who is almost twice and large as the medium sized robot, but still...

    I suppose I could just prevent large sized creatures from using medium sized rifles. hmm..

    Let me know what you think.


    Thanks.

    LargeCreature.PNG MediumSized.PNG
     
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    @AndersMalmgren has described this exact solution - although it has been for a different problem. Might look back through his threads to see the solution they are using.
    From general knowledge - I think IK would only be needed for the left (support) hand to find the correct target on the weapons - not the right dominant hand holding the weapons.
     
  3. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    I solved this in Deftly by adding a "Character Scale" float and multiplying the IK position offsets by it.

    I suppose it depends on your implementation, but it worked perfectly in my case within reason. Obviously at some point it becomes impossible to accommodate giant characters and tiny weapons without scaling the weapons up.

    Either way, you need to establish a base size and assign that as Scale 1. Anything bigger or smaller would require a manually configured value to scale the offsets by. You might be able to automate it by the Collider Height.
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    We have solved this exact problem for our NPCs. We parent the weapon (keep its correct scale) to the main hand. THen with IK we move the off hand to the frontgrip of the weapon. Works pretty well for most sizes of rifles and SMGs
     
  5. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Thanks I'll certainly give IK a try.
     
  6. itchyOwl

    itchyOwl

    Joined:
    Feb 28, 2013
    Posts:
    25
    I've done similar things with IKs. Like @AndersMalmgren said, parent the weapon on the main hand and move the offhand with IK. The IK target position can be defined as an empty object in the weapon (or any object) prefab. Then set the target in OnAnimatorIK callback like this:
    Code (CSharp):
    1. Animator.SetIKPosition(ikGoal, targetPos);
    2. Animator.SetIKPositionWeight(ikGoal, targetWeight);
    3. if (rotate)
    4. {
    5.     Animator.SetBoneLocalRotation(GetBone(), Quaternion.Euler(Rotation));
    6. }
    You also need to smooth the transition so that it looks good, but that's another topic. Also remember to enable IK pass in the Animator component.

    This works, but if you need to do more complex things (like fencing), you might want to consider Final IK.
     
  7. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Or clone the hand bone and parent to weapon. Rotatate the fingerbones so they grip the frontgrip correctly. At runtime use this data to rotate the characters finger bones to the correct pose. This is how we do it

    edit: We did a rigging tool for it
     
    TeagansDad and itchyOwl like this.
  8. itchyOwl

    itchyOwl

    Joined:
    Feb 28, 2013
    Posts:
    25
    If you have to fix the fingers, then that's the way to do it. There's a tool for that in Final IK also.
     
    AndersMalmgren likes this.
  9. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Yeah we use Final IK for our player avatar, but for NPC we wanted as little CPU overhead as possible since we have many at the same time.

    Here its in action, old vid, though, couldnt find a better one. We have disabled animation for the hands using a avatar mask since that video. Which makes the fingers stay 1:1 sync with your hand rig on weapon. This is a VR game so it looks a bit funny, its hard to get a VR rig tro look one 100% perfect :D

     
  10. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    Anders, I love your game's graphics, they look very crisp and smooth.

    I've been pondering Final IK for a while now, but haven't decided if I'll need it or not.

    Actually, I'm already using IK for weapon aiming, climbing, foot placement. So far hand coding all the IK functionality hasn't been too difficult for me, but i'm sure there are all sorts of gotchas as I dive into it more that a system like Final IK will have solved.