02 Prefab & Music Setup

In this tutorial we’ll continue our initial setup.

There’s still a bit more to do before getting down to the whole fighting business. We need an interaction controller for our princess to talk to an NPC (which starts the battle), some music and a schematic handling rotating mount points based on the movement direction (more on that below).

We’ll set up:

  • interaction controller
  • music
  • mount point rotation schematic

Let’s get to it.

Interaction Controller #

The Interaction Controller is used to interact with any interaction within it’s trigger when the player uses the Interact key.

We’ll create a prefab that’s automatically added to the player.

Prefab Setup #

Use the scene hierarchy context menu (Makinom > Interaction Controller 2D) or the Makinom Scene Wizard (Create Game Object > Interaction Controller 2D) to create a ready-to-use setup.

Change the following settings in the inspector

Box Collider 2D #

  • Size
    Set to X=1, Y=1.

Create Prefab #

Create a prefab out of the Interaction Controller (2D) game object.

Remove the game object from the scene.

Interaction Settings #

Open the editor and navigate to Base/Control > Game Controls and change the following settings.

Interaction Settings #

The default setup should already use an interaction controller and the accpet input key.

  • Interaction Control
    Select Interaction Controller.
  • Interact Key
    Select Accept.

The Interaction Controller settings handle adding our prefab to the player.

  • Add Automatically
    Enable this setting.
  • IC Prefab
    Select the Interaction Controller prefab you created.
  • On Child
    Set to: Mount/Front
    The interaction controller weill be placed on this child object – we’ll use a schematic to rotate it based on our movement direction (see below).

That’s it for the interaction controller.

Music #

Next, we’ll set up some music clips.

Navigate to Game > Music, we’ll change the Default music and add additional music.

You can find the used audio clips in Assets/Tutorial Assets/NinjaAdventure/Musics/.

0: Town #

  • Name
    Set to Town.
  • Audio Clip
    Select 4 – Village.
  • Loop Clip
    Enable this setting.

1: Battle #

  • Name
    Set to Battle.
  • Audio Clip
    Select 17 – Fight.
  • Loop Clip
    Enable this setting.

2: Victory #

  • Name
    Set to Victory.
  • Audio Clip
    Select 3 – Revelation.
  • Loop Clip
    Enable this setting.

4: Lost #

  • Name
    Set to Lost.
  • Audio Clip
    Select 7 – Sad Theme.
  • Loop Clip
    Enable this setting.

Save Changes #

That’s it for the setup.

Don’t forget to save your changes by clicking on Save Settings at the bottom of the editor.

Move Direction Rotate Schematic #

Since we’re working on a 2D game, we play different animations for the directions we move in – however, this doesn’t change the location of our mounted things, like the interaction controller or equipment viewers on our combatants.

We could also handle this in animations, but due to using blend trees, that’d also blend the used animation, leading to missplaced mounts when moving diagonally. Instead, we handle this in a schematic that rotates our mounts based on the combatant’s movement direction, same as the animator controller.

Our combatant prefabs have the following hierarchy:

The interaction controller (although only used on the player, i.e. the princess) will be mounted on the Mount/Front child object, weapons on the Mount/Front/Weapon child object. Our schematic simple needs to rotate the Mount child object by 0, 90, -90 or 180 degrees on the Z axis, based on our movement direction.

Navigate to Schematics and start workgin on a new schematic.

Change Variables #

Add Node > Value > Variable > Change Variables

We’ll first store the combatant’s move direction into a Vector3 variable for our checks. The combatant will be the Machine Object of the schematic, since the schematic is executed on the combatant’s game object using a Tick Machine.

Click on Add Variable.

  • Change Type
    Select Variable.
  • Variable Key
    Set to direction.
  • Variable Origin
    Select Local.
  • Type
    Select Vector 3.
  • Operator
    Select Set.
  • Vector3 Type
    Select Game Object > Game Object.
    We’ll use a value from a game object.
  • Object
    Select Machine Object.
  • Value Origin
    Select Last Move Direction.

We get the last move direction from the Combatant Component that’s automatically attached to a combatant’s game object. For other game objects (non-combatants), you can use this by adding an Object Changes component to them (if none is added when using that value, it’ll automatically be added).

Check Value #

Add Node > Value > Check Value

We’ll now check if the Y axis is greater than the X axis of the move direction we stored – i.e. are we more moving up/down or left/right?

  • Value
    Select Variable > Vector3 Variable.
    Select the Y axis.
  • Variable Key (Value)
    Set to direction.
  • Variable Origin (Value)
    Select Local.
  • Math Function (Value)
    Select Abs.
    This’ll check the absolute value (i.e. negative values will be used as positive values).

We’ll check if Y is greater than X.

  • Check Type
    Select Is Greater.

The check value will be the X axis.

  • Check Value
    Select Variable > Vector3 Variable.
    Select the X axis.
  • Variable Key (Check Value)
    Set to direction.
  • Variable Origin (Check Value)
    Select Local.
  • Math Function (Check Value)
    Select Abs.

Variable Fork #

Add Node > Value > Variable > Variable Fork

This node is used to check one variable against multiple values, each check having it’s own Next slot to continue the schematic.

This node is connected to the Success slot of the Check Value node.

We’ll use it to check if the direction’s Y axis is positive or negative, i.e. if we’re moving up (north) or down (south).

  • Variable Key
    Set to direction.
  • Variable Origin
    Select Local.

Change the already added Variable Condition 0.

  • Type
    Select Axis Vector 3.
  • Axis
    Select Y.
  • Check Type
    Select Is Less.
  • Check Value
    Select Value > Value.
    Set the value to -0.1.

Copy Variable Condition 0.

  • Check Type
    Select Is Greater.
  • Check Value
    Select Value > Value.
    Set the value to 0.1.

