What the course offer
My course will teach you how to build multiplayer games from scratch, feel free to try the games I build first before deciding if you want to learn how to build it from scratch. I have published my multiplayer games titled "multiplayer space shooter" on Apple Appstore, Google Playstore, Newgrounds and Kongregate games portal.
I designed this multplayer games programming course to be easily understood by absolute beginners. You just need to follow step by step to complete your games and I will even provide you with some 3D modelled spaceship created by me.
I will start by teaching you how to setup the network server and the background, then I will show you how to add in each object and controlling them using the script, finally I will show you how to smooth the game with various technique and also testing the game in multiplayer mode to make sure everything work correctly. Please give it a try and you wont be disappointed.
My name is Freddy Phu,and I am going to teach you how to build a multiplayer games from scratch.
My finish multiplayer game can be found on Apple Appstore, Google Playstore, Newgrounds.com and Kongregate.com, just search for "multiplayer space shooter" and you can try it out before decide if you want to learn how to build it from scratch. Player can play together from different platform so that once you build the game you can publish it anywhere.
Thanks and hopefully you will join my course.
1. Open Unity
2. Create New Project
3. Import Photon Unity Networking package from the Unity Asset Store
4. Create free account at www.photonengine.com
5. Create new real time app at the www.photonengine.com
6. Copy the App ID and paste into your Unity Photon Server Setting
The Photon Engine allow you to create unlimited games with up to 20 concurrent user at the same time. If you want to upgrade to allow more player you can upgrade later. By using Photon Engine you do not need to host your own server and can start building multiplayer games for free. Your games can later be published into Iphone appstore, Android marketplace and even Web and players who login in 3 different places can play together.
1. Save the Scene
2. Creating empty object "networking"
3. Create new C# script "networking"
4. Connect to photon network using syntax PhotonNetwork.ConnectUsingSettings ("1.0.0");
5. Once you connected, make sure tick the "automatic join lobby" on the Photon Network setting
6. Once you joined lobby, join a random room using syntax PhotonNetwork.JoinRandomRoom()
7. When no room found, create a new room using syntax PhotonNetwork.CreateRoom (null)
8. Show the status on function OnGUI using syntax GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ())
9. Attach your networking script to your empty object "networking", and finally click run
10. You should see the status, join server->join lobby->create room->successfully join room
You should use the version control to control who can play with who, let say your game in Iphone already update to version 2.0 but your game in Android and Web still 1.0 so Iphone player should only play with Iphone player but not with the others.
1. Create a Plane
2. Rotate camera 90 degree to show the plane
3. Add a photo to the newly created material
4. Attached the material to the plane
5. Adjust the camera to show the background nicely
1. Download spaceship1.fbx.zip under this lecture
2. Unzip the zip file into spaceship1.fbx and copy Spaceship1.fbx into your project folder
3. Pull the spaceship from your folder inside Unity into the scene
4. Adjust the camera and plane so that the spaceship look nicely in the game
5. Create a new script call "spaceship"
I created this spaceship using a free 3D modeling software "Blender"
1. Write the script in spaceship.cs that will control the spaceship
2. Add rigid body to the spaceship
3. Attach your script to your spaceship
4. Make sure your spaceship position is set at x= 0,y= 0, z=0
5. Modify the 5 public variable so that your spaceship move within the boundary of the screen
X Min: Example -1.2
X Max: Example 1.2
Y Min: Example -2
Y Max: Example 2
Speed: Set this to control your spaceship movement speed
1. You can control your spaceship using arrow key or WASD.
2. Rigid body is the Unity physic and gravity simulation, so when you attach it to the spaceship when you push the spaceship it will accelerate first then slowly decelerate, when it collide with other object it will also bounce off automatically base on the object weight.
3. When you play the game, you can always check the X,Y coordinate of the spaceship so you know the value of X and Y for you the set the boundary
1. Prefab is a 3D object template that you can instantiate into the game.
2. Example: Spaceship is the prefab.
- You then instantiate the spaceship into the game base on how many player, if three player join the game you should instantiate three times to create three spaceship.
3. Prefab must be placed into the "Resources" folder, otherwise it will not work, however, the "Resources" folder can be put under any folder.
4. Prefab must have the script "Photon View" attached to it otherwise it will not work
1. Drag your spaceship into the "Resources" folder
2. Add "Photon View" script to your spaceship prefab
3. Drag transform into "Photon View" -> Observed Component
4. Instantiate your spaceship using the syntax :
PhotonNetwork.instantiate("spaceship1", new Vector3(0.0f,0.0f,0,0f), Quaternion.identity,0)
5. To build the EXE file, click File -> Build Setting -> Platform(PC, MAC or Linux) Standalone -> Build
6. Build two EXE, one for player 1, one for player 2 and test the game to see if it sync
Problem at this stage:
Problem 1: You should notice by now, if you press the arrow key as player 1, both ship will move, this is because both spaceship have the same template and same code, so when you press arrow key to move left, both ship will also move left
Problem2: Both ship seem to flicker a lot and not moving very smoothly, it seem like it will jump from one place to another
Both problem will be solved in the next tutorial
1. Untick the spaceship script on the prefab so by default no one can control the spaceship
2. Modify the networking script so that once you instantiate the ship, only then you enable the script back using syntax
GameObject mplayer = (GameObject)PhotonNetwork.Instantiate ("spaceship1", new Vector3 (0.0f, 0.0f, 0.0f), Quaternion.identity, 0);
Let say player 1 and player 2 play this game
When player 1 create the game he will instantiate spaceship 1 and then enable spaceship1 script, when player 2 join the room, player 1 do not instantiate the spaceship2, player 2 will instantiate the shapeship2, so inside player1 game, spaceship1 script is enabled but spaceship2 script is not enabled. So player1 can only control spaceship1 but not spaceship2.
Remember last time our space ship -> Photon View script -> Observed component, we drag the transform into it?
This mean we want the PhotonNetwork to keep track the position and rotation of the spaceship and send it across the network to other player. However, doing this is inefficient and lagging. This is because our ship move way too fast. Example, let say our ship movement
seconds 0.1 x= 0.0 y=0.0
seconds 0.2 x= 0.1 y=0.1
seconds 0.3 x= 0.2 y=0.2
seconds 0.4 x= 0.3 y=0.1
within 0.4 milliseconds our ship move to upper right then move to lower right, however the network cannot send data fast enough, the network might only have data such as
seconds 0.2 x=0.1 y=0.1
seconds 0.4 x=0.3 y=0.1
while some data have loss, so it will show the other player ship keep flickering and look like its teleporting from place to place in a random fashion.
We need to get other player coordinate then we tell our computer to move the other player ship from current position to the new position slowly and smoothly, so that the entire thing look smooth and no flickering
1. Add a new script "networkcontrol.cs"
2. Drag the new script to the spaceship prefab. Then drag again the networkcontrol script from the spaceship prefab to spaceship -> photon view script -> observed component
3. Inside the script "networkcontrol.cs" we overwrite the function void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
(this is where we just want the position and rotation from other player, and also we send our position and rotation to other player)
4. Inside the script "networkcontrol.cs" we also add some code to the update() function
- Where we check is this spaceship ours? if yes, do nothing, if no, then we get the new position of the spaceship and slowly move it from current position to the new position using the Vector3.Lerp.
5. Inside the script "networkcontrol.cs" we need to make sure the class is inherited from Photon.MonoBehaviour instead of MonoBehaviour
6. Make sure the spaceship prefab -> rigidbody -> gravity is off
7. Build the game into EXE and test 2 player
1. Create a sphere and name it bullet
2. Add rigid body to the bullet and off the gravity
3. Create a new C# script "bullet.cs" and attached it to the bullet
4. Inside the script "bullet.cs" , write the code inside update() function so that the bullet will destroy itself after 5 seconds
5. Create another C# script "shootonly.cs" and attached it to the spaceship
6. Inside the script "shootonly.cs" , write the code inside update() function so that you instantiate the bullet base on the fire rate, example if fire rate = 1, mean you instantiate the bullet every 1 seconds.
7. Drag your bullet into the resources folder so that it become the prefab.
8. Drag your bullet prefab into the spaceship -> shootonly script -> Shot
9. Create an empty object called "shotspawn" under your spaceship, and drag the shotspawn to the spaceship -> shootonly script -> Shotspawn
10. Modify your "bullet.cs" and modify the start() function to make the bullet going forward base on the speed variable
1. Download the enemy ship, unzip it and drag it into your project
2. Add a box to your enemy ship and make it transparent, the box will have box collider
3. Add photon view script to your enemy ship
4. Add rigid body to your enemy ship, then on the rigid body, freeze the enemy ship rotation x,y,z
5. Add network control script to your enemy ship then drag the network control script to the enemy ship-> photon view-> observed component
6. Create a new C# script "enemy1.cs" and attach it to the enemy ship
7. Edit the "enemy1.cs" on the update() function to move the enemy ship to a random position
8. Set the zMin, zMax, xMin, xMax in enemy ship -> enemy1 property so that the enemy ship do not fly out of boundary
1. Add a cube under enemy ship and resize it, make it red, and rename it health.
2. Inside "enemy1.cs" script, add a new function gethit(), if enemy ship getting hit, decrease life and destroy the enemy ship, make sure to make the gethit() function [RPC] so that every player will execute the gethit() function
3. Inside "bullet.cs" script, add a new function OnCollissionEnter(), to detect what the bullet hit, if the bullet hit the enemy ship, call the enemy ship gethit() function
4. Add the box collider to the enemy ship (make sure you remove the previous box under the enemy ship)
5. Inside "enemy1.cs" gethit() function, make sure the lifebar get local scale base on the life of the enemy ship
6. Inside "bullet.cs" script, make sure once the bullet hit the enemy, it will destroy itself
1. Drag enemy ship into resource folder to make it as a prefab
2. Inside "Networking.cs" , inside the update() function, check if its master client, then instantiate the enemy ship every 10 seconds
3. To shoot faster, adjust the firerate of your spaceship from 1 to 0.2
1. Assets-> Import package -> particle system -> import
2. Copy the explosion prefab into your resource folder
3. Inside "enemy1.cs" script, right after the enemy ship is dead, instantiate the explosion
4. Add photon view script to the explosion prefab
5. To make the explosion smaller, adjust the explosion force to 1 and multiplier to 0.1
1. Create a 3D object -> Capsule
2. Copy laser texture into your project folder
3. Create new material and attach your laser texture into it
4. Then attach your new material to your capsule
5. Rotate the capsule y:90, z:90
6. Set the capsule radius to 0.1 and height to 1.1
7. Attach bullet script to the capsule
8. Add rigidbody to your capsule, freeze the rigidbody rotation and off the gravity
9. Rename your capsule to "laserbullet"
10. Drag your laserbullet to the Resources folder to make it as a prefab
11. From the Resources folder, drag the laserbullet to Spaceship -> shootonly -> Shot
12. Modify the "shootonly.cs" script so that when instantiate the bullet the rotation is the bullet rotation
13. Increase the enemy ship rigidbody mass to 100 so that they wont be push by the bullet
1. Create 2 empty object name "shotSpawn1" and "shotSpawn2" and park it under the enemy ship
2. Create new C# script "enemyshootonly.cs"
3. inside "enemyshootonly.cs" script, instantiate 2 bullet, one shooting from shotSpawn1, another one shoot from shotSpawn2
4. Drag your "enemyshootonly.cs" script to your enemy ship
5. Clone the laser bullet prefab into enemy bullet prefab
6. Create new C# script "enemybullet.cs"
7. Inside "enemybullet.cs", copy everything from bullet.cs paste it inside, modify it to go downward instead of upward
8. Drag your "enemybullet.cs" into enemy bullet
9. Drag enemy bullet to the enemy ship -> enemyshootonly -> shot
10. Drag enemy ship shotSpawn1 to the enemy ship -> enemyshootonly -> shotSpawn1
11. Drag enemy ship shotSpawn2 to the enemy ship -> enemyshootonly -> shotSpawn2
1. File -> Build Setting -> Build the exe
2. You can run player one in Unity and run player two using the exe (or run both on two exe)
3. You can pause the game in Unity and check all the property of the game to check for error
4. If enemy is hurting you too much you can reduce the enemy bullet damage
5. If enemy is generated too fast you can set the spawning frequency on "networking.cs" script
6. Keep testing until you are satisfied with your game
1. In "networking.cs" script, create your start game interface on the OnGUI() function
2. In "networking.cs" script, create a new variable "showgui" to control your screen, example showgui=1 show menu, showgui=2 show the gameplay
3. Make sure you scale the GUI.matrix so that the menu will auto scale base on the screen size
4. Create 2 button, one for start game and one for quit game, when start button is press, you should connect to the server and start the game, when quit button is press you should quit the game
5. You can modify the button text and color using the syntax shown in the video
1. Add 3D text under your spaceship, adjust the size and location to make it look nice
2. Rename the 3D text as "myname"
3. Inside "networking.cs" script, use the GUI.TextField function to let the user key in the name
4. Use the syntax GUI.skin.textField.fontSize to change the font size
5. Inside "networking.cs" script, after successfully joining the room, change the "myname" text mesh to the name that player key in
6. Inside "networkcontrol.cs" script, on the update() function, update the player name for other player so that each player have their own unique name
1. Under your project folder, Asset -> Photon Unity Networking -> UtilityScript , find the script "InRoomChat"
2. Drag the script "InRoomChat" into your networking object
3. Click the align bottom so that the chat appear on the bottom
4. Build the game into 2 exe and test it out
1. Go to spaceship.cs and declare a new variable Public Transform colorship
2. Inside your prefab, drag the child object "ship" into your spaceship component spaceship script colorship
3. Now go to networking.cs to create 3 button in your game to change color for your ship
4. You can use http://www.rapidtables.com/web/color/RGB_Color.htm to find the color code that you want
5. Now you should be able to change your spaceship color by clicking on one of the 3 button you newly created
1. Download Goldcoin.zip and extract it into your game asset folder
2. Drag the goldcoin.fbx into your scene, add rigid body and sphere collider
3. Create a new c# script coin.cs and make the coin rotate randomly on start
4. inside "coin.cs" detect the collision from spaceship and use PUNRPC call to ask the masterclient to destroy the object once collided.
1. Drag the coin into the resource folder to make it a prefab
2. Instantiate the coin inside "enemy1.cs" the part where enemy die
3. add money to the player when spaceship touch the coin
Let say player 1 have 0 money and player 2 have 0 money, when they kill enemy1, only one coin appear on the screen. So if player1 eat the coin first, player1 will have 1 money and player2 still have 0 money. Thats why we need to store the money inside "spaceship.cs" so that each player keep track of their own money.
But to display the money on the screen, we need to store the money inside "networking.cs" because that is the part where we have OnGUI function to display word on the screen. Then inside the "networking.cs" update() function we check every second to make sure the money inside networking.cs equal to the money store in spaceship.cs
1. Download sound.zip and extract it into your project Asset -> sound folder
2. On your spaceship prefab, create new component -> Audio source and drag shooting sound into it
3. On your shootonly.cs script, add audio source and play it when instantiating bullet
4. Create 2 empty gameobject on your scene and create Audio source component for them
5. Rename first gameobject coinsound and drag coinsound into its Audio Source
6. Rename second gameobject explodesound and drag explode sound into its Audio Source
7. inside "enemy1.cs" script, create a GameObject temp and find "explodesound", play it when enemy die
8. Inside "coin.cs" script, create a GameObject temp and find "coinsound", play it when player eat the coin
You can always adjust the sound for each audio source to match your need
I have attached all my source code until this stage on multiplayerspace1.zip, so you can check against your code if anything missing
1. remove the join random room
2. change showgui==2 to showgui==3 and create a new showgui==2 section
3. let player key in username, roomname, and create room
1. Use the GUI.BeginScrollView to create a section where player can scroll through multiple page and click join on each room
2. Test the game in multiplayer mode to make sure everything working correctly
In this tutorial I will show you how to control your spaceship using mouse instead of just keyboard, with this, you can play the game on iphone, android or ipad
Hi, My name is Freddy Phu Tze Pang and I work as a programmer for more than 10 years. During my free time, I like to create computer games for fun. I used to use java as my programming tool to code for android but ever since Unity been launched, I have been using Unity to create games.