Day 139 — IK Weapon System in Unity Part 2
Hey and welcome!
Now that the idle animation is up and running it’s time to start looking into adding a gun into the view of the player and where we get the IK implemented for our player’s arms!
If you have Filebase there’s a handy USP Glock asset that you can make use of for this guide:
If not then you’ll need to make or find your own, you should get an idea of how this model works from this article though! Once you have your gun go ahead and add it to your scene and position it in front of your player to the bottom right of their view:
When you’re happy with the position go ahead and add your weapon as a child object to your main camera in your player object. Go to your main camera and set the clipping planes value to 0.01, you’ll probably start seeing the inside of your head when you do this so move your camera forward until you can’t see that.
Next we’re going to get our IK rigging set up, go ahead and make your way over to your package manager and then find and install the Animation Rigging package into your project.
With that imported, add a Rig Builder component to your Player object making sure it’s where you have your animator and scripts and then create a new empty child object for your player named Rig. Add the rig component to this and then drag and drop your Rig object into the Rig Layers option in your Rig Builder.
Inside your Rig add 4 new empty child game objects called “L_Hand”, “R_Hand”, “L_Hint” and “R_Hint” which will soon make sense to you. Let’s look into setting up the L_Hand IK first, inside your Player object you should find an object called mixamorig:Hips. This is the rig that we imported along with our model from Mixamo and inside that under spine you’ll find three rigs for the left shoulder, left forearm and left hand.
Head back to your L_Hand object and add a Two Bone IK Constraint component and then add in your shoulder as the Root, your forearm as the Mid and your left hand as the Tip values that this component looks for. Lastly add in your L_Hand as the target and then your L_Hint as the hint and you’ll now have your IK fully set up!
Position your L_Hint object below and behind your left elbow and then move your L_Hand somewhere in front or near your left hand. Run your game and then pause it and have fun moving about these two objects now in order to see your IK in action!
As you can see here we’ve just created a very easy way to move our arm to our liking which gives us a lot more control when lining up our hands so that they look like they are holding the gun! We’ll leave things here for now but feel free to add in the IK for your right arm as well.