Game Toolkit for Unity
Setting up the player’s weapon controls.
In this tutorial we’ll create a schematic to control the player’s weapon.
First, we’ll create the schematic to fire the player’s weapon. The schematic will play an audio clip, particle effect and show a line renderer. The player input firing the weapon is handled by an Interaction Machine component we’ll use to play the schematic.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We’ll set up a local variable as Machine Start Variable for easy setup in the machine component at a later time. When using the schematic in a machine component, the defined start variables will be added automatically, using their default values.
Click on Add Start Variable to add a local start variable that will be exposed to the machine component’s inspector.
Add Node > Audio > Audio > Play Sound
This node is used to play audio clips. We’ll use it to play the gun shot sound.
Add Node > Game Object > Component > Enable Component
This node is used to enable/disable components. We’ll use it to enable a light component.
Add Node > Game Object > Component > Emit Particles
This node is used to start/stop emitting particles. We’ll use it to start a gun fire particle effect.
We’ll use this node to enable a line renderer component.
Add Node > Function > Call Function
This node is used to call functions on components or static classes. We’ll use it to set the line renderer’s start position, calling the SetPosition function on the LineRenderer component.
The SetPosition function needs 2 parameters, an int and a Vector3.
Click on Add Parameter to add a parameter.
Click on Add Parameter again.
Add Node > Game Object > Raycast > Raycast
This node is used to send a raycast into the scene. We’ll use it to see what we’ve hit with our gunfire.
We will store the game object and position we’ve hit with the raycast.
We want the game object that was hit to be used as Selected Data. Selected data is used to store things for later use, e.g. game objects that are found during a running schematic (in our case through raycasting). We’ll use the stored game object in the following nodes.
Add Node > Machine > Start Tagged Machine
This node is used to start a tagged machine. We’ll use it to start a tagged machine with the tag damage, which will handle reducing the target’s health. The actual damage is stored in the local float variable damage (local start variable of the machine).
This node is connected to the Success slot of the Raycast node.
Click on Add Tag to add a starting tag.
We’ll use it to start a hit particle effect on the target.
We’ll use it to set the line renderer’s end position to the point we hit with the raycast.
This node is connected to the previous node and the Failed slot of the Raycast node.
We’ll use it to start a tagged machine that disables the light and line renderer of the gun after a short time. This is separated from this schematic to prevent extending the fire rate.
Add Node > Base > Wait
This node is used to wait for some time. We’ll use it to wait for the player’s fire timeout (object variable).
And that’s it for the schematic – click on Save Schematic and save it as PlayerShooting in Assets/Schematics/.
The PlayerShooting schematic will be added to the player’s gun. Select the GunBarrelEnd game object, a child object of the player’s game object.
Add an Interaction Machine component to the game object (e.g. using the component menu: Makinom > Machines > Interaction Machine). Change the following settings.
We’ll only want the controls to work while the player is alive, i.e. we need to check the health of the player using our template.
Click on Add Condition, we’ll set up the variable condition here.
In the added Condition 0, we’ll set up our variable condition.
Click on Add Variable.
The Machine Start Variable we’ve set up in the schematic is automatically added here with its default values. Make sure the variable is enabled.
Now it’s time to create the schematic to end the gun effects (i.e. disable light and line renderer components).
We’ll use this node to disable the light component.
We’ll use this node to disable the line renderer component.
And that’s it for the schematic – click on Save Schematic and save it as DisableGunEffects in Assets/Schematics/.
Finally, we’ll add the DisableGunEffects schematic to the gun using a tagged machine. Again, select the GunBarrelEnd child game object of the player and add a tagged machine component (e.g. using the component menu: Makinom > Machines > Tagged Machine).
Change the following settings.
Click on Add Starting Tag to add a tag that can start this machine.
We’ll delay the machine’s start for a short amount of time, allowing the effects to display for a flash. Expand the Wait Time Settings to show the needed setting.
And that’s it for now – apply the changes to the prefab by clicking on Overrides > Apply All (having the Player game object selected, top of the inspector).
Don’t forget to save the scene.
Click on Play to test the game. You’ll be able to shoot the gun by clicking the left mouse button (Fire input key).
The next tutorial will handle enemy game mechanics.