Multiplayer Time!

Joseph Ewert
3 min readJul 23, 2023

--

Ever just learn something cool and just want to talk about it? That’s me today with Unity Netcode.

We’re gonna need this package first to start.

Next, make an empty game object. I named mine aptly “NetworkManager” but something like “ConnectionManager” or “MultiplayerManager” might work as well. We need to add the Network Manager component added with our new package to it though, so Network Manager it is!

Next, we need a Player Prefab with its own camera. Make a random sprite or object (doesn’t matter what, but I’m in 2D mode so I made a sprite). Add the existing camera to the object we just made and prefab it.

Add these objects to our player prefab. We need a network object, so we define as an object that will be synced on the network. Add network object, so the transform is synced over the network.

Let’s set up our network manager next. Set Select transport to Unity Transport.

Give the player prefab field the square we made.

Next, we need some logic to join the server.

We need an empty object to hold our join script.

The bool was there so I knew the script saved when I wrote it.

That’s the entire script. We are going to call the network manager, the only instance of it, and tell it to start the client.

You may have noticed the button at the start, and yes thats next.

Make a generic button. Make sure to place it so its visible on the canvas. Unity will be mad at us right now since there is no camera. Tell the button to call the Join server command from the Joiner script (this is why it needed to be public).

Save and build the project. In the Unity editor, start the game once done.

In the editor, click start host. We could make a button for this, but my goal is the camera trick. Open our build, and click the button we made.

I grabbed a random photo off of my PC so we could see the camera move.

Once done, it should look something like this. If you manually move the camera, you’ll notice only one moves. It’s still pretty jank of course, it has no code telling it what to do or to operate smoothly. But! Each player ends up with their own camera this way.

--

--

Joseph Ewert

Hello everyone! Stick around and learn something new about Unity! I also have a portfolio page at https://squirreltalegames.com