Creating a Gun
This article uses Humanoid Control version 3.
With the functionality available in our products it is easy to create interactive objects. In this article we show you how you can create a working pistol gun.
For this tutorial, we will use a very simple gun which is build from two Cubes and a Cylinder. Additionally we added a Audio source to it to make gun sounds and a light to implement the nozzle flash.
First we will add an Interaction Pointer to the gun. This will implement the shooting action of the gun. When the Interaction Pointer is activated it will perform a raycast to determine which object is hit. This raycast will be performed in the Forward direction of the Transform. So we create a new GameObject which is placed on the nozzle with the forward direction pointing in the shooting direction.
The Interaction Mode is Straight. If we want to take gravity into account for Heavy projectiles you could select Gravity for the Interaction mode.
The maximum shooting distance is 10 meters/units by default. You can change this if you want.
Furthermore, we have a Crosshairs GameObject as the Focus Point Object of the Interaction Pointer. This is optional and is used to visualize the place where you are shooting. It is implemented as a child GameObject of the Interaction Pointer and uses a Sprite Renderer to render the crosshairs themselves.
We will now add the functionality to use the gun. This is done using a Handle. First of all, the Handle determine at which location the Rigidbody is normally grabbed with a hand. We add a new child GameObject of the Gun and add the Handle Component to it.
In the scene view you will see a yellow Gizmo which i shaped like a uppercase L. This visualizes how the object is grabbed. The longer side of the L is grabbed in the palm of the hand such that the shorter side is in the direction of the pointing index finger. You can adjust the position and rotation of the Handle Transform until it is positioned correctly on the grip of the gun.
So now we can grab the gun correctly. Next step is to fire the gun.
For this, we extend the Controller Input section of the Handle. This defines the working of controller buttons while the guns is grabbed. We choose to use the Trigger 1 button for this, as this is the button which is operated by the index finger. When we press this button we want three things to happen:
- A nozzle flash
- Playing the Shooting sound audio clip
- And execute the shooing itself using the Interaction pointer.
This is done by attaching three function calls to the Trigger 1 button.
All three function call work in a similar way, but just call different functions. First is the Nozzle flash.
This calls the Flash() function of the FlashLight Component found on the NozzleFlash GameObject when the trigger is pressed (OnStart).
For addition a function for playing the sound, we press the + Button at the right and add a similar function as for the Nozzle Flash: when we press the trigger 1 button, the function Play() is called on the Audio Source component of the Audio GameObject.
Finally, to execute the shooing itself we will also call the Click() function on the Interaction Pointer:
All this together results in a working shooting gun which you can adapt you your needs.