2D Deep Dive

Lets dig in to the FSMs and see how everything works!

The Hierarchy

PlayMaker Photon Proxy

This GameObject will move from scene to scene, as long as you have it in the first scene where you make your initial Photon Connection. This is a prefab that comes from the Playmaker Ecosystem PUN 2 package. In a blank project, you could just search for it in your assets folder (assuming you've installed the PUN 2 Playmaker Package from the Ecosystem already).

One tip I can give you about the PlayMaker Photon Proxy, is to turn off the 'Show State Labels' in the 'Debug' section of the FSM tab in the PlayMaker editor window. For some reason its checked by default in this prefab. So, you'll need to right-click the PlayMaker Photon Proxy in the hierarchy, choose Prefab>Unpack Completely. In the Playmaker Editor, click on the 'FSM' tab, scroll down to 'Debug' and uncheck 'Show State Labels'. Otherwise you may find a random grey square on your screen that appears and not know where it comes from.

PUN2 Setup Canvas

The canvas has a 'Canvas Scaler' component on it. I always set my 'UI Scale' mode to 'Scale with Screen Size'. I set my target resolution to 960 x 600, because its a good fit for webGL, but you can use whatever target resolution you prefer.

The Canvas has three different panels on it. Consider each panel a different 'menu' that becomes active based on the UI selections made by the player. If you've never used layout groups, be sure to check out the layout group components on the inspector for each panel. Layout groups are a great way to keep your UI pretty and distributed.

  • Panel Public or Private

  • Panel Private Room

  • Panel Public Room

Photon Connection Manager

This GameObject is where I put the logic to manage connecting to the Photon Servers. If you've never used FSM Templates before, you may wonder why the FSM Editor looks blank. Be sure to check out my YouTube tutorial on using FSM templates.

Usually, I'll make an FSM a 'template' if I'm going to use it on several GameObjects in my project. But, this FSM simply makes the network connection. It will not be on any other GameObjects. So, why make it a template? By making an FSM a template, it is saved in the following project folder Assets>Playmaker>Templates. You can import it into another project, and use it again without needing to rebuild it.

If you decide to use this template in other projects, I've exposed all the important pieces as variables in the inspector. You can just drop in all the required pieces, and it should work.

What are all these pieces?

  • Environment_Parent: This holds all the environment pieces from the scene.

  • Game Canvas: This holds the scoreboard.

  • miniMapCamera: This isn't used for the 2D Scene.

  • privateKeyInputPlaceholderText: When using private rooms, this field is used to display some debug information.

  • publicRoomCountMax: How many players do you want to support in one room?

  • publicRoomPanel: This is the public room panel (to be activated/deactivated when needed).

  • privateRoomPanel: This is the private room panel (to be activated/deactivated when needed).

  • PUN2 Setup Canvas: We store this GameObject so we can deactivate it when we are done with it.

-----Player Instantiation Settings-----

  • nameForLocalPlayer: Naming the local player GameObject makes it easier to find in the hierarchy when testing.

  • playerPrefab: Your prefab that gets spawned for each player.

  • playerSpawnPoint: GameObject where you want your player to spawn (optional).

  • playerSpawnPosition: World Position where you want your player to spawn (optional).

  • playerSpawnRotation: Do you need to rotate your player when they are spawned?

-----Private Room Panel-----

  • privateJoinButton: What button does player click to join private room?

  • privateRoomPanel: The panel that houses private room UI.

-----Public or Private Panel-----

  • privateRoomButton: The button to move to the private room panel.

  • publicOrPrivatePanel: The panel that houses these buttons.

  • publicRoomButton: The button to connect to public room.

-----Room Settings-----

  • roomKeyInputField: The input field where the player types the private room name they want.

GameManager

The GameManager is an empty GameObject holding a single FSM. When a Player collects a 'collectible', the GameManager updates their score on the scoreboard.

The Start state Sets the GameManager as a Global GameObject variable so it can be easily referenced from the FSMs on other GameObjects.

When a 'collectible' is picked up, it sends the GameManager/Collectible RPC along with a string the actor number of the player who picked up the collectible. There is a string switch that gives a point to the player corresponding to the actor number.

Environment

This is an empty GameObject I used to hold most of the GameObjects that don't have logic on them. The background, ground, platforms, and even the Camera are all housed in this GameObject. Its not required to do this, but helps my hierarchy look cleaner.

GameCanvas

This canvas has the scoreboard on it. There is also an FSM that activates rows on the scoreboard if more players enter the room.

Collectibles

This is an empty Parent GameObject to keep all my collectibles organized in the hierarchy.

The Player

For all the samples, the player is located in the Assets/Resources folder. For the 2D Sample, we use 'PlayerPrefabForMultiplayer_2D'.

What's On the Player (Check the Inspector)?

  • Sprite Renderer-This is the pink 2D Capsule.

  • Capsule Collider 2D-The keeps the player from falling through the ground.

  • Rigidbody 2D-This allows us to use physics on the player.

  • PhotonView-This is required for any GameObject that is observed by the network.

  • PhotonTransformView-This allows the position and rotation of the player to be synced across the network for other players.

  • Movement FSM-This has the logic to move the player.

  • Player Look Direction FSM-This has the logic to make the player sprite face the position of movement.

  • Player Input-This is a required component to use Unity's new input system.

  • Play Maker Photon Game Object Proxy-This is required to reference an object across the network with a PlayMaker FSM.

Movement FSM

The Movement FSM is well documented within the FSM itself.

If you want to adjust the default jump velocity, or movement speed, you can do so right in the inspector.

Player Look Direction FSM

This FSM is pretty well documented within the FSM itself.

Player Input

When you add the 'Player Input' component to a player, you need to add an 'Input Action Asset' in the 'Actions' field. In this sample, I already have an 'Input Action Asset' chosen. I didn't have to make it from scratch though. When you import the 'Input System' from Unity's package manager, it comes with some default 'Input Action Assets' you can use and/or modify.

The Collectibles

In the 2D Sample, we are using the 'CollectiblePrefab2D' Prefab.

The Parent Object has three components worth noting:

  • Sprite Renderer-This is the image of the Googly Eye VR Headset.

  • Box Collider 2D-This has it's 'is Trigger' set as true.

  • Rotate Sprite FSM-The logic that makes it spin, and play it's particle system when picked up by the player.

Rotate Sprite FSM

As with all the other FSMs, this FSM is well documented within itself.

Last updated