This course will cover how to synchronize players in multiplayer games using Unity, allowing each client to control only their player character. The course will start with the basics of network programming, explain Unity’s networking system, and discuss the client-server architecture. Additionally, we will learn how to manage and synchronize the input of each player.
1. Basic Concepts of Unity Networking
Unity provides powerful features that make it easy to develop multiplayer games. To understand the network system, you need to grasp a few basic concepts.
1.1 Client-Server Architecture
The client-server architecture is a crucial concept in network games. The client refers to the instance of the game that a player can use, while the server is the central system that manages the state of all clients.
- Server: Manages all game progress and sends information to clients.
- Client: Processes player inputs and progresses the game based on information received from the server.
1.2 Network Synchronization
It is essential to synchronize the states of player characters or objects in the game. That is, the same information must be provided to all clients to achieve smooth gameplay.
2. Installing Unity and Creating a Project
First, you need to install Unity and create a new project. Follow these steps:
- Install Unity Hub.
- Download and install the desired version of Unity.
- Click the ‘New Project’ button in Unity Hub and select either a 3D or 2D project.
- Choose a name and a save location for the project, then click the ‘Create’ button.
3. Setting Up the Networking Package
There are various networking libraries available in Unity, but in this course, we will use Unity’s MLAPI (Mid-Level API). Follow the steps below to install MLAPI.
- Open the Package Manager (Window > Package Manager) and click the ‘+’ button in the top left corner.
- Select ‘Git URL’ and enter
https://github.com/Unity-Technologies/Mirror.git
, then install it.
4. Basic Network Settings
After installing the networking package, you will need to set up the basic network settings. Follow these steps:
- Right-click in the Hierarchy view to add a NetworkManager object.
- Add the scenes you want to use in the NetworkManager’s settings menu.
- Add NetworkManagerHUD to set up the basic UI.
4.1 Configuring the Network Manager
Set up the NetworkManager to manage servers and clients. To achieve basic functionality, you need to be aware of the following settings:
- Maximum Connections: Set the maximum number of clients that can connect.
- Network Port: Set the port number that the server will use.
5. Player Settings
You need to set up each player’s character. Since the player character is directly controlled and needs to be synchronized with other clients, it should be created as a prefab.
5.1 Creating a Player Character Prefab
- Create a player character as a new 3D object (e.g., Cylinder).
- Add a Rigidbody component to the game object for movement and rotation.
- Create the above object as a prefab and save it in the Resources folder.
6. Writing the Player Control Script
Now, let’s write a script that will control the player character. Refer to the code below to implement the PlayerController.cs script.
using UnityEngine;
using Mirror;
public class PlayerController : NetworkBehaviour
{
public float speed = 5f;
void Update()
{
if (!isLocalPlayer)
return;
float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement);
}
}
6.1 Script Explanation
The above script constructs a structure for controlling the player using basic WASD/arrow key inputs. It checks isLocalPlayer
to ensure that only the player character of the current client can move.
7. Synchronizing Players Over the Network
The next step is to ensure that all player characters communicate with the server and synchronize. We will use SyncVar and Command to synchronize states over the network.
7.1 Setting Up SyncVar
SyncVar is used to synchronize variables over the network and automatically reflects any changes that occur on the server to the clients.
public class PlayerController : NetworkBehaviour
{
[SyncVar]
public Vector3 position;
void Update()
{
if (!isLocalPlayer)
return;
float moveHorizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float moveVertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
position += movement; // Update the local position
CmdUpdatePosition(position); // Command to update the server
}
[Command]
void CmdUpdatePosition(Vector3 newPosition)
{
position = newPosition; // Update the server's position
}
}
8. Conclusion
In this course, we learned how to synchronize players in Unity and allow each player to control only their character. By setting up a basic network environment and scripting the players, we established the foundation for multiplayer games in Unity.
In future courses, we will add more features and increase the complexity of the game, covering more advanced topics. If you have any questions or feedback, please leave a comment!