Mirror Quick Start Project
Written by JesusLuvsYooh / StephenAllenGames.co.uk, edited by James Frowen
Last updated
Written by JesusLuvsYooh / StephenAllenGames.co.uk, edited by James Frowen
Last updated
This guide currently shows you:
It is best to first make a mini practice game before converting your single player game, or creating your ideal brand new multiplayer.
The Pre-made Mirror examples are great for using as reference, it is recommend to use them regarding connection setup, with ports and firewalls. This can be a huge topic that changes from person to person, and is not covered in this guide, here we will use localHost (multiple games on same PC). End Result:
Blank Project, import Mirror from Asset Store.
Create new scene, save it, and add it to build settings
Create a new GameObject, name it NetworkManager in the scene, and add these 3 components
NetworkManager
KCPTransport (TelepathyTransport is older, you do not need KCP and Telepathy)
NetworkManagerHUD
On the NetworkManager component, drag your Offline and Online scene into the slots, we have only one scene for now, so put your scene in both
The scene must be in the build settings before dragging it to the field
Setup the scene
Add a simple Plane floor with:
positions (0, -1, 0)
scale (2, 2, 2)
(optional) add a material to this, I added one called dirt that is used one of mirrors examples
Next we add a GameObject, name does not matter
Add NetworkStartPosition
component to this GameObject
Duplicate the GameObject a few times, and scatter around your scene floor so that you have multiple spawn points. I did 4, one near each corner
Creating the player
Create a capsule using the menus as shown in the image
Attached a NetworkTransform component, this will auto add a Network Identity
Tick Client Authority on the NetworkTransform
(Note: Newer Mirror versions use "Sync Direction" set this to "Client To Server".)
Rename that object Player
Add an empty PlayerScript
Drag into Project to create a prefab
Then delete Player from scene
Drag your player prefab into Network manager,
Set spawn method to Round Robin.
Add the following to your PlayerScript.
Press play in Unity editor, and then Host (server + client) button in the game window. You should be able to move around with a first person view capsule.
Build and run your scene, open it, host on one, and press the Client button on the other. Congrats you made a mini multiplayer game!
Player name above heads
Inside your player Prefab, create an empty GameObject
name it something like FloatingInfo
position Y to 1.5
scale X to -1
Inside that FloatingInfo
, create a 3D text using Unity menu (GameObject - 3D Object - 3D Text),
Set it up as shown in the picture below
Update your PlayerScript.cs with this:
Add the PlayerNameText
and FloatingInfo
objects into the script on the player prefab, as shown below.
Now if you build and run, host on one, join on the other, you will see player names and colors synced across the network!
Well done, 5 stars to you!
A scene networked object all can access and adjust.
Create a SceneScript.cs, add it onto an empty GameObject in the scene called SceneScript.
Then create a Canvas with text and button, similar to below.
Add the sceneScript variable, Awake function, and CmdSendPlayerMessage to PlayerScript.cs Also add the new playerName joined line to CmdSetupPlayer();
Add this code to SceneScript.cs
Attach the ButtonSendMessage function to your Canvas Button.
Attach Canvas Scene Text to SceneScript variable.
ignore SceneScript’s, playerScript variable, it automatically sets this!
Attach a NetworkIdentity component to the SceneScript gameobject, if it has not automatically done so.
Now if you build and run, host and join, you can send messages, and have a text log for actions!
Wahooo!
Experiment and adjust, have fun!
Weapon switching! The code bits.
Add the following to your PlayerScript.cs
Add the weapon switch button in update. Only local player switches its own weapon, so it goes below the !isLocalPlayer
check.
Weapon models
Add the basic cube weapons first, change these later.
Double click your player prefab to enter it
Add a "WeaponsHolder" empty GameObject, with position and rotation at 0,0,0.
Inside that GameObject, create a cube from unity menu, (GameObject, 3D object, cube)- Remove the box colliders.
Rename this Weapon1
, change position and scale to match the below pictures.
Duplicate weapon 1 for a Weapon 2, and change its scale and position, now you should have 2 different looking ‘weapons’!
Weapon switch finale.
Add these 2 GameObjects to your PlayerScript.cs weapons array.
Disable weapon 2, so only weapon 1 shows when spawning.
Build and run!
You should see each player switching weapons, and whatever your player has equipped, will auto show on new joining players (sync var and hook magic!)
Here we will make a small adjustment, as using a GameObject.Find() may not guarantee Network Identity scene objects are found. In the image below you can see our NetworkIdentity scene object gets disabled, as they are disabled until a player is in ‘ready’ status (ready status is usually set when player spawns).
So our chosen workaround is to have our GameObject.Find() get the non-networked scene object, which will have those Network Identity scene object as pre-set variables.
Create a new script called SceneReference.cs, and add this one variable.
Open up SceneScript.cs and add the following variable.
Now in your Unity scene create a gameobject, name it SceneReference, and add the new script. On both Scene gameobjects, set the reference to each other. So SceneReference can speak to SceneScript, and SceneScript to SceneReference.
Open up PlayerScript.cs and overwrite the Awake function to this:
Menu and Scene switching, here we will go from an offline Menu, with a play button, to a Games List with a back button and the Host/Join HUD, to your online map, and then a second map for host to switch to.
Open up SceneScript.cs and add the following function.
Duplicate your previous Canvas button, rename it and reposition it, then setup the OnClick() to point to SceneScript.ButtonChangeScene, like in the image.
Then drag your NetworkManager into your Project, to make it a Prefab, this way any changes we make later will apply to them all. If you haven’t already, you can sort out your project into folders, one for scripts, prefabs, scenes, textures etc. :)
Save, and then Duplicate your MyScene, rename to make a Menu, GamesList and MyOtherScene, then add them to the build settings, with Menu being first.
Open up the Menu scene, remove the spawn points, SceneScript, SceneReference, Network Manager and Plane, so it looks like the below. Adjust the canvas button to say Play, centre it. Here is where you could add the Scores scene, Contact section, News, etc
Create a Menu.cs script, add it onto a Menu gameObject.
Add the code to Menu.cs, then in the Button, drag the Menu gameobject into the On Click () and set it to Menu.LoadScene, like in the picture.
Open up GamesList scene, do similar to Menu but KEEP NetworkManager prefab.
Create a GamesList.cs, add the code, and add it onto a GamesList gameobject in the scene. Adjust a canvas button to say Menu (this is our back button). It should look like the image below.
The games list is where you can add List server contents, or matchmaker, or just the host and join buttons, similar to the default NetworkManagerHud, for now leave this. :)
Open MyOtherScene, this is our second map. Change the camera background colour and floor material (or anything, just so you can see both scenes are different. To summarise, MyScene is map 1 and MyOtherScene is map 2.
In your NetworkManager prefab in PROJECT (not the one in scenes), add Menu to offline, and MyScene to Online variables. This should change all the NetworkManager prefabs to have these settings.
Build and Run, press Play on the Menu to go to GamesList, then click Host (for player 1). For player 2, press Play on Menu, then client connect on GamesList.
Now the host can change scenes between map 1 and map 2, and if anyone disconnects or stops the game, Menu scene is load to start again. This whole process can be tidied up, but should provide a good scene switch template to your Mirror game :)
Here we will add basic weapon firing, using rigidbody prefabs. Raycasts with a representation of the fired object is usually better to do this, and keep phycisal objects for things like Grenades and Cannon balls. This section will also lack a lot of security and anti-cheat techniques in order to keep the guide simple, but anyway, here we go!
Double click the Player Prefab to open it, create empty gameobjects and line them up with the end of your weapon, add them as child to each weapon. Some weapons may be short pistols, others long rifles, so the place where objects spawn will be different.
Create a Weapon.cs script, add it to the Weapon1 and Weapon 2 gameObjects inside the player prefab.
Now back in your scene we shall make 2 bullets, in Unitys menu, go to GameObject, 3D Object, Sphere. Add rigidbody to this sphere, make the scale 0.2, 0.2, 0.2, then save it as a Prefab in the Project. Do the same with a cube, so you have two different looking bullets.
Inside your player prefab again, select a weapon, and set the variables on weapon script.
In SceneScript.cs, add this variable and function.
Enter MyScene (map 1). Duplicate the Canvas StatusText, rename to Ammo, then drag that Ammo text UI into SceneScript gameobject, canvasAmmoText variable. Do this on BOTH MyScene (map 1) and MyOtherScene (map 2), as we have not yet linked or prefabbed our canvas and scene scripts to auto update changes on each map.
Open up PlayerScript.cs, add these two variables:
In the ‘OnWeaponChanged’ function, update it with the new line, so it should look like this.
In Awake(), add this at the end:
In Update(), add this at the end:
Add these two functions after the Update() {} function finishes.
Build and Run, you should have firing, with different speeds and cooldowns on all players :)