Day 140 — IK Weapon System in Unity Part 3

Hey and welcome!

In this one we’re going to be finishing up our IK weapon system starting off by getting our arms to line up with our gun and move around with it.

Firstly make sure that you have your IK system in place for your right arm as you do for your left arm and then create two child objects in your gun which should be a child of your main camera and name them HandPos_L and HandPos_R. Once you’ve done that you can now create a new script to attach to your L_Hand and R_Hand objects called SmoothDamp and then add in the following code to that:

public Transform target;
public float speed = 10f;
private void LateUpdate()
Vector3 targetPosition = target.transform.position;
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * speed);
transform.rotation = Quaternion.Euler(target.transform.rotation.eulerAngles);

This is our first time looking into lerping which is used to linearly interpolate between two points. In our case here the two points will be the L/R_Hand and our HandPos_L/R in the rig which is going to allow our arms to move along with the gun. Once you have your script in place you can drag and drop your HandPos_L and HandPos_R objects into the relevant target value and you should be getting this happening for you:

Your hand will probably look a bit off as you can see above but go ahead and adjust your gun position or your HandPos_L/R to get your arms and hand placement to a suitable area around your gun.

Once you’ve done that we’re now going to adjust our hand positions as well as the gun position if need be, since we’ve already put in the groundwork for our IK rigging this is actually a pretty easy step to do!

Go ahead and run your game and set your aim so that your gun is level and your arms are straightened out and the click into your hierarchy, doing will prevent your mouse from interacting with the game which will allow you to adjust your hand positions and gun positions while the game is running:

There are three objects you need to move about, your HandPos_L, HandPos_R and the gun. Adjust these one at a time making sure that you change the position and rotation if needed so that both your hands line up nicely with your gun. This is also a good time to adjust the scale and position of your gun if necessary. Once you’re happy with the position you can copy the component and then paste in the values when the game is no longer running. Don’t worry too much about your hand/arm placements look from an outside view, just focus on how they look from a first person view until you have something you’re happy with:

When you run your game and test this out you’ll notice that there’s noticeable lag between you hands and your gun when you move about quickly. Luckily there’s one last thing we can do to fix this. Go ahead and create an empty object in your gun calle GunPos and copy and paste the transform values from your gun into this new object. Now drag out your gun from your main camera so that it’s no longer a child of it.

Add your SmoothDamp script to your gun and then set your GunPos as the target value and you should have something like this now:

Good work here, I’d say that this is looking pretty good! Next time we’re going to look into implementing a reload animation!

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.