Using Counters and Switches

Проверено с версией:: 2018.1

-

Сложность: Базовая

We can connect several objects together, next we’ll be looking at using a Counter to count 3 switch activations before opening the door.

  • In the Hierarchy select the Switch

  • Press Ctrl + D (Cmd + D on Mac) to duplicate the Switch

  • Repeat this one more time to get another Switch

You should see Switch, Switch (1) & Switch (2)

These will appear at the very bottom of the Hierarchy, if you are keeping your project organized Ctrl + Click (Cmd + click) each of these and drag them close together in the Hierarchy.

They will duplicate in the same place as the original;

  • In the Hierarchy, select each of the switches individually and position them one by one so they form a row.

You can also see that as we have duplicated the object, it has also duplicated it’s connection to the door. Colliding with any one of these switches will open the door. You can press Play and give that a try if you wish.

We want to open the door when all 3 switches have been activated;

  • In the Project Window go to Assets > 3DGameKit > Prefabs > Interactables

  • Find the Counter prefab and drag it into your scene

  • Position it near your switches

This object has no physical presence in the scene as it will hold game mechanics, you can see it by using the Transform Tools. Since we can’t see it in the scene, we will be selecting the Counter through the Hierarchy.

Tip: You can make these kind of object more visible in the Scene View by adding a label;

  • Select the Counter

  • At the top of the Inspector click the Blue box.

  • Select a colour label from the drop down.

This will add a label to your object.

If you can’t see it you may have your icons too small in the scene view, you can change this;

  • At the top of the scene view, click the Gizmos drop down

  • Adjust the slider which will change the size of the icons in the scene view

Now that we have a Counter in the scene that can count a number of senders, we need to send the Counter the "Open" Game Command from each Switch.

  • In the Hierarchy, select Switch

  • Click and drag the Counter into the Interactive Object slot in the Send on Trigger Enter script

This will replace the Door as the object to send a command to.

The arrows will appear to be connected to the Counter in the Scene View.

  • Repeat the above steps with Switch (1) & Switch (2)

All 3 switches will now be sending a command to the Counter

We can double check this in the Game Command Receiver script on the Counter.

Now if we enter play mode and run into each switch, nothing will happen. We need to tell DoorSmall to open when the Counter has reached 3.

  • Select the Counter in the Hierarchy

  • In the Inspector change the following settings on the Gameplay Counter script

    • Interaction Type: Open

    • Is One Shot: True (tick the box)

    • Target Count: 3

This will receive a command of type Open, only count once and set the target amount to fulfil the counter at 3.

Now let’s make the Counter send a the Command to the door.

  • With the Counter selected input the following settings on the Send Game Command script

    • Interaction Type: Open

    • Drag DoorSmall from the Hierarchy to the Interactive Object slot

    • One Shot: Enabled (tick the box)

Now each switch will send a command to the Counter and the Counter will send an Open Command to the door when it has received 3 commands.

Test your level by pressing Play, you should be able to collide with each switch and the door will open when you have done all 3.

The Counter can be used for many conditions that requires a measure to take place. The Send Game Command allows various Interaction Types beyond Enable and Open:

Indicating Progress to a Player

Usually in games we would place these sorts of mechanics far apart from each other, we have done this in the example game with this project. It’s generally good practice in game design to indicate progress to a Player, whether through a User Interface or indicators in the game.

We’ll be adapting our Game Command and Counter to show Crystals lighting up near the door when each switch is used. This would show a player that there are 3 switches in the world for that door, and indicate how many they have activated.

  • In the Project Window go to Assets > 3DGameKit > Prefabs > Interactables

  • Find the Crystal Prefab

  • Drag it into the scene

The Crystal is a little large for our level, let’s resize it;

With the Crystal Selected in the Hierarchy

  • Go to the Inspector and take a look at the Transform component

  • Set the Scale to the following;

    • X: 1

    • Y: 1

    • Z: 1

  • Place your Crystal somewhere near your door using the Transform Tools

  • With the Crystal Selected in the Hierarchy, press Ctrl + D (Cmd + D on Mac) twice to duplicate it twice.

  • Select Crystal (1) and Crystal (2) in the Hierarchy (Ctrl + Click or Cmd + Click on Mac) and drag to reorder them near Crystal

  • In the Scene View, use the Transform Tools to position the Crystals to desired locations

We have placed ours just next to the door to make it obvious to a player, let’s make them change colour when a switch is activated;

  • In the Hierarchy select the Switch

  • In the Inspector, scroll down to the bottom and click Add Component

  • In the search box type ‘Send on Trigger Enter’

  • Hit Enter or click on the Script called ‘Send on Trigger Enter’ to add it to the Switch Object

This will add another Send on Trigger Enter script to the Switch. We can use this to send a command to the Crystal to change colour when the switch is activated.

  • Set the values in the Send on Trigger Enter script we just added to the following;

    • Interaction Type: Activate

    • Drag Crystal from the Hierarchy into the the Interactive Object slot

    • One Shot: Enabled (tick the box)

    • Layer: Player

This will send a command to the Crystal to activate when the Switch is activated.

Test the mechanic by pressing Play, run up to the first switch, it should also activate the first Crystal.

Repeat these steps for each remaining Switch and Crystal combination, be sure to match Switch (1) with Crystal (1) and Switch (2) with Crystal (2). Each Switch will now send a command to the Counter and their Crystal.

User Interface (UI) Prompts

To finish off the switches gameplay elements we need to give the user a hint as to what the switches are doing and the importance of them. This is most commonly done via dialogue from the protagonist to the player, in the 3D Gamekit this takes the form of an InfoZone Prefab.

  • Go to 3DGamekit > Prefabs > Interactables in the Project view and select InfoZone prefab

  • Drag infozone into the Hierarchy under the group for ----- UI -----

Remember to add it below the Empty label

  • Move the sphere collider over the switches and resize to include all 3 in the area but not too far as you don’t want the trigger showing when Ellen is far away from them.

  • Set Layer to Environment if it has not set automatically in Inspector

  • Set Is Trigger to enabled for the Sphere Collider component in Inspector

Next step is to link the DialogueCanvas Prefab that will allow the game to tell the player the required information about the switches when they enter the trigger zone using Game Commands.

  • Go to 3DGamekit > Prefabs > UIPrefabs and find the DialogueCanvas Prefab

  • Drag in a DialogueCanvas into Hierarchy under UI

  • Deactivate it as we only want it to display when the player enters the trigger zone of the InfoZone

Select the InfoZone in your scene and find the Interact on trigger Script component in Inspector

  • Set Layers to Player if it has not automatically

  • Set On Enter (Object) and On Exit to the Dialogue Canvas Controller Script by dragging it into the empty object slot

  • Set On Enter Function to ActivateCanvasWithText from the DialogueCanvasController Function list

  • Update the text field to provide some useful text to the player. We used the following

    • "If I switch all of these on maybe the door will open."

  • Set On Exit Function to DeactivateCanvasWithDelay and set Delay equal to 1

Now that these are all placed, you can move the switches and crystals to other areas and hide them from the player to make it more challenging. The Game Command links will remain attached and allow you to use the objects more dynamically in your level. Be sure to add the DialogueCanvas to areas where you require the player to be notified of key gameplay mechanics in your project.