IK Weapon System in Unity

Today, I have got into detail within Inverse Kinematics in Unity. If you are also curious about why it is called “Inverse”, you better click here to check the brief explanation in Unity Manual. Simply, in order to achieve the desired result, a top-down approach is used in determining the position of joints.

It is time to add a character and a gun to the game. As we will be holding a gun, I have downloaded a Gun Idle animation from Mixamo. For a brief information regarding using animations from Mixamo within your Unity project, you can check my article by clicking here.

As the character slightly rotates to the left because of the animation, “Root Transform Rotation” setting is set to Based Upon “Original”.

Animation Settings — Humanoid

Again, from Filebase, we have downloaded a gun and imported to the project. Well, thanks to Al, it is ready to use. After setting the gun and camera position appropriately, the only — and the crucial — thing left to do is Rigging the Hands so that we will get the perception that our character is actually holding a gun.

Now, the exciting part: Animation Rigging.

To enable Animation Rigging, click on the Gear Icon in the Package Manager, and check the “Enable Preview Packages” box.

And then select the Animation Rigging and Install it to your project.

The IK Rigging must be done within a parent object that is holding the Animator component. On the Player game object, add the component “Rig Builder”.

The component will ask you to drag a Rig. Create an empty game object as a child to the Player game object and add “Rig” component. Then, drag and drop the Rig (The empty child object you have just created) into the Rig Layers.

As we need two hands (what a surprise), simply create a child game object for both hands. Additionally, create a “hint” game object as a child for both hands. Here’s the Unity definition for Hint:

Briefly, setting the Hint point appropriately results in desired movement of the Target. Here, the target is the hand and hint point is like the elbow. The hand will calculate its movement according to the elbow position.

Changed the name of the Rig to IK

First, the hands. Add “Two Bone IK Constraint” Component.

If you set the target point too far away that the character is unable to reach, it will just hold it straight at its closest position to the target. As the Hint points will be the elbows, we just make sure that their transform are at the back and closer to the floor (just like it should be).

We would be probably having multiple types of weapons within the game. Certainly, it is better to design the project so that we would need minimum effort to adjust the hand positions for each weapon.

If we update the IK game object with the necessary positions of hands, we can solve this problem. For the USP gun we are holding, 2 game objects (1 for each hand) has been created.

A short script is added as a component to both L_Hand and R_Hand child game objects of IK.

And as you can guess, the public target variable for each hand will be set as the child objects of the gun.

At the first adjustment, you will probably get a funny result:

Anyway, try to observe if you have succeeded in setting the hint and target points.

A reminder, if you do not observe the arms within the Game view eventhough you have set the clipping planes Near value to 0.01; you will need to check the “Update When OffScreen” checkbox for the parts that you want to be visible.

Before:

After (Not yet arranged):

It is easily fixed with some update on the hand L_Hand and R_Hand positions:

So are we done? Of course not :) After arranging the hand positions, you will realize that the hands and gun are not in lock with each other:

To solve this, create an empty game object as a child object to the Main Camera, name it “Gun_Pos” and copy-paste the transform values of the Gun.

Now, take the gun (USP_Pos in my project) outside of the Main Camera. Add the SmoothDamp script we have created and set the new Empty Gun_Pos Game object as child.

Not perfect but, better:

Unity Developer