Creating a Network LAN Game in Unity

Creating a Network LAN Game in Unity

Single player games are fun but eventually gamers will expect some multiplayer functionality within your gaming experiences. This is thankfully fairly simple to set up in Unity and by adding a few components you can create some wonderful multiplayer experiences.

For this example I am using a simple plane with a dirt ground texture mapped to it and some fog effects for atmosphere. The Player character is a green car model with a player controller script attached (this uses simple transforms to move the car using the arrow keys) and a Rigidbody and box collider attached to interact with the environment.

The first component that needs to be added to your player character is the Network Identity. This component allows your player character to have a unique ID in the game environment so that the game can keep track of it. Each new player character in the mulitplayer game will have it's own unique instance. I have ticked the Local Player Authority checkbox to give the player authority over the locally spawned server instances.

The second component that should be added to the player character is the Network Transform component. This component controls the position of the player object in the game as it moves around. This ensures that the movement is transferred between the client and the various servers that make up the network topography.

To make this a networked application we need to create a Network Manager, this manager will convey all the messages to the client and server in our game. Create an Empty Game Object in the Unity Hierarchy. Call it NetworkManager - it doesn't matter where it is in the scene. Add a Network Manager component to this object. In this component place your player object, in my case the car into the Player Prefab slot. This will spawn your player character once the network begins running.

Also add the Network Manager HUD to your Network Manager. This will create a very basic GUI so that we can manage the various Network options available.

When you press Play in the Unity game editor, it will immediately load the GUI for the Network Manager HUD. This contains the default Unity buttons and show a variety of options on how to manage this instance of your game.

You can create a LAN HOST which makes this instance both a server and a client. You can create a server by itself with the LAN SERVER ONLY option, this means that you won't be able to control any of the players. You can create just a client with the LAN CLIENT option, in order for this to run you will need a server to be running as well.

Begin your test with a LAN HOST, this will create a server instance and you should be able to see your player character. This ensures that the Network Manager is working correctly. In order to test the network connectivity, Build and Run an instance of the game.

Playing your Network Game

  1. Open the executable file of the game you have just built.
  2. Press Play on the Unity editor
  3. Select the LAN HOST option in the Unity editor (this will begin a server and client instance, spawning a player in the scene)
  4. Take a note of the IP address of the server (in my case it's the LOCALHOST as I am running this example on the same PC)
  5. On the executable instance of the game enter the IP address of the server in the LAN CLIENT option.

You should then see the two cars appear and be able to control each of them on screen.



Do these components come with any plugin or external package?

Like
Reply

To view or add a comment, sign in

More articles by Dr Chris McAuley

Others also viewed

Explore content categories