Multiplayer FPS in Unity: Photon 2 Setup & Menu

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so I'm a little bored right now I don't really have anything to do other than all of my homework so I'm gonna make a multiplayer FPS game here and just start this off I'm gonna make a new unity project and I'm gonna call it just a face game whatever and in this project we're gonna be using a networking solution called photon which is great because it allows us to have up to 20 players at once playing our game network together for free now I've used this for my past few games run match one Android match two and it works like a charm if you're a small indie developer just trying to make a game with zero budget so to import I'm going to search pun 2 on the asset store fun to freeze when we're gonna be using you don't really the paid version doesn't give you much more paid version gives you like a hundred players at the same time and a few more features but it's really not necessary I've I've only ever used the free version and I've never had any problems with it or anything I wasn't able to do all right now that it's imported pun I'm going to we're gonna need to set up an app ID for our game so I'm going to go to my web browsers are going to get a photon engine comm which is their website I'm going to sign in with my account if you don't have an account you can create one it's free once I'm signed in I'm gonna go to my cloud applications these are all the cloud applications I've made and we're gonna create a new application and we're gonna use photon pun we're gonna name it a ps4 elope game and then we're gonna create it and now I'm gonna look for it in this list you won't have all these these are my projects and I'm gonna copy this app ID don't share this with anyone oh they can you know have players connect to your game this is what the app ID players will use to connect to your game exclusively I'm gonna paste that in here set up my project alright it generates a file for me which is great in server cloud settings you can expand this usually I set the fixed region as usw which means that players will only be able to connect to the United States West photon relay server which is where I live so I I'm selfish I've set up the server where it's lowest paying for me but that means if you live somewhere like in India or Europe then you're gonna have a higher pink but this means that all players will always be able to join each other if you don't have a fixed region it'll automatically go to the closest relay server and near you and that can make it hard to find other players because you'll only be able to find players in that region and by having a set to us west if anyone joins online to be able to find anyone else's room and it doesn't matter what region they're in since I'm making a small game I want all players to be able to find each other on all time so I'm gonna fix the region to us West and photon has a full region list online if you want to look at that and find the region closest to you all right so to set up our scene I'm gonna start here by creating a canvas and then I'm gonna set it to scale screen size and 1920 by 1080 I'm gonna have it match the width and height with halfway between width and height so it matches on both axes and I just set the reference resolution to my monitors resolution so it's easier to work with now let's see what are we gonna do next what are we gonna do next I'm gonna make a scripts folder great folder now I'm gonna make a new c-sharp script in order they call it launcher and this is what we're going to use to connect to the photon services connect to their master server which lets us find other games on the network and other players and it's also gonna launch our game handle joining room stuff like that listening rooms now in our launcher class I'm going to use the namespace photon fun then I'm going to do monobehaviour pun callbacks which is going to give us access to some helpful callbacks we're gonna need to connect of photon servers and my start method here I'm gonna do photon Network connect using settings and this is going to connect to the photon master server using the settings set up in our photon server settings file that it generated here so it's going to connect to the fixed region uswest and it's going to establish the connection with the master server in United States West so now what's happening when we launch the games we're going to just connect to the server but that's not all we need to do to start finding rooms or creating rooms I'm gonna make a public override void one connected to master this is going to be called when we actually finally connect to the master server and I'm going to do photon and work dot doin Lobby lobbies are basically where you find rooms where you create rooms you need to be in a lobby to find rooms or create rooms so before we can really do anything with our game we're gonna needed to join the lobby so as soon as we connect to the master server I'm just going to call the join lobby method so that we can connect to a lobby and start creating rooms now once we've connected to a lobby which I can look over I've deployed on connected on John joined lobby this will be called when we during the lobby so I can actually put some debug log statements here telling us when we joined the lobby join lobby it's gonna up this debug [Music] and now if I go back into unity and put our laundry script on our canvas now when we run the game you can see that it's connecting to the master server it connected to the master and then it joined the lobby so now we're in a lobby we can list for him so you can create rooms we can do everything we want to do with matchmaking and stuff like that so I'm gonna actually start setting up some of our menus here I'm gonna create an empty object I'm gonna call this floating menu I'm gonna hold down shift and alt and then double click on this in the bottom right which is gonna make it fill the screen nothing on a 2d mode I can edit this menu here this is gonna be our loading menu telling us that we're joining the server connecting this over connecting the lobby because if we don't have this and someone tries to create a room while we haven't connected that the master server is gonna give us an heir we don't want that so I'm gonna make a new text element here with techsmartt extra mesh pro text mesh pro is just a better version of a unities default text and I use it for all my UI it's easier to use it's easier to you can add effects to it and stuff it's really nice and it comes built into unity all I have to do is create a text message I set to start the import I'm gonna say loading and you can't really see that against the sky box right now I'm actually gonna set our main camera to solid color and then make it maybe gray just for now I'm gonna say this to ya loading base the font size crease this 600 by 100 will probably fit our screen well I'll make it in the middle there we go that's good enough for me so now we have a loading menu here and I'm also going to make another menu and call this title and this is gonna be where all the main where you once you load on the game you're gonna be introduced to this menu let's write get all your options so we're gonna create an empty object here so to 600 by 0 there's gonna be our button container a button container here I'm gonna add a vertical layout group component gonna make the child expand on the width and I set the spacing to 10 so that they're spacing in between the buttons UI and button text much pro here's our button let's increase the height to a hundred knots to big seventy take the button text maybe make that up a bit scale it up 45 find route so first button I'll duplicate that with control D and this will be great room and this third button can be quick these are the three buttons we're going to need for matchmaking and photon has a matchmaking interface which you can implement if you want join random room but what we're going to be doing here is just being able to create a room and then finding that route from a list if you're working on a small indie game and you're just gonna have small groups of people playing you don't want people to get mixed up in rooms when they just want to play with their friends so being able to create a room and having other people join it is really convenient and matchmaking for a game this small isn't really necessary so now we got to make it so that the title menu actually appears when we are done loading so I'm actually gonna make I like to be neat about this because i really love i love my UI and this is all going to be temporary hopefully in the future we can redo all this to actually look pretty and i'm going to build some infrastructure for our menu systems here I'm gonna make a menu manager script and this is going to control opening and closing the menus so for our loading menu I'm going to add a new script to it called menu and we're gonna add a menu script onto everything that we want to be able to come in and out as a menu now if we open our menu script I'm going to delete these default methods and add in a new public void open and public void and close all right and close I'm going to do game object on set active false it's going to disable our menu and then open I'm going to do game object on set active true which will enable it now in our menu manager script I am going to make us realize field which is going to expose our variable to the inspector without making it public because we don't really want it public we don't need other classes access in this field gonna make a menu array of menus and now if we go back into unity you'll see on our menu manager we've got an array here I'm gonna make this the right size too and I'm gonna add a menu component to our title menu also we're going to add both these menus into the menu list our menu array and then in our menu I'm gonna add another field public string menu name alright and this is gonna allow us to open and close menus based on their name I'm going to call this menu loading I'm gonna call this menu title now back in our menu manager script here make a new function open menu string menu name of public void open menu menu menu and now I have two functions each with different parameters this first one is gonna take a string and open a menu and the second one is gonna take a menu object a mini script and open a menu and this makes it easier to assign references when you're editing in just having this button if I just want this button to open like the loading menu I could make this I could set menu manager open menu and then I could drag in my loading menu and that's really nice but if we're trying to access access the menus through scripts like in our launcher class we want to close the loading menu and open the title menu we don't want to store a reference to the menu in every single class we want to access it so we're just gonna have a string key for our menus here for now I'm just going to loop through every menu in our menus if or something if men use a new name equals menu name that we are trying to open then we can open menu menu and then in our manuscript I'm gonna add another thing another little thing here another variable and I hide this from the inspector because I don't want to accidentally change this while I'm editing or anything it'll be a public will open and we're gonna use this to figure out which menus are open or not so in our open menu script here I'm going to say menu dot open equals true actually I don't even need to do that we can simplify this and our manuscript and I open we could set open equals true and close it said open equals false it's nice I always like to if I'm working with a more complex system like this I like to dedicate tasks on object to list script on that object so for our menu script here I like to have everything controlled by the sexual scripts so the open variable and enabling and disabling it I like to control by this and then if we want to change that later it's really easy then our menu manager all we have to do is call menu dot open that's it that'll open our menu and now if we were to do this it would work we could open a menu but we will also want to be able to close the menu so here we're checking if the menu is the menu we want to open them and then we're opening it but if it's an auth and if the menu that we're checking right now is open then we want to close it and I'll make another function here for this all right if we open a menu with this function it's not going to close the one that we currently have open so in order to do that I'm gonna say I check everything and if the menu is open then we're going to close it because it will only want to have one many open at a time in our menu manager script I want to make this actually a singleton so that we can access it from anywhere in the game at any time I'm going to make a new variable public static of any manager instance and our awake function instance is gonna be this so here we're gonna host a static reference which means the variable is bound to the class not the actual object in unity and this static reference is going to reference this script which we have in our scene here and in our launcher now we're going to be able to do menu manager dot instance dot open menu title so we don't have to have a reference to our menu manager script to open menu and that's really nice to have so now I'm going to set the title menu to be not active so that are loading menu is active by default and if I play the game once I've just noticed something okay so the loading money you know we're actually gonna have to do the center mini script I'm gonna have to make this public so that we can make sure the loading menu set to open because right now it's trying to close the menu that is open and we don't have any menu that's open so now that I've set it to open when we start our game once we've connected everything it's gonna open the menu cold title and that's this menu with a lot of buttons here alright alright we we've done it made a mini manager and all that work is gonna pay off later down the road when we have more menus here for like customizing and exp joining rooms and a room menu and look at loading a room and stuff like that that's gonna pay off with our menu measure here now that we've got this done I think that the next step is actually creating a room and finding room but let's start with creating room I'm gonna make a new menu called great menu can I make that fill the screen I'm going to disable the title menu here and in our create room menu I want to have an option for the name of the room and I want to have a button to create it so I'm gonna make a new text mushroom input field it's kind of small sort of button size 600 by 70 I won't do that for input field too and then I want my text scale to match that 45 point size 45 there we go and then in our placeholder I'm going to just set it to enter just so that you know what to type in there all right now I'm going to take one of our buttons from here paste it here reset its anchor so it's in the center of the screen and then lower it down whenever 100 it's probably I'm gonna do 80 so it has 10 pixel spacing because our button is so need pixels tall all right that's great we're gonna rename this great room then our launcher script I'm gonna make a new public void great room and then I'm also going to have I'm going to import the namespace text most Pro so that we can use that serialized field I'm sorry analyze field checks maestro input field room name and put you know and then in our crate room function I'm just gonna call photon gonna work great room with this room name and then I'm also gonna say if shreya is null or empty my room name that we're just gonna return and this is to make sure that we actually enter a name for room before we create one so we don't just create a room with no name if you actually enter this into the career room function it's gonna generate a random string of letters and numbers for your room so if you don't ever want room names to be seen you can just do that but we're gonna have our room names listed in a list that we can join later so I want our room news to be customizable so that players can recognize which room their friends are in so now that we've started creating a room here it's gonna take a second for photon to actually contact the server telling it we want to create it and then the server telling us that we've created and joined a room so in order to stop players from clicking any other buttons there I'm going to do menu manager that instance dot open menu loading and then I'm going to make a new override void function here joint room and then it will open up a room menu and I'm gonna make another on great room failed and when we start to create a room there are two callbacks that can be potentially called we can either have successfully joined a room on created room is also called but on joint room is called when you join a room or you create a room so we're just going to use that because all we're gonna do is load the room menu and then an on career room failed that means we fail to create a room and it's going to give us an error message here which we could display to the user so once we've joined a room I'm gonna make a new menu here create empty put the screen room menu will actually add a menu component of this and call it great room [Music] and then we can add it to our list of menus here now in our room menu I want to have the room title at the top and then maybe a list of players but we'll get to that later and a button at the bottom that allows us to leave the room so you eye next 45 is our font size for this project apparently so that down 50 a little spacing on the sides make at the center I call it room name and rename this text room name text and honestly I should probably go and rename all my buttons so that they're easy to tell what they're doing what their actual purpose is but we can do that once we actually assign what they do in the on click event all right so our room name Texas there I'm gonna cop your button I'm gonna at the bottom here dat it's got a little spacing 5075 doesn't really matter I just wanted to look clean we'll probably read through this later but that's fine now in our fun script right let's put leave room because that's what we're doing here and then we'll make a new function here public overwrite actually no not yet let's set up our joint room and on create room failed functions first any manager to instance open menu room and when I was a sets up room menu had a menu component call it room good our menu manager has a menu alright and in our on create room field we should have an air menu or something so let's make a new air and I'll add a menu all we need here is some text and a button so I'll just copy that from the room menu you say well that for now while we're at it we're gonna menu air next make that red let them know that it's an air and a test to make sure that this wraps nicely it doesn't so I'm going to set that to the top alignment so it does wrap nicely alright look at that works perfectly so if I have an air like 20 lines long we can actually read it I'm gonna rename this button to okay cuz that's all you really can do you got in there so let's go back in our script here menu manager Justin stop open menu error and then in here we should probably include a reference to the air text so that we can actually assign it a value so when our room creation fails I'll make the air text text to room creation failed Plus message and I can make this button open up our title menu no that's wrong what am i doing canvas menu manager master controlling class open menu one for menu title great so now if we run this we can see that clicking ok button returns us to the menu but we haven't actually added it to our list here so let's do that so that it closes itself all right error text their text in here let's try get our room name and put field while we're at it all so let's set up some of our buttons here in the title menu let's say our great room button go to the canvas menu manager open menu great room menu [Music] then in our create room any we're gonna have this button actually call not sure not great room [Music] let's make our loading when you active again and see if this actually works all right run the game create room what is wrong we accidentally call up clothes menu okay open anything okay it's opening the menu finally we go create room into room name test great room loading right now we are in a room that's great ok so let's start off this part by actually setting our room name text to our room name so in our launcher I'm going to make a new reference to name text and then in on joint room we're gonna set our own int X at x equals photon network current room name okay there we go long line there but it's going to give us the name of our room and now if we start it up great room test room I didn't assign it [Music] start it up great room just great room there we go that's the name of our room at the top there great I'm so glad and for a room menu let's make this actually leave the room we don't have a method for that yet all right public voice the fru-u-- photon Network room and then we're gonna open the loading screen and look all right put on left room we finally leave the room we're gonna open up the title man all right now called launch you're done his room and then you get room just room there we go it's at the top leave room and we're back in our lobby that's great you know what I'll just we'll leave it off here that's a nice tan episode one let's see what do we do today we made a menu manager we imported photon and gotta so that we load in the menu and we are able to create a room and then leave the room and return to the menu that's pretty good for thirty minutes of work bye bye see you soon [Music] [Music]
Info
Channel: Rugbug Redfern
Views: 137,882
Rating: undefined out of 5
Keywords: pun 2, photon 2, multiplayer fps tutorial, photon tutorial, photon fps tutorial, pun fps tutorial, pun 2 fps, unity multiplayer fps, multiplayer fps in unity, unity multiplayer tutorial, unity multiplayer, multiplayer, fps, photon, pun, unity, development, devlog, help, online, gaming, shooter, first person shooter, gun, shoot, connect, how to, how, create, code, c#, unity3d, csharp, walkthrough, lesson, online multiplayer, multiplayer fps, unreal, godot, series, source code, 2020, 2021, new, C sharp
Id: zPZK7C5_BQo
Channel Id: undefined
Length: 29min 8sec (1748 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.