Gameplay Loop

Part 2: Win/Lose Conditions and Collectibles

12 min read 73K+ views Apr 12, 2021
Part 2 of 5 - Visual Scripting Series

In this part, we'll create the gameplay loop - adding ways to win, lose, and collect coins. Make sure you've completed Part 1 first.

1 Detecting When the Player Falls

The first lose condition: falling off the platform.

  1. Open your Player Graph
  2. Remove the Start and Update events (we won't need them)
  3. Group your existing movement logic (Ctrl + drag to select, then group)
  4. After the movement logic, drag out and add an If unit

Check Y Position

  1. Add Transform Get Position
  2. Add Vector3 Get Y
  3. Add Less Than unit
  4. Set the B value to -1
  5. Connect the boolean output to the If unit

2 Restarting the Scene

When the player falls, restart the level:

  1. Add Scene Manager Load Scene (by name)
  2. To get the current scene name dynamically:
    • Add Scene Manager Get Active Scene
    • Add Scene Get Name
    • Connect to the scene name input
  3. Connect the If unit's True output to Load Scene
Dynamic Scene Loading

By getting the active scene name, the restart logic works on any level without hardcoding the scene name.

3 Collision with Obstacles

The second lose condition: hitting an obstacle.

Setting Up Tags

  1. Select your Obstacle object
  2. In the Inspector, click the Tag dropdown
  3. Click Add Tag → Create a new tag called "Enemy"
  4. Select the Obstacle again and assign the Enemy tag

Detecting Collision

  1. In your Player Graph, add On Collision Enter
  2. Add an If unit
  3. Add Compare Tag unit
  4. Connect the Collider output to Compare Tag
  5. Set the tag to "Enemy"
  6. Connect the boolean to the If unit

4 Adding a Delay Before Restart

Instead of restarting immediately, add a brief delay:

  1. Add a Timer unit between If (True) and Load Scene
  2. Set duration to 1 second
  3. Connect On Completed to Load Scene

Preventing Multiple Triggers

  1. Add a Once unit before the Timer
  2. This ensures the restart only triggers once, even if you hit multiple enemies

5 Disabling Movement on Death

Stop player control after hitting an obstacle:

  1. Add a Toggle Flow unit at the start of your movement logic
  2. Connect Fixed Update to Toggle Flow's input
  3. Connect Toggle Flow's On output to your movement logic
  4. When the Timer starts, connect to Toggle Flow's Off input

6 Creating the Finish Line

  1. Create a new Cube (3D Object → Cube)
  2. Name it "Finish"
  3. Position it at the end of your level
  4. Scale X to 15 to match the ground width
  5. Create a green material and apply it
  6. Assign the Finish tag (create it if needed)

Win Detection Logic

  1. In your collision detection, after checking for Enemy (False branch):
  2. Add another If and Compare Tag for "Finish"
  3. On True: Add Rigidbody Set Is Kinematic to True
  4. This stops the player from bouncing off the finish line
  5. Connect to your restart logic (we'll change this to load next level in Part 3)

7 Adding Collectible Coins

Creating the Coin

  1. Add a Cylinder (3D Object → Cylinder)
  2. Scale Y to 0.2
  3. Rotate X by 90 degrees
  4. Create a yellow material and apply it
  5. Rename to "Coin"

Making it a Trigger

  1. Select the Coin's Capsule Collider
  2. Check Is Trigger
  3. Adjust the collider size if needed (e.g., 0.8, 0.1, 0.8)

Coin Collection Logic

  1. Tag your Player object as "Player"
  2. Add a Script Machine to the Coin (use Embedded)
  3. In the graph, add On Trigger Enter
  4. Add Compare Tag checking for "Player"
  5. Add an If unit

8 Saving Coin Count

Use Saved Variables to persist the coin count across scenes:

  1. Go to Saved variables tab in the graph
  2. Add a new variable: "Coins" (Integer, default 0)
  3. Drag the variable into your graph
  4. Add an Add unit (set B to 1)
  5. Hold Alt and drag the variable to get Set Saved Variable
  6. Connect the incremented value

Destroying the Coin

  1. Add GameObject Destroy
  2. For the object input, use This (the current game object)
Saved Variables

Saved Variables persist across scene loads, perfect for tracking total coins collected throughout the game.

What's Next?

In Part 3, we'll cover:

  • Creating multiple levels
  • Start and End screens
  • Displaying the coin count with UI
  • Building the game