[6] Multiplayer FPS in Unity: PlayerController and Movement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another photon multiplayer tutorial and today we're going to be covering player controller instantiation player movement and syncing player movement between connected players first things first we're going to need to delete the demos folders from photon because photon already has a script called play if we go to add player manager to our player manager here you can see it already has a script called player manager and we don't really want that it's a lot of just extra junk in our game so go over find the demos folder under photon unity networking and just delete that and then go ahead and delete the real time demos and also the chat demos if you want to look at photon demos to help you learn how to use photon just import photon into a separate project and mess around there so i'm going to add a new script called player manager to our player manager prefab and i'm going to put that in my scripts folder and open it up in visual studio all right so in our script here i'm going to make sure i'm using the namespace photon.pun and i'm going to make a new variable here photon view and we're going to call it pv and i'm going to make a method our wake method here and in it we're going to assign the pv to get component photon view i'm going to make a new function here called create controller and in here we're just going to instantiate our playercontroller and we'll get to that later but right now we can just put a debug.log statement there in our start method i'm going to check if pv dot is mine and if it is we're going to create a controller pv is mine is true if the photon view is owned by the local player so as you saw in our previous tutorial when we instantiated the player manager prefabs they each had different owners so this will only ever be true for one of the instantiated player managers in our game and now if we run our game and start it you can see in the console that we instantiate our player controller so the next thing we're going to do is actually create our player controller prefab all right i'm going to make a new 3d object capsule and i'm going to call it playercontroller i'm gonna reset its transform so we're at zero zero zero and then i'm going to add a camera to it and i'm gonna make sure that this camera is has a y of 0.5 so it's in like the head position of our capsule and then our player controller i'm going to make sure that a photon view because we need to have a photon view on any um photon prefab we want to instantiate and i'm just going to drag that into our photon prefabs folder and make it a prefab then we can delete it from our scene so now in our player manager scripts we can replace this here with photon network instantiate and we're going to need to be using system.io again photon dot instantiate path dot combine photon prefabs and player controller and then we can just spawn that at vector 3.0 and quaternion dot identity for now but we we will change this later and have a spawn at a random random location alright so now if we were to run the game create a room start it you can see that our player controller was actually instantiated and we're getting an error here there are two audio listeners in the scene that's because in our game scene let me just go to that um we have a camera already in here and on this camera just remove the audio listener that's all i need to do all right so back in our menu i'm going to run it again create room test great room start room and now you can see that our player controller has been instantiated and its camera is the one being used so we're seeing through the v of our of this player controller and that's important now that we've got the player control instantiation down let's work on actually adding movement and looking around with our player so in our player controller here i'm going to make an empty object i'm going to move this up to the camera position i'm going to call this camera holder and we can put our camera inside of camera holder all right so let's add a new script to it called player controller and i'll move this into my scripts folder now opening it up in visual studio i'm going to make a reference to our rigidbody which i'm going to add to our prefab now [Music] now back in visual studio i'm going to make some new variables sorry lies field float mouse sensitivity sprint speed walk speed jump force smooth time and that's it for our variables we want to access the inspector next i'm going to add a bool grounded a vector 3 smooth move velocity and a vector 3 move amount and also a private flow vertical look rotation now on our wake method i said rb to get component rigid body and now we're going to start working on rotating our player so in our update method we're going to use transform.rotate vector3.uptimes input.getaxis raw mouse x times mass sensitivity there we go and now if we were to let's set our mouse instead of something like three for now to test out and then in our we run our game now [Music] if i move my mouse left and right you can see that the world is rotating also in our game scene let's just make a plane here and reset it and just move it down a bit so that our player doesn't immediately fall out of the world when we run it alright so now if we try [Music] then our player lands on top of the plane and it doesn't fall and if we look our player in the editor or if we look at our play on the scene view we can see that using the mouse makes it rotate and our player just fell over so on our player controller here let's actually go into constraints and freeze the rotation on the x and z and now our player should no longer fall over and that is the case there we go these errors here are just random unity errors they don't mean anything unity just does that sometimes all right so now our player can rotate that's great let's also make a new variable realize field game object folder and then our update method let's do vertical look rotation plus equals input dot get axis raw mouse y times mouse sensitivity vertical look rotation and let's clamp it at negative 90 and 90. then we'll set camera holder.transform.localer angles to vector three dot left times vertical look rotation and if we run the game here [Music] all right now you can see that our player can look all around like so now since this is all functionality for looking around i'm actually going to cut this and put it in and put it in its own method vector3 look and then we can just call look from update keep it nice and tidy next i'm going to work on the movement vector3 moveder equals new vector3 input dot get axis raw horizontal zero input dot get axis raw vertical and then we're gonna normalize that and normalizing it will prevent us from being able to move faster if we hold down two keys at once like w and d then we're gonna say move amount equals vector three dot smooth damp move amount move direction times input dot get key go go.left shift which will be our sprint key [Music] sprint speed lock speed rest smooth velocities [Music] and we'll have to put this in parentheses so this section right here is a compact if statement and basically we're checking if we have the left shift key pressed down which is our sprint key then we'll use the sprint speed value times move direction and if we do not have a press then we'll use the walk speed value times move direction and smooth damp will just smooth out our movement it'll make it nice and smooth next flat jumping if input.getkeydown keycode.space and we are grounded and we will add force in our upward direction times jump force and um next we're actually going to work on the grounded state so i'm going to make a new function here public void set grounded state cool grounded and we'll just set our local variable to that right next we are going to all right to get the grounded state to work um some games will use a raycast rate down to detect if they're grounded or not but i find that doesn't work well if you're standing on top of a mountain peak or something or if you're standing on a sharp point or an edge and it gets really frustrating when you're not able to jump so what i like to do is on my player controller prefab here we'll make a new box collider so 3d object cube move this down and then i just want to disable the mesh render so i can only see the box collider we'll actually just remove that we're not going to use it um let's set the scale on the x like 0.6 0.6 on the x and z and then the y will be like 2.1 and we'll rename this ground check and we can add a new script to it called player ground check all right in our player ground check let's include a reference to our player and in our awake method there's some guy yelling at people in my driveway it's really loud um all right in our awake method let's set our player controller to get component and parent player controller since this will be a child of our player controller we can just get the reference to our player controller by getting the component in our parent all right i'm going to make a few new functions here later on trigger enter deploy it on trigger exit and put on trigger stay all right and onto your enter let's set player controller dot set grounded state true enter exit will set a false and on trigger stable signature again and in each of these we have to make sure that other is not equal if other equals player controller dot game object other game object so if the trigger is our player then we want to just return and not do the rest of it so we'll add that into everything all right now that's sorted out in our player controller i mean here we all right let's consolidate this into its own method here void move move that down and then we can consolidate this into its own method for jump we can add these all back into our update with all that done let's make fixed update we'll say rb.move position rb.position plus transform dot transform direction move amount times time dot fixed delta time the reason we're separating some of the code into the update method and the fixed update method is because update is called every frame while fixed update runs on a fixed interval it's important that we do all physics and movement calculations in the fixed update method so that our movement speed isn't impacted by our fps and now if we go into unity and let's actually set our movement speed here so i'll do like six and three and five and 0.15 for our values we need to make our ground check a trigger and then our ground check script let's make a few more functions right on collision enter point on collision exit and void on collision stay copy all these and just replace other with collision there we go all right so uh turns out five is way too low said it's just like 300 and try that some of these values will take a bit of tweaking to get them how you like there you go 300 seems fine for now all right so we can move with wasd we can sprint by holding shift and we can jump with space that's pretty good let's also fix the lighting in here so in our game scene i'm going to go into lighting and i'm going to click auto generate just make sure that's checked and now if we run our scene we shouldn't get that gross kind of yellowish tint oh we got a nice clean white tint there we go so now if we build the game here well if we start this right now we're actually gonna okay and we spawned in [Music] and everything is janky and the reason this is is because both of our players are actually controlling each other so the way we're gonna fix this is in our player controller we're going to need a new variable here photon view pv and we're going to set pv to get component photon view and then our update function we're going to say if not pv dot is mine we're just going to return yeah not do anything so now if we build you'll find that moving moving our player only one of our players moves now which is good but there's another problem the cameras are synced up wrong we're moving this player but our the camera being used is the other player and that's a little janky so it's not what we want so um here's how we're going to fix this in our player controller script in the in a start method we're going to say if not pv dot is mine we're just going to hit component and children camera i'm just going to destroy that dot game object make sure it's the game object too because if you don't include that it's only going to destroy the camera and not the audio listener and then we'll end up with two audio listeners in our scene again and that's also the reason i didn't want to make um guns and stuff the child of the camera and why we put the child and and why we put the camera under the camera holder object because we're just going to end up destroying the camera for anyone who is not the local player pv dot is mine can be a hard hard thing to wrap your head around at first but after a while of using photon you'll eventually get used to it alright so now if we build it you can see that we control there we go we're looking through the camera of our own player and we control the wsd and if we switch over to unity here if i move i control my own player but the movement is not syncing between the clients we can control our own characters now but the movement doesn't change on the other person's screen and the way we're going to do this is in our player controller here all we have to do is add a new component called photon transform view and then we have to make sure that in observed components here on our photon view we drag in photon transform view all right photon transform view will just sync the transform of this object between clients so if we run our game now find room test let's start it you can see that if we move on this screen our player moves on the other client screen too and it's a little janky right now and i will tell you why because we are not destroying the rigid body on our other player so the physics are being applied on the other client too and we only want to apply physics to our player on our own client and that's what's getting this janky like rubber banding when we're jumping up and down so on our player controller here all we have to do is get new line here and destroy rb now we can build getting an error here and um so as you can see in our scene now the jitterness generous of jumping is fixed pretty much but we're getting another error here and that's just because we forgot to include this same return function or fixed update now everything should work perfectly in our game scene we might as well add a few different cubes here just to spice it up a bit make it a little more fun to jump around and i'm also going to make a new folder here called materials and i'm going to make a new material called just block and we're just gonna make it like a grayish color just so that we can tell our cubes from the ground make a few more cubes mess around with we'll make some parkour if we want sky's the limit with this kind of thing guys it's like this is a masterpiece all right now we've got our nice little obstacle course set up here whatever you want to call it i can build our game for like the 50th time today and now you can see that we can jump around have a bit of fun and the other player is able to see us move around as you can see jump on other player's head oh oh and our player seems to be rotating around strangely and what you just saw there was another bug where let's just freeze rotation on the y too freeze rotation on all axes for your player controller that was because our rigid body was actually adding um having force applied to it um in a rotation let's also set our collision detection to continuous just up just so that our players don't randomly fall through the floor and we want a freeze rotation in the y because as you can see there the player is kind of drifting and that's it for now [Music] you
Info
Channel: Rugbug Redfern
Views: 40,565
Rating: undefined out of 5
Keywords: unity, c#, tutorial, multiplayer, photon, fps, first person shooter, online, connect, friends, guns, how to, walkthrough, pun, pun 2, development, devlog, help, shooter, gun, shoot, bullet, photon 2, how, create, code, unity3d, csharp, lesson, lecture, online multiplayer, multiplayer fps, unreal, godot, series, open source, source code, multiplayer fps tutorial, photon tutorial, photon fps tutorial, pun fps tutorial, pun 2 fps, 2020, 2021, new, updated, c sharp, unity multiplayer fps, multiplayer fps in unity
Id: AZRdwnBJcfg
Channel Id: undefined
Length: 22min 41sec (1361 seconds)
Published: Wed Jul 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.