Change Rotation #

Add Node > Movement > Rotation > Change Rotation

This node is used when we’re moving south, i.e. we need to rotate back to 0.

This node is connected to the 0: is Y IsLess -0.1 slot of the Variable Fork node.

  • Object
    Select Machine Object.
  • Child Object
    Set to Mount.
  • Rotate Component
    Select Transform.
  • Set Rotation
    Enable this setting.
    Otherwise the rotatio will be changed by adding to the current rotation.
  • Vector3 Type
    Select Value > Value.
  • Value
    Set to X=0, Y=0, Z=0.

Change Rotation #

Copy the previous Change Rotation node, this time we’re moving north, i.e. we need to rotate to 180.

This node is connected to the 0: is Y IsGreater 0.1 slot of the Variable Fork node.

  • Value
    Set to X=0, Y=0, Z=180.

Variable Fork #

Copy the previous Variable Fork node, now we’ll handle the X axis, i.e. if we’re moving left (west) or right (east).

This node is connected to the Failed slot of the Check Value node.

We simply need to change the used axes to X.

  • Axis (Variable Condition 0)
    Select X.
  • Axis (Variable Condition 1)
    Select X.

Change Rotation #

Copy the previous Change Rotation node, this time we’re moving west, i.e. we need to rotate to -90.

This node is connected to the 0: is X IsLess -0.1 slot of the 2nd Variable Fork node.

  • Value
    Set to X=0, Y=0, Z=-90.

Change Rotation #

Copy the previous Change Rotation node, this time we’re moving east, i.e. we need to rotate to 90.

This node is connected to the 0: is X IsGreater 0.1 slot of the 2nd Variable Fork node.

  • Value
    Set to X=0, Y=0, Z=90.

Restart Schematic #

Add Node > Machine > Restart Schematic

This node is used to restart the schematic in the next frame (or when used in Fixed Update, the next physics frame). If you use a schematic each frame, it’s better for performace to let it restart instead of having it stop and start again by a machine component.

This node is connected to the Next slots of all Change Rotation nodes and the Failed slots of all Variable Fork nodes.

No further settings needed.

And that’s it for the schematic. Click on Save Schematic to save it, e.g. as MoveDirectionRotate.

Music Scene Setup #

We’re done with the editor now, so you can close it.

Forest Battle Scene #

We’ll now add music to our two scenes. First, open the Forest Battle 1 scene (Assets/Scenes/).

We’ll add a Game Starter and a Music Player.

Add Game Starter #

Use the scene hierarchy context menu (ORK Framework > ORK Game Starter) or the scene wizard to add an ORK Game Starter to the scene.

The added game starter should already have your Project asset set up. Change the following setting in the ORK Game Starter component’s inspector.

  • Start Game
    Enable this setting.

It doesn’t really matter where you place the game starter.

Add Music Player #

Use the scene hierarchy context menu (Makinom > Music Player) or the scene wizard (Create Object > Music Player) to create a Music Player in the scene.

  • Play Type
    Select Fade To.
  • Music Clip
    Select Battle.
  • Fade Time
    Set to 1.
  • Interpolation
    Select Linear.

It also doesn’t matter where you place the music player.

Save the changes you’ve made in the scene.

Town Scene #

Open the Town scene, we’ll only need to add the music player.

Add Music Player #

Use the scene hierarchy context menu (Makinom > Music Player) or the scene wizard (Create Object > Music Player) to create a Music Player in the scene.

  • Play Type
    Select Fade To.
  • Music Clip
    Select Town.
  • Fade Time
    Set to 1.
  • Interpolation
    Select Linear.

It also doesn’t matter where you place the music player.

Save the changes you’ve made in the scene.

Combatant Prefab Setup #

Finally, we’ll set up the combatant prefab – all our combatant prefabs are variant of a base prefab, so we only need to make changes to the base prefab.

Open the CombatantBase prefab for editing, you can find it in Assets/Tutorial Assets/Prefabs/Combatants/.

We’ll add a Tick Machine to use the schematic we set up and also add an Equipment Viewer to show weapons during attacks.

Add Tick Machine #

Add a Tick Machine component to the CombatantBase root game object using the component menu.

Start Settings #

  • Fixed Update
    Enable this setting.
    Since our combatants move using Rigidbody 2D components, the move direction is only updated in fixed updates, so we only need to use our schematic there too.

Machine Execution Settings #

  • Schematic Asset
    Select the MoveDirectionRotate schematic.
  • Update Type
    Select Fixed Update.
    This is important to have the Restart Schematic node also restart in fixed update.

Add Equipment Viewer #

Equipment Viewer components are used to display a combatant’s equipment by spawning it’s Viewer Prefab. We’ll only display equipment during attacks – we’ll control this by using a bool variable on our combatants.

Select the Mount/Front/Weapon child object and add an Equipment Viewer component using the component menu.

Equipment Viewer Settings #

  • Equipment Slot
    Select Weapon.

Display Conditions #

Click on Add Condition.

  • Condition Type
    Select Variable.

Click on Add Variable.

  • Condition Type
    Select Variable.
  • Variable Key
    Set to showWeapon.
  • Variable Origin
    Select Object.
    We’ll use the combatant’s variables.
  • Game Object
    Select User.
    This’ll use the combatant.
  • Is Valid
    Enable this setting.
    The check needs to be valid.
  • Type
    Select Bool.

And that’s it for the prefab – save the changes.

Testing #

Hit play in the Town scene – you don’t really see what’s going on unless you enable displaying Gizmos in the game view.

When displaying gizmos (and having the princess selected), you’ll see the mounts will rotate with our move direction.

Oh, and you can also hear the music we’ve added.

 

Next, we’ll randomize our combatants!