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.
- Open your Player Graph
- Remove the Start and Update events (we won't need them)
- Group your existing movement logic (Ctrl + drag to select, then group)
- After the movement logic, drag out and add an If unit
Check Y Position
- Add Transform Get Position
- Add Vector3 Get Y
- Add Less Than unit
- Set the B value to -1
- Connect the boolean output to the If unit
2 Restarting the Scene
When the player falls, restart the level:
- Add Scene Manager Load Scene (by name)
- To get the current scene name dynamically:
- Add Scene Manager Get Active Scene
- Add Scene Get Name
- Connect to the scene name input
- 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
- Select your Obstacle object
- In the Inspector, click the Tag dropdown
- Click Add Tag → Create a new tag called "Enemy"
- Select the Obstacle again and assign the Enemy tag
Detecting Collision
- In your Player Graph, add On Collision Enter
- Add an If unit
- Add Compare Tag unit
- Connect the Collider output to Compare Tag
- Set the tag to "Enemy"
- Connect the boolean to the If unit
4 Adding a Delay Before Restart
Instead of restarting immediately, add a brief delay:
- Add a Timer unit between If (True) and Load Scene
- Set duration to 1 second
- Connect On Completed to Load Scene
Preventing Multiple Triggers
- Add a Once unit before the Timer
- 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:
- Add a Toggle Flow unit at the start of your movement logic
- Connect Fixed Update to Toggle Flow's input
- Connect Toggle Flow's On output to your movement logic
- When the Timer starts, connect to Toggle Flow's Off input
6 Creating the Finish Line
- Create a new Cube (3D Object → Cube)
- Name it "Finish"
- Position it at the end of your level
- Scale X to 15 to match the ground width
- Create a green material and apply it
- Assign the Finish tag (create it if needed)
Win Detection Logic
- In your collision detection, after checking for Enemy (False branch):
- Add another If and Compare Tag for "Finish"
- On True: Add Rigidbody Set Is Kinematic to True
- This stops the player from bouncing off the finish line
- Connect to your restart logic (we'll change this to load next level in Part 3)
7 Adding Collectible Coins
Creating the Coin
- Add a Cylinder (3D Object → Cylinder)
- Scale Y to 0.2
- Rotate X by 90 degrees
- Create a yellow material and apply it
- Rename to "Coin"
Making it a Trigger
- Select the Coin's Capsule Collider
- Check Is Trigger
- Adjust the collider size if needed (e.g., 0.8, 0.1, 0.8)
Coin Collection Logic
- Tag your Player object as "Player"
- Add a Script Machine to the Coin (use Embedded)
- In the graph, add On Trigger Enter
- Add Compare Tag checking for "Player"
- Add an If unit
8 Saving Coin Count
Use Saved Variables to persist the coin count across scenes:
- Go to Saved variables tab in the graph
- Add a new variable: "Coins" (Integer, default 0)
- Drag the variable into your graph
- Add an Add unit (set B to 1)
- Hold Alt and drag the variable to get Set Saved Variable
- Connect the incremented value
Destroying the Coin
- Add GameObject Destroy
- 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