A Pump-Action Gun
This article uses Humanoid Control version 3.
In the previous article we creates a gun with a sliding barrel. In this article we show how we can create a pump-action gun using similar techniques.
For the article we creates a simple shotgun type gun based on the gun we created in our first article. We basically extended the cylinder and added a sliding grip to it.
Note that the collider of the sliding grip should not intersect the collider of the cylinder. If they would overlap, the gun will collide with itself and behave erratically.
Two Handed Grip
In contrast to our first mode, this gun should be held with two hands. One of the grip and one on the sliding grip. Because of this we also have two Handles: one for each hand.
The first Handle is similar to the handle we had for the Gun. This handle will also implement Controller Input to shoot the gun.
The second Handle on the sliding grip is placed horizontally to achieve a realistic grip position.
The Sliding Grip
Like the barrel in the previous article the sliding grip can move in a limited way. But instead of moving it by scripting, we will be able to move it using the movements of the hands holding the gun.
So the grip has a kinematic Rigidbody and Kinematic Limitations.
Now if we grab the gun with two hands, we will be able to slide the sliding grip backwards and forwards by moving the hands towards or outward for each other.
We want to have the sliding grip make a ‘Click’ sound every time it reaches the end of the sliding range, so we add an Event Handler on the movements along the Z-axis in the Kinematic Limitations. With an OnChange event type with a function call to an AudioSource.Play() function we can achieve this effect:
The Bullet Chamber
The pump action gun can only shoot when a bullet has been loaded in the bullet chamber using the pump action. To keep track whether a bullet has been loaded, we use a Counter script on the gun. We set the maximum to 1 (maximum 1 bullet can be loaded), but you could increase that if you want. We start with the gun loaded, thus the (starting) Value is 1.
Every time we shoot the gun, the number of loaded bullets in the Counter is decreased:
To load a bullet, we need to pull back and forward the sliding grip completely. To implement this, we add an Event Handler when the slider reaches the most forward position. This is when the Z-axis position of the sliding grip reaches the maximum value thus using the On End event type. When this happens we increment the Counter, adding a bullet to the amount of bullets.
Shooting only with a Loaded Bullet
We cannot shoot the gun directly like we did in the previous article. We can only shoot the gun when it has at least one loaded bullet. So we need to use a Script with a Condition for this.
This script only executes when the Shotgun Counter has a value greater than 0. The functions which will then be executes are the same as we had for the normal gun.
So instead of calling the Nozzle Flash, Audio Play and Interaction Pointer Click directly, we now execute this new script when we press the trigger:
So now we have a pump action gun which requires you to pump the gun after each shot in order to shoot again.