How to put gun in enemy AI hands and adapt it to animation ?

Hello everyone !!!

So, here’s my problem : i’m actualy coding an AI enemy in a FPS game like, but i confront to animating him with a gun in hands, and after reading post and post, i’ve been aware there’s multiple solutions and the one i’ve finally found on my own for the moment is to put the gun in AI hands and change the transform of his weapons depending of the animations played, (here is an exemple) :

static private void AnimWalk(Transform M4A8) // Permet de faire tourner l'animation Walk et de regler l'arme avec l'animation
            anim.SetBool("IsAttacking", false); // ANIMATION Arrete d'attaquer
            anim.SetBool("IsIdle", false); // ANIMATION : arrete de rien faire
            anim.SetBool("IsWalking", true); // ANIMATION : commence a marcher
            Vector3 M4A8position = new Vector3(-0.079f, 1.079f, 0.254f);
            M4A8.transform.localPosition = M4A8position;
            M4A8.transform.localRotation = Quaternion.Euler(-31.912f, 110.747f, -9.157f);

But i think you will agree,i think this solutions take too much ressources for what it is and set not the gun perfectly with the animations

I know there is the IK (Inverse Kinematic) solution but i’m starting in Unity and everything i’ve seen about it let me know that it’s hard to handle IK (unity’s doc say it’s for professional use : and i’ve try to search tutorials about it but nothing really efficient to solve my problem in my case

Maybe there is another solution i don’t know but i’m currently searching

in fact i’m turning to you, asking you if you can help me to solve this problem

Thanks for reading :wink:

I’m going to interpret your question as “how can I make a character hold a gun with both hands?”

This IS a tricky technical problem, and you will need to learn the IK systems.

To start with, you really need a “walk-holding-gun” animation so your starting point is as accurate as possible. Then parent the gun to the trigger hand so at least that one matches. This should leave you with hopefully a small correction to make to the supporting arm.

You’ll need to set up an IK chain from the shoulder to the hand on the supporting arm, and parent a dummy GameObject to the gun to indicate where the supporting hand SHOULD grab it. Then you tell the IK chain to place the hand at that position/orientation, and it will bend the elbow to fit.

Common problems:

  • The supporting arm can’t reach the target. This may mean you have to adjust the trigger hand’s position to compromise, which can get pretty complicated. You may be able to slide the grab point up and down the gun to get it as close as possible to the unmodified supporting hand location.
  • Elbow bends weirdly. You should be able to fix this by tweaking the IK constraints.