0% found this document useful (0 votes)
76 views

Creating A Multiplayer Game Using Mirror

The document provides steps to create a basic multiplayer game using the Mirror networking package in Unity. It describes setting up a Network Manager, spawning players, and adding scripts to synchronize an interactable object's color and shape between the host and client players.

Uploaded by

suravillajohnrey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Creating A Multiplayer Game Using Mirror

The document provides steps to create a basic multiplayer game using the Mirror networking package in Unity. It describes setting up a Network Manager, spawning players, and adding scripts to synchronize an interactable object's color and shape between the host and client players.

Uploaded by

suravillajohnrey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Creating a

Multiplayer Game
using Mirror
First let’s setup a Mirror to be use in creating multiplayer
game, by opening unity and create a new project

Go to Unity Asset Store and search Mirror. Open it in unity and


download it
Click import to import the package into your project and wait
until finished

The package is ready to be use in your project. We can now


start creating basic multiplayer game.
In the project hierarchy create a 3D objects like plane that will
served as a ground, and a capsule that will serve as player

You can add color to it if you want, by creating material in the


project asset folder and drag it to the object you’d like to color
In player create a simple Player Movement script
Simple code:
After that we will add additional script called Camera Follow so
that when you hit play the camera will follow the player
Simple code:
Add additional lines of codes to the player movement
using Mirror;
using UnityEngine;

public class PlayerMovement : NetworkBehaviour


{
[SerializeField] private float speed = 5f;
private Camera mainCamera; // Reference to the main camera

void Start()
{
// Find the main camera in the scene
mainCamera = Camera.main;
}

void Update()
{
if (!isLocalPlayer)
return;

// Get input from horizontal and vertical axes


float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");

// Calculate movement direction based on camera's forward direction


Vector3 moveDirection = mainCamera.transform.forward * vertical + mainCamera.transform.right *
horizontal;
moveDirection.y = 0f; // Ensure movement stays on the horizontal plane
moveDirection.Normalize(); // Normalize to ensure consistent speed in all directions

// Move the player


transform.Translate(moveDirection * speed * Time.deltaTime);
}
}
Hit play to see if it works

You can modify those codes according to your game desires


After that prefab the player
Now we will create a Network Manager by creating an empty
object in the hierarchy and rename it “”Network Manager”

In the inspector add these following components to the game


object Network Manager
In network manager component just drag the network
manager game object you created in transport field

Also drag the player in the player prefab field


Create another game object in the hierarchy and name it
spawn point

Add component to it called network start position


Duplicate the spawn point Game Object to add another spawn
point for the other player

Position the spawn point anywhere you want


Now for the Logic of the game we will create a simple game
concept that the host player and the client will interact with

Add a cube in the game


Now create a script for the game mechanics

The mechanic is simple only we will set the player to interact


with the cube by clicking the space button. The host player can
change the cube color while the client player will change the
shape of the cube
First let’s make a script name in “InteractableObject”
Defining Script Variables and Imports
At the top of the script, add the necessary using directives:

Define the InteractableObject class, inheriting from NetworkBehaviour:

Adding SyncVars for Object Properties


Inside the InteractableObject class, define two SyncVars for the color and
shape index:
Initializing Renderer and Applying Properties
Define a private Renderer variable to hold a reference to the object's renderer:

In the Start method, initialize the objectRenderer variable and apply the
initial properties:
Handling Property Changes with Hooks
Implement hook methods to handle changes to color and shape properties:
Changing Object Shape
Implement the ChangeShape method to update the object's shape based on the
shape index:
Synchronizing Property Changes Across Network
Implement methods to change color and shape on the server and client:
Implementing Remote Procedure Calls (RPCs)
Implement RPC methods to synchronize color and shape changes
across clients:
Attach the script to the cube and also add a Network Identity
component to it
In the Interactable Script change the Sync Direction to
“Client To Server”
Now to test it, at the top bar of unity you’ll see a parrel sync
click it and choose clone manager this section will appear. Add
a new clone, this will clone your unity projects and open
automatically
Your project and the clone must be open at the same time to
test it. Now hit play on both project and choose host to the
first project and client to the clone project.
In host server when clicked space it will change the color of the
cube.
In client server the shape of the cube change.
That’s it. You’ve completed all the necessary steps in creating
multiplayer game using Mirror.

Thank You!

You might also like