Holding Weapon with IK Pt.02- Game Dev Series 129
Objective: using IK to make Player holding a pistol.
After creating the IK system to both arms, now we can make Player holding a pistol with it.
Import a pistol
First, let’s import the pistol gameobject. You can download one from Unity Assets Store, or using the pistol prefab from GameDevHQ Filebase like me.
After imported the pistol prefab, drag it to Camera as its child object. Now the pistol should be under Camera, and Camera should be under Player.
Adjust the hand position
Now we can make the arms holding this tiny weapon.
To make it holding perfectly when game starts, we need to use codes to control arms moving to the right position.
Let’s create the holding point to pistol first. Create 2 empty objects under pistol prefab.
Next, create a script called “SmoothDamp”, and attach this script to both left and right hand gameobjects under IK.
Inside this script, we will make both hands snapping to the position we just created in pistol.
Back to editor and drag the HandPosition objects into this script on both hands.
Now you can press Play and see what would happen.
Fix the holding position
When play the game, you should see something quirky like this:
And now we need to fix this issue.
When under Play Mode, click some place outside the Game window, Player movement should be paused at where it is.
With that position, adjust the position and rotation of HandPosition gameobject.
Remember, one gameobject a time.
After adjust it, copy the transform component. Then exit Play Mode and paste it back to the HandPosition gameobject as new value.
Do the same setting to another HandPosition gameobject.
And don’t forget to adjust the snapping speed of SmoothDamp script, which will make the movement of hands look more naturally.
Once done, your Player should holding the pistol tightly when playing the game.