Game Toolkit for Unity
Setting up the enemy game mechanics.
In this tutorial we’ll create schematics for the enemy game mechanics. We need schematics for the movement, being hit by a rocket and death of an enemy.
First, we’ll set up the movement schematic. The enemy will just move in one direction (left or right) until an obstacle is in the way (checked through a point shapecast), and continue moving in the other direction (i.e. flipping the sprite).
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
Add Node > Game Object > Raycast > Check Shape
This node will use a shape (e.g. a 3D sphere or a 2D box) to check for colliders in the scene. We’ll use it to check for 2D colliders at a point in world space (in front of the enemy) to determine if we’ve reached an obstacle.
The Point defines the 2D point in world space we’ll be checking – using 2D shapes will only check for 2D colliders.
The Layer Mask defines the layers we’ll be hitting.
We’ll use Filter Game Objects to only check for objects with the tag Obstacle.
Click on Add Object Check to add a filter check.
Add Node > Machine > Start Tagged Machine
We’ll use this node to flip the enemy’s sprite by starting a tagged machine with the tag flip.
This node is connected to the Success slot of the Check Shape node (i.e. we’ve run into an obstacle).
Click on Add Tag to add a starting tag.
Add Node > Value > Variable > Change Variables
We’ll use this node to store the X-axis velocity we’ll use for the enemy. The velocity will be the scale (which determines if the enemy is facing left or right, i.e. -1 or 1) multiplied by the speed (an object variable of the enemy).
This node is connected to the previous node’s Next slot and the Failed slot of the Check Shape node (i.e. we didn’t hit an obstacle, but still need to move).
Click on Add Variable to add a variable change – first we’ll store the X-axis scale of the enemy.
Copy the previous variable change and change the following settings – we’ll now multiply by the speed.
Add Node > Game Object > Rigidbody > Rigidbody Change Velocity
Finally, we’ll set the enemy’s velocity.
We’ll use the velocity variable for the X-axis.
We’ll use the current Y-axis velocity of the enemy as Y-axis.
The Z-axis velocity will be set to 0.
That’s it for the movement schematic – click on Save Schematic and save it as Enemy in Assets/Schematics/.
Next, we’ll set up the schematic that’ll be started by the rocketHit tag when the enemy was hit by a rocket.
Create a new schematic and change the following settings.
We’ll change the sprite of the enemy when the health is down to 1, so we’ll need to add a sprite resource to the schematic.
Click on Add Sprite Resource to add a sprite resource.
We’ll set the sprite in the machine’s resource overrides.
Add Node > Value > Variable > Check Variables
We’ll use this node to check if the enemy still has health left.
We’ll use this step to reduce the enemy’s health by 1 due to the rocket hit.
This node is connected to the previous node’s Success slot.
Click on Add Variable to add a variable change.
Add Node > Value > Variable > Variable Fork
This node is used to check a single variable for multiple conditions – the first condition that’s valid will continue the schematic. We’ll use it to check the enemy’s health – if it’s down to 1, we want to change the sprite, if it’s 0 or less we want to initiate death.
Change the settings of the already added variable condition.
Copy the variable condition and change the following settings.
Add Node > Game Object > Renderer > Change Sprite
We’ll use this node to change the sprite of the enemy to the damaged sprite.
This node is connected to slot 0 of the Variable Fork node (i.e. is equal 1).
We’ll use this node to initiate the death of the enemy.
This node is conencted to slot 1 of the Variable Fork node (i.e. is less equal 0).
That’s it for the hit schematic – click on Save Schematic and save it as EnemyHit in Assets/Schematics/.
Before handling the enemy’s death, we’ll set up a small schematic that will be used to move a notification/flying text we’ll display when the enemy dies (displaying the points the player earned). Flying texts are displayed using a Show Flying Text node and can be animated using schematics – we’ll fade the text in and out and move it upwards.
Add Node > Game Object > Renderer > Change Color
This node is used to change colors, e.g. fade the color of game objects (changing the color of the renderer) and can also be used to fade flying texts. We’ll use it to fade in the text at the start.
Add Node > Movement > Movement > Move Into Direction
We’ll use this node to move the flying text upwards.
Add Node > Base > Wait
We’ll wait for 2.75 seconds – afterwards, we’ll fade out the text for another 0.25 seconds.
Finally, we’ll fade out the text.
That’s it for the schematic – click on Save Schematic and save it as MoveNotification in Assets/Schematics/.
Finally, we’ll set up the schematic handling the enemy’s death. When the enemy dies, we’ll disable all sprite renderers and only enable the main sprite renderer and change it’s sprite. Afterwards, we’ll send the enemy flying down. Additionally, we’ll add points to the player’s score (global variable) and display a score notification (flying text). We’ll need to set up a UI prefab for the flying text later with the rest of the UI setup.
We’ll play a random enemy death sound, a random taunt sound on the player and change the sprite of the dead enemy, so we’ll need some resources.
Click on Add Audio Clip Resource to add an audio clip resource
We’ll use 3 different audio clips for the enemy’s death sound, so add the needed amount of clips by clicking on Add Audio Clip.
Click on Add Audio Clip Resource again to add another audio clip resource
We’ll use 8 different audio clips for the taunt sound, so add the needed amount of clips by clicking on Add Audio Clip.
Add Node > Game Object > Component > Enable Component
We’ll use this node to disable the sprite renderers on the enemy.
We’ll use this node to enable the main sprite renderer on the body (we’ll use it to display the dead sprite).
We’ll use this node to change the sprite of the enemy.
We’ll use this node to increase the player’s score (global variable) by the points reward of the enemy (object variable).
Add Node > UI > Dialogue > Show Flying Text
This node is used to display a flying text in the scene. We’ll use it to display the points the player earned – using the schematic we’ve created earlier to animate the text.
We need to return here later after setting up the UI prefabs.
The Content defines what’s displayed in the flying text, we’ll show the points and use an additional content to show a ‘Points’ text. Additional content can be used to display multiple contents in a UI on different locations and different styles (beyond using text codes).
Click on Add Additional Content to add another content.
We’ll also use the schematic we just created to animate/move the text.
Add Node > Function > Change Fields
This node is used to change fields or properties on components or static classes – you can use it to change options on components that aren’t yet supported by their own nodes.
We’ll use this node to change the fixedAngle property of the enemy’s Rigidbody2D component.
Click on Add Field to add a field/property that will be changed.
Add Node > Game Object > Collider > Collider Change Trigger
We’ll use this node to set all colliders on the enemy’s game object to be triggers. That way the enemy will fall through the ground.
Add Node > Game Object > Rigidbody > Rigidbody Add Torque
We’ll use this node to add a random torque to the enemy, this will cause the enemy to rotate.
Add Node > Audio > Audio > Play Sound At Point
We’ll use this node to play the player’s taunting sound at the player’s position.
We’ll use this node to play the enemy’s death sound at the enemy’s position.
And that’s it for this schematic – click on Save Schematic and save it as EnemyDeath in Assets/Schematics/.
The next tutorial will handle adding the enemy machines playing the schematics we created in this tutorial.