Adding a weapon to your character/aiming appropriately

Al Heck
3 min readJul 7, 2022

--

More often than not, you want to add weapon to your character and it should be simple. Every weapon is different so you should plan on your position for each weapon to be the same — everything else underneath can be different.

First start by grabbing a gun and placing that object under an empty game object right at the handle. This is where you will always grab the gun. Save that as a prefab.

In your script, we need to add 2 things — a weapon position and the weapon. Be sure to connect the gun prefab to the weapon and then the right hand joint to the right hand position.

Next, we will run a function at start called EquipGun() and we will instantiate the weapon, then set it to the parent, and then be sure to reset the position and rotations locally.

Once you add this and press play, the gun will spawn in the correct position, but now your gun may be pointing the wrong way. This is fine — you just need to adjust the angle of your gun object inside this gun position. For me, I adjusted the scale and held control/alt while adjusting values to make micro movements.

Once that’s complete, the gun is now attached to my player at start. There is an issue though. When you look down the sights, the gun starts pointing down. This is because there is an issue with the aiming pitch and the actual float of the value we pump into the blendtree. The aiming pitch values come in from -1 to 1.

But the value of our blendtree is from 0 to 1. 0.5f should be the middle point and that only occurs when we point up .75f. We need to run a calculation to adjust this value.

Now with this set, we can right click and our gun will aim properly in the center of our screen and move up and down appropriately.

--

--

Al Heck

Unity Developer and Software Engineer who loves making games with GameDevHQ