Setting up game mechanics to hit and destroy asteroids, enemies and the player.
Hitting game objects (e.g. a player bolt hitting an enemy ship or an asteroid hitting the player) and destroying them (when their health is down to 0) will be handled by 2 schematics that will be reused on many different game objects.
The schematic handling hitting a game object will reduce the health of the game object that was hit and trigger it’s destruction, when the health is down to 0.
Open the Makinom editor, navigate to Schematics and create a new schematic.
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 > Value > Variable > Check Variables
This node will check variable conditions. We’ll use it to check if the game object that was hit (i.e. the starting object) is marked invincible through an object bool variable.
Click on Add Variable to add a variable condition.
Add Value > Variable > Change Variables
This node is connected to the Success slot of the previous node.
This node will change variables. We’ll change the object int variable health of the starting object using the local variable damage (start variable of the machine component).
Click on Add Variable to add a new variable that will be changed.
Now, we’ll check if the starting object’s health is 0 or less.
Add Node > Machine > Start Tagged Machine
This node will try starting a tagged machine added to a game object. We’ll use it to start a tagged machine with the tag destroy on the starting object when the health is 0 or less.
This is the game object with the tagged machine component.
This is the game object that will be used as starting object by the tagged machine.
Click on Add Tag to add a tag.
Now, we’ll also trigger the destruction of the machine object. E.g. an asteroid colliding with the player will cause both to be destroyed.
This node is also connected to the Failed slots of both Check Variables nodes and the Next slot of the previous Start Tagged Machine node.
Add Node > Game Object > Game Object > Activate Object
This node will enable or disable game objects. We’ll use it to disable the machine object (in case it wont be disabled by the destroy tagged machine).
That’s it for the hit schematic – click on Save Schematic and save it as Hit in Assets/Schematics/.
We’ll add the Hit schematic to the Bolt prefab to allow the player’s bolt to damage and destroy other game objects. You can find the prefab at Assets/Tutorial Resources/Prefabs/.
Add a trigger machine component (e.g. using the component menu: Makinom > Machines > Trigger Machine). Change the following settings.
The Machine Start Variable we’ve set up in the schematic is automatically added here with its default value. Make sure the variable is enabled.
That’s it for the Bolt prefab. Since we added the component directly on the prefab, we don’t need to apply the changes.
The schematic handling the destruction of a game object will spawn a prefab (particle effect) and play an audio clip before deactivating the game object. It’ll also increase the player’s score (when the destroyed object has an object variable points).
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We’ll add a prefab and an audio clip resource to allow resource overrides in the machine component that’ll use the schematic. This allows us the reuse the schematic on different game objects, playing different particle effects and audio clips.
Click on Add Prefab Resource to add a prefab resource. Make sure that Destroy Prefabs is disabled.
Click on Add Audio Clip Resource to add an audio clip resource.
Add Node > Value > Variable > Change Variables
We’ll change the player’s score (global variable) using an object variable points of the destroyed object (i.e. the machine object). Both are int variables.
Click on Add Variable to add a variable that will be changed.
Add Node > Game Object > Prefab > Spawn Prefab
This node will spawn the prefab.
Add Node > Game Object > Prefab > Destroy Prefab
This node will destroy prefabs spawned by the schematic. We’ll use it to destroy the prefab after some time.
This node will enable or disable game objects. We’ll use it to disable the machine object.
That’s it for the destroy schematic – click on Save Schematic and save it as Destroy in Assets/Schematics/.
We’ll add a tagged machine component to the player’s game object. Select the Player game object in the scene 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.
The Resource Overrides allow us to use different prefabs and audio clips.
First, we’ll override Prefab 0 used by the schematic.
Click on Add Prefab to add a prefab that will be used.
Now, we’ll override Audio Clip 0 used by the schematic.
Click on Add Audio Clip to add an audio clip that will be used.
And that’s it for now. Don’t forget to save apply the changes to the player’s prefab (Overrides > Apply All) and save the scene.
The next tutorial will handle the asteroids.
© 2015 Gaming is Love e.U.
Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!