Creating a Realistic Gun with the XR Interaction Toolkit

David Brown
6 min readJul 26, 2022

--

Objective: Learn how to create weapons and defend yourself from alien invaders.

Lately my pistols have been getting bought out by scalpers, so I haven’t been able to sell them. However today is your lucky day because I’m going to teach you how to build them yourself.

Finding a Gun Model

The first place I go when looking for 3D models is GameDevHQ’s FileBase. It has thousands of assets that you can download into your Unity project. I chose the Baretta pistol because it has a detachable gun clip, a movable gun slider, and a script that controls the sound/particle effects.

Grabbing The Gun

After importing the gun, the first thing I did was add a XR Grab Interactable component to the Baretta Pos game object. This interactable should have kinematic movement and the rigid body should have gravity on.

Then I added some box colliders to the gun by creating two cubes and sizing them to fit the gun. Then I dragged the cubes in the colliders array. This allows the gun to be grabbed only by those colliders. We could add a mesh collider, but they can decrease performance in your game which is why I you should try to avoid using them as much as possible.

The final thing I did was create an empty game object that is a child of the pistol called attach transform. I rotated it -90 degrees on the y axis and added the game object into the attach transform slot. Now the gun can be grabbed.

Inserting The Gun Magazine

The second ability we need to add is being able to grab and insert a gun magazine into the gun handle. I duplicated the gun clip and set it where it isn’t a child of the pistol.

You probably know what to do next and that is to add a XR Grab Interactable component to the gun mag. This time I have the movement type set to velocity tracking. I also added a mesh collider this time. Now we can grab our gun clip.

We can grab it now but how do we insert it into the gun handle? I created an empty game object and placed it where the mag position should be when inserted in. Then I added the XR Socket Interactor component. This component moves objects to your desired position when in the collider radius. I added a sphere collider with isTrigger checked to detect the clip.

Now when you try to add the gun clip in the handle it actually works.

But there’s two problems. The first problem is that you can put any grab interactable object in that socket.

One way you can fix this is by creating a new interaction layer called Pistol Clip and only accept a grab interactable with that Pistol Clip layer. The XR interaction layer masks our separate from the regular layer masks.

The second problem is if you drop the pistol after inserting your gun clip, it will have collision issues.

Is this how a gun is supposed to work?

We will fix this later when we start coding.

Moving The Gun Slider

The third part of making this gun realistic is moving the gun slider. Like the other objects we need to add a XR Grab Interactable on the gun slider with the velocity tracking movement type. For the collider I used the mesh collider.

Anytime you have a unique interaction like this, then you’ll most likely need to use the configurable joint component. Add that component and select the pistol as the connected body so the slider stays in place when grabbed. Then set the X motion to limited and everything else to locked.

Then set the Linear limit to 0.01f. Set the X drive position spring to 2000 and the damper to 200. This makes sure the gun slider doesn’t move unless you grab it. Finally set the Target Position x value to double the amount of the linear limit, which is 0.02f. You need to do that to make sure the joint doesn’t move in two directions.

Now our gun interactions are complete.

Coding the Gun system

Before you add my code into your project you need to setup a few things.

Gun Clip

Create a layer called Gun Clip and a layer called Player. Assign the player layer to the left and right controllers. Don’t assign the gun clip layer to the gun magazine because it will do this in the code. Then in the project settings physics tab, go to the collision matrix make the gun clip layer ignore the default and gun clip layer. The gun clip layer should only be able to interact with the player layer.

When the player inserts the gun magazine in the gun, it will change the layer of the gun, so it doesn’t interact weirdly with the gun. Then when the mag is taken out the gun, it can interact with the gun

Gun Slider

For the gun slider you need to create an empty game object called Slider target and move it to the back of the gun. Also set the layer of the gun slider to gun clip so it doesn’t collide with the gun.

In the code, it checks the distance between the gun slider and slider target. If the gun slider is close to the slider target, then it will register that you pulled the gun slider.

The Gun

The last thing you’ll need to add is an audio source component to the gun.

Adding the Code

Finally, were able to start coding. Click on the links below and paste the code into your project. Then add the gun script to the gun and the GunClip script to the gun magazine.

Make sure on the socket interactors select entered and select exited methods, you call the AddOrRemoveGunClip method.

On the guns activate method, call the FireGun method.

Sound Effects Used

Out of ammo: Freesound — “Dry Fire Out Of Ammo.wav” by CombatSFX4You

Gun slider release: Freesound — “Gun-Slider Release.wav” by MGA95

Insert Mag: Freesound — “mag slide in” by nettimato

Thanks For Reading!

--

--

David Brown

I like pizza, cream soda, and making games with Unity