9 EASY Steps to create a multiplayer game with Unity & Photon - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to black thumb prods i'm liam in this video i will show you how to transform this small single player game into an online multiplayer game in just nine simple steps when i first started out developing a multiplayer game seemed like a very complicated and daunting task this video will hopefully show you the contrary and give you the knowledge so you can develop your very first multiplayer game without too much hassle before we begin thank you to milano for sponsoring this video and supporting this game creation community they've created one of the most impressive project management tools out there what we love is its incredible simplicity and elegant design while still being filled with depth you drag and drop to do checklists images notes and arrows to organize your thoughts plan ahead and share your vision with the rest of your team there's even dozens and dozens of pre-made templates you can use to get started such as the level design layout rpg dungeon map or even character profile simply use the link in the description to get access to miller notes for free alright so in my project i've got possibly one of the simplest games that you can make it's just a player character that can run around the scene and pick up some coins the character also switches from a idle animation to a run animation and we have a little ui text to keep track of the number of coins that we have collected so far now let's turn this little single player project into an online multiplayer experience so step one is to create a server to host our game any multiplayer game needs to have a server you could think of a server has a messenger one player will do something in the game and then the messenger which is the server will in turn let all the other players know about what that player did we'll be using the very popular multiplayer solution called photon to develop this game and thankfully photon lets us create free servers so head over to photonengine.com and start off by creating yourself an account if you don't already have one once you're signed in head over to your dashboards this is the place where you will see all your multiplayer servers or apps as they call them so let's create a new one we will go ahead and choose photon pan for the photon type and then you can name your application as you wish once you're done just go ahead and click create and that's it your fulton server has now been created step two is to import photon into a unity project and link it up to our photon app so i'll head over to the unt asset store that's assetstore.unity.com once you're on the website just search for photon2 then click on the free version of photon so go ahead and add it to your assets and then open it up inside of unity here you will just need to press this import button once it has finished loading but you'll get prompted to provide an app id or email so quickly go back to your dashboard and then copy the app id from the app that you just created you then just need to paste it in here and click on the setup project button perfect our project and our photon app are now linked together step 3 is to create a loading scene that connects us to the photon server basically before you can play any multiplayer game each player needs to successfully connect to the server so we went ahead and made this simple loading scene that just has a little background image and a text object that says loading now i'll create a new empty game object called connect to server i'll then create an app to this object a new c-sharp script also called connect to server in here we will need to import the photon.pan namespace this will give us access to all the multiplayer tools that fulton provides and then simply inside of the start function we'll call the photon network dot connect using settings function it's as simple as that okay so step four is once the player has connected to the server we want to transition him from the loading scene to the lobby alright so for those who don't know a lobby is the place where players can create and join rooms and a room is basically a private instance of the game so if i wanted to play a game with my brother noah i'll create a room with a secret name and then i'll share that name with my brother so that he can join in and we can play together so we also went ahead and created this simple lobby scene that just has a input field and a button for creating rooms and also an input field and a button for joining rooms okay so inside of our connect to server scripts we want to check when we have successfully connected to our server to do so we'll first need to derive our script from mono behavior pan callbacks this will give us access to the public override voids unconnected to master callback function if you don't already know a callback function is a function that gets called automatically by photon when a certain event happens in this case any code we put inside of this function will get called when we have successfully connected to our server in here we will write photonetwork.join lobby this line will now give us the power to create and join rooms later on let's now create another callback function this time it will be the public override void on joint lobby so once you have successfully joined the lobby we will of course want to go to our lobby scene to do that we will first need to import the unity engine.scene management namespace and then say scenemanager.loadscenelobby ok so to recap the script we are first connecting to the photon server inside of the start function then once we have connected to it we're joining the lobby and once we have joined the lobby we're loading up our lobby scene okay so in unity i'll go to file build settings then i'll drag and drop my new scenes into the scenes to build section now we can head over to our loading scene and press play so after a couple of moments you'll see that we're getting moved to the lobby scene this means everything is working great step 5 is to now actually create and join rooms so inside of my lobby scene i'll create an empty game object called create and join rooms once it has been created i'll add to it a new c drop script also called creating joint rooms first of all i'll import the unity engine.ui namespace this will now let me create two public input field variables one for my create inputs and the other for my join inputs let's now import the photon.pan namespace like usual and we'll also derive from monobehaviour pun callbacks since we will be using a callback in the script so let's start off by creating a public void function called create room this function needs to be public since we'll be calling it from our create button in here we simply need to use the photonnetwork.createroom function this function needs a string parameter for the name of the room so i will say createinput.txt this way the name of the room we create will be whatever we wrote inside of the create input fields likewise we'll want to make the public void join room function this time we'll use photonetwork.join room we'll pass in join input.txt so here we'll be joining a room with the name being whatever we wrote inside of the joint room input fields it's important to note that when you create a room you also automatically join that room with that being said so we'll use the public override voids on joint room callback function which you guessed will get automatically called once we join the room in here we'll want to transition to our actual game scene but instead of using the basic scenemanager.loading function like we normally use we'll need to use the photonnetwork.loadlevel and pass in whatever the name of your game scene is in my case it's game so simply put whenever you're going to want to load a multiplayer scene you'll need to use this special photonetwork.loadlevel function okay so back in unt i'll select my create and join room's gear object now i'll drag and drop the create input field and the join input fields into the respective slots i'll then select my create room button and add a on click events then just drag and drop the create or join room game object and look for the create room function that we made then just do the same process for the join room button but this time of course use the joint room function now go ahead and build your game by pressing ctrl d so that you can have two instances of our game running so once we connect to our server i'll create a room called a on this version of the game and indeed the game scene is getting loaded up and then we can join the room called a on this instance of the game and we're also loading up the game scene perfect however there is still only one player in the game let's fix that now with step 6 which is to spawn in our player character when we join the game so at the moment in our game scene we have got this player character i'm going to add to him the photon view components this component is required on any game object that we want to spawn on the network then we're also going to create a folder called resources and we'll then turn our character into a prefab inside of that folder i can now safely delete him from the scene i would usually put all my prefabs inside of a folder called prefabs but it is required by photon that all game objects that get spawned on the network must come from a folder called exactly resources that's with a capital r so to recap we need a photon view component on the object as well as turn them into a prefab inside of the resources folder with that done i'll create an empty gear object called spawn players once it's been created i'll add to it a new c sharp script with the same name first things first i'll import like usual the photon.pan namespace now let's create a public game check variable called player prefab which will of course store our player prefab that we just made we're going to spawn in our player at a random position on the map so i'll make four public flow variables to set the boundaries in which we can spawn him so min x max x min y and max y then inside of the start function i'll calculate a random vector to position called spawn pose with the x coordinates being a random number between min x and max x and the y coordinates being a random number between min y and max y then we simply need to call the photonnetwork.instantiate function the first parameter is the name of the object that we want to spawn so i'll type playerprefab.name then we just specify the spawn position so i'll use my spawn pose variable that we just calculated and for the rotation i'll put quaternion dot identity which just means no rotation if we just use the classic instantiate function each version of the game will only see their player and not the other ones that's why we need to use the special photon network.instantiate function to spawn the player on the server so that all the players are visible to each version of the game okay so back inside of unity you'll need to drag and drop the player prefab into the slots and then assign the correct values into these four variables you can quickly place a player character on the boundaries of the map to find the right numbers if you now build the game and test it out you should see that we now have two players in each version of the game now of course there is a new problem that has arrived and that is that each player is now controlling both player characters and that's not something we want so step 7 is to make sure that each player can only control the player character that they spawned in so let me open up my prayer scripts like in almost every script when developing a multiplayer game with photon i'll import the photon.pan namespace let's then make a photon view variable called view remember that we added a photon view component on our prayer character so instead of the start function i'll set view to be equal to the photon view components that is attached to our player okay so this is the code for the player movement we'll just wrap all this code inside of an if statement that checks if view dot is mine in other words this code will only run if this is my player character the one that i spawned in when i arrived in the game so now if you re-test your game you'll see that each player can only move their player character now there is another problem and that is that the player's position is not sinking across a network so if i move a character on one version of the game the other version does not get updated to reflect that move so that brings us to step 8 which is the sync the players movements and this is extremely simple to do i'll just select my prayer prefab and add to him the photon transform view classic components i'll just take the position if you now play your game again you'll see that our players are now beautifully syncing the positions across the network i remember feeling really happy and satisfied when i first got this to work step 9 is to sing or animations because at the moment the other player's player character is moving but he is still playing his idol animation again syncing animations with photon is a piece of cake all we need to do is add the photon animator view components and then inside of the synchronized parameters section you should find your parameter that lets you go from your idle to your run animation in my case i called it is running you now just need to set this from disabled to discrete and there we have it player movements and animations are now perfectly sinking over the network you'll see that our solo game has now been completely transformed into a multiplayer experience we went from this very boring game to still a very boring game but at least it is multi-player now i hope you got a good deep dive into multiplayer game development with this video if you want to take your multiplayer skills to the next level we've got a great udemy course that teaches you much more in depth about photon multiplayer developments by the end of the course you will have created this entire multiplayer game which this time is pretty fun to play drop a like if you learned something cool in this video and it helps us know what kind of content you guys enjoy the most also comment some tutorials or video ideas that you would like us to create in the future thanks for watching cheers
Info
Channel: Blackthornprod
Views: 123,244
Rating: undefined out of 5
Keywords: blackthornprod, unity, tutorial, noaCalice, c#, programming, art, animation, game dev, multiplayer game dev, multiplayer, photon 2, photon2, multiplayer server, sync animations, sync transform, how to make an online multiplayer game in unity, create online game unity, beginner, easy tutorial, step by step, single to multiplayer, unity photon
Id: 93SkbMpWCGo
Channel Id: undefined
Length: 15min 51sec (951 seconds)
Published: Wed Jun 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.