HowTo: Build a 3rd person shooter in Unity - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up Jason your community 3d college today we're gonna go into a slightly bigger tutorial than usual and what I'm gonna run through is how to set up a character that you can run around with now track it with the camera blow some stuff up see things animate and kind of go through the whole process of building up a little basic shooter now I think we're gonna split this into two parts so I'm gonna do the movement and tracking stuff in part one and then we'll jump over to shooting and blowing things up from there in part two so let's just get started I'm going to show you kind of what the walking round part will look like and then we'll go through the steps to actually rebuild it all so here I've got the character you can see I can walk around he's kind of nicely tracked he animates I can run back behind things control it all and I'm controlling this keyboard now but I could also just grab out an Xbox controller and move them around too and then we've got a little limiter here so you can't get too close and here he is walking around with the controller you can see it just kind of maps everything just using a standard input system all right so let's kill this all start over and see kind of how this stuff's all put together and how to make everything work so I'm gonna create a new scene in fact I think what I'm gonna do is start with this simple town light demo scene they had a nice little strip of land there or strip of street this is what I used to get started and this was just a free asset pack from the asset store I'll put a link to all the assets use down below though alright so the first thing I need to do is just build out a real quick little area where the user or player could walk around so I like to start by creating an empty game object and naming it environment and then resetting the position and I use this just to put all of the environmental stuff underneath this so buildings maybe lighting some other stuff that's really the scene not so much the gameplay so grab all these objects drop them under the Empire an object and then I'm just gonna select all of them and duplicate so control D or command D if you're on a Mac and then hold control and just drag so that they snap and get it nicely lined up you can see if you hold ctrl you get that grid snapping where it moves by a one unit each time instead of partial so that way it's nicely lined up now I need some buildings on the other side so I'm just gonna select the buildings both these building parts and duplicate drag them over a bit now I'm not holding ctrl but I want to rotate all of these too so I'm just gonna do a 180 rotation on the Y drag them kinda in and build up this little street and I think I'll drag it just that way a bit me a little bit in so that's kind of up on the sidewalk not gonna get it perfect but good enough okay um now move some of these cars maybe a little bit out of the way let's grab these these and maybe this time I'll just drop some cars at the end to kind of block the view and then put a let's put a pizza place at the end this time so here I'm going to take that rotate it what do we need 180 there we go and it doesn't quite fill the area but since I'm not being picky right now I'm just gonna scale it and stretch it a little bit it's kind of far away it probably won't be too noticeable all right now I want to move my camera kind of get that lined up so I make sure I have a good view I think that looks good good enough at least so I've got a view where I can kind of move my player around now let's get started with actually setting up the character so we've got the environment and I've got this tuned soldier that I downloaded again free from the asset store and I'll have a link below and I'm gonna go in and take this tuned soldier demo and drop it into my scene but before I do that I'm gonna create an empty game object and I'm going to name this player and then I'm going to drop the tuned soldier underneath it as a child and the reason I do this is I want to be able to separate out my logic of my game from the visuals and the art so if the tune soldier right here is a child then I can swap out the character pretty easily without big changes to the play it just makes it a little bit simpler it's also easier to do live swapping I could just you know switch the model and not have to change out different play or prefabs or anything like that so I've got the player here selected I'm gonna move him up and closer to me in fact I'm gonna do get down where I want him hit ctrl shift F can move him right to where I am and then a zero out is uh rotations and slide them just down to just over the ground there we go it's like- 0.19 and I think that's because this ground piece right here is just a little bit lower than zero all right so I've got that set up now I need to add a character controller so I'm just going to use a basic character controller drop that right on to the player here remember not onto the art part we want to have all of the control and logic on the player separate it off from the model and if you look here you see that little capsule is down in the ground I'm just gonna move that up by adjusting the y-value of the center to one now ideally you probably want this to match up so that it really goes up to his head if we were doing some collision checks for him or we want to make sure you can't you know not walk into something too short but I don't really care for this case so I'm just gonna leave it at one but if you need to do that you'd adjust the height and the y center here to line them up and maybe pull in or out the the radius as well all right so we've got a character here right now it doesn't do anything though so I want to make him move so I'm gonna create a new script in fact I've got one here but I'm gonna create a new version and just go straight from scratch so do a new script and we're just gonna call this actually here's what I'm going to do I'm going to rename this to player movement two because that's my old one I'm gonna create a new player movement script and I'm gonna have to go in there and just update my my old script and rename that so that it doesn't break things in the actual class but I think we'll be good I kind of want to go through this whole process of rebuilding listen said so there we go got that one done let's just get that out of here and open up the actual new player move script so that's gonna be if you're in Visual Studio oh I should probably make that fit so if you're in Visual Studio and you'll see your project and an editor version of your project if you want to look for scripts in here you can just go down under the main project assets and then wherever you keep them normally think scripts or a code folder something like that is where you should find this stuff but it depends on your project and how you like lay things out so I've got an empty script here I'm going to delete the start method and this comment over update and then I use a control K control D to just clean up the formatting a little bit and explicitly call this update method private actually I think I do want a start method but we'll worry about that in a moment so what I want to do now is read the input so to do that I'm just gonna do bar horizontal equals input get access horizontal now the casing on this string name here is important you have to match it it's going to line up with what's in the input settings I'll show you that in a moment you and I'm gonna copy this and do another one for vertical and here we'll just do vertical and now for our variables keep them lowercase because they're variables that aren't public you usually keep them lowercase that match with standards alright so now I've got a horizontal and vertical and I need to figure out a movement vector so to do that I'm just gonna do bar movement equals new vector3 and I'm gonna pass in the horizontal value for the X and we're not going to use Y because that would make us move up and down so we'll put a 0 for the Y and then we use vertical for the Z now to move my character I want to access the character controller and have it do the actual movement that way the collisions will work right he'll fall down everything will kind of just work kind of semi almost magically so I need an a reference to my character controller so I could do something like getcomponent character controller dot simple move and pass in movement and then multiply some stuff out but I really don't want to be calling getcomponent every update in this case it wouldn't actually make a difference but it's just not a good practice to get into so I'm gonna cut this part and I'm gonna go up here and just do a private void awake and if you just hit enter when you're halfway done with those by the way in Visual Studio if everything is working right it should oughta give you your parentheses and your little squiggly braces so in a wake what I want to do is cache that character controller so I'm going to do character controller lowercase C on the one because it's gonna be a variable equals and I'm just gonna paste in that getcomponent character controller that I had from before hit a semicolon and then if I hit control period I'll get the option to generate a field and that's exactly what I want so I'm just going to generate a character controller field we're gonna cash it in awake so that way we only call getcomponent once again not a huge deal in this case but in a bigger project it's important so it's just a good habit to get into and then down here an update we'll just call the reference to that character controller and we'll call this simple move now we don't want to just pass in movement we want this to be based off of the speed our game is running out so our frame rate and to do that we multiply it by time.deltatime so I can save this and move around in fact let's do that first before we go any further let's hit play oh wait before I hit play I need to add the script I don't know about you guys but I do this all the time when I write a new script get it already get it working start playing and then I realized that I never actually hooked up my script that I wrote so there we go let's drop this player movement script on hit play and theoretically if everything's going good oh I can't really walk so what happened there the issue is that my um my character is falling through the world because there are no colliders on the rest of the world if you look at these if I select these there's no collider here just renderers no collider on the building's no collider on these now there are a couple ways you could set this up for a simple project like this where I'm not building out a big area there not a lot of huge polygon meshes what I like to do is just find all the mesh renderers so just up in here just type mesh renderer no spaces not case-sensitive that'll find all of them you can see right here it's not gonna find these skinned mesh renderers you notice he's kind of grayed out and not selected so then we just go over here and I hit control a just select all of them and then you have to uncheck this demo scene right I don't know if you have to anymore but you used to so I didn't check that and then just go add component mesh renderer now of course oh not mesh fracture mesh Collider sorry now of course if you already have mesh colliders on some of these things you can't just you know select all and do it but if in this case it works ideally you probably want to set these up individually again for your project if you have a big giant city a box Collider is probably better than a mesh Collider on these buildings or on these ground pieces but yeah like same doesn't matter right now so we'll just go with mesh colliders and now I should be able to hit play and see what happens I shouldn't fall through at least there we go not falling through and I am kind of able to move let's see so if I select my player and I hit it look if you look at these numbers you can see that they're changing they're just changing really really really small so what we need to do is multiply this movement vector a bit by some speed so I'm going to add another variable here and I'm gonna call this move speed so I'm gonna do we're gonna move it the movement Direction times the time Delta x times move speed you know I hit control period and generate a field for move speed and it's going to wrongly make it a vector3 really what I want is a float and I want to set it to a default value of let's go with like a hundred now I'm gonna add the serialized field attribute over it so that way I can go into the editor and modify this a run time and figure out a good speed so let's try one more time I'm gonna select the player this time hit play and then we'll move around and play with the movie they're go to play all right walking around any second now there we go okay so now I should be able to walk I did not save my script let's jump back over code one more time sorry about that oh no it was just recompiling okay so we've got move speed there and I should be able to move around look at that I can move kind of side to side left and right the controls aren't actually the direction I want and the speeds a little slow let's turn this up to maybe like 300 there we go that feels a little bit better but right now when I hit forward and back I'm actually moving in his forward and back and if I hit left and right I'm moving in his left and right and that's not because of the direction he's facing it's because of the orientation with everything so if I hit it again I still get the same movement so what I want to do here is there are a couple options I could change the control stuff in this case though what I like to do is just go in here select my environment in fact let's make the player a child of the environment for one moment go to the environment I'll zoom out a little bit and I'm going to rotate it 90 degrees so now let's see now if I where's my player players right there and I'll move my camera over here to look at him hit play and I can't remember if I needed to go 90 or negative 90 now we'll find out in a second so here we go now I walk Oh yep it's backwards so I'll select my environment flip that over to negative 90 yeah what's happening here isn't really just adjusting the orientation of the world so that my camera is kind of lined up with the way the controls are oriented which which makes sense for this game like we want it to line up so that when I press forward I'm walking down the alley if I hit left I go towards the left of the alley you know down when I come towards me oh didn't move my camera there we go so let's get that here get the camera in position set press play and movement should be pretty smooth in in the right directions see there we go movements good I think I did my change to move speed while we were playing so it didn't save so now that I'm not playing I'll pull the play her back out of the environment he's not part of the environment thank that movespeed back up and move on to the next part so you saw in the other video or the beginning of the video he is animating when he walks he's not just standing around and sliding right so let's set up an animation controller to make that work so I'm gonna go under assets and we're just gonna create a new folder and call this animation and I'm gonna create an animation controller under here so just go right click and go to animator controller I say animator animation controller but it's ending yeah it's the one that it says on the screen so let's call this soldier and a maid tour or let's call soldier controller it's not really the animator the animator is on the actual soldier there so once this is created I'm gonna select the soldier there we go and assign it to his animator now if you don't have an animator on your character it's probably because it wasn't set up as a humanoid you may want to switch characters or switch it over to be humanoid and add an animator to it okay now I've got the animator I'm gonna double click on it to open it up and if you've never used the mechanism animation system before don't worry it's relatively straightforward we're gonna do semi advanced concepts but I'm not gonna go too complicated here so for this animator what I want to do is assign the animations for this character now this guy came with a folder full of animations he's got an idle he's got a run he's got a shoot in a burst shoot and this is the old controller that I made in my first run-through with this so what I want to do is take the animator or the uh what is this assault combat idle and just drag it and drop it on here now you want to make sure that you grab the animation here if you expand out these FBX files and you'll see the animations right down there and if you don't we'll know how to look at the animations by the way if you click on the mesh these are actually little tabs here and you can see the animations and you kind of get a little preview of them you can adjust their looping settings cut them trim them speed them up in anything like that that you need to do but here we don't really need that we just need to make sure that you expand it grab it out and drop it on now when you drop it on the for that you drop on is going to become orange and oranges because it's the default one see if I drop another one on here like run it's gray there's no line to it it's not the default now I don't actually just want to use an animator if I do this and I just hit play you're gonna see that he's just gonna sit in his idle state let's try it real quick just demo it so you see he's in idle and just kind of stays there he moves around but he still just kind of idles while he's moving it doesn't matter what he's doing he's just constantly idling now I want him to speed up and slow down based off of the speed that we're gonna write we want him to walk and then go back to an idle so to set that up what I'm gonna do is right-click here and hit create new blend tree in state and I'm gonna rename this because it's no longer gonna be just an idle I'm gonna call this what's called a motion for now now if i double click on this once it's a blend tree you'll see it looks a little bit different and if you look up here this is kind of like a folder structure in Windows or on a Mac or something with the little back and navigation stuff so I can click on it to go into motion then click back to base later to go back to it now I'm gonna click on my blend tree and over here I see we have a list of motions that's empty and I want to add some new motions so I'm going to hit add motion field twice and then I'm going to grab this assault combat idle drop that down to be the first motion and then I'm gonna take a run and make that be the second motion now right now there's a parameter named blend that got automatically created when I made this blend tree I'm actually gonna delete this yep and then we're gonna hit plus and add a new float and name a speed now up here there's a field for the parameter for the blend tree so now I need to pick speed from that drop-down blend was no longer in existence what's gonna happen here is based off of the speed value we're going to blend between idle and run so if speed is at zero the threshold of zero I know will play this is the speed that the animation will play at like the playback speed so it'll play at normal speed so you can use it to double things or slow them down whatever you need most of time this is one or semi near one depending on how you play tria setup but then if the value of speed right here is 1 or greater than 1 we're gonna be in the run animation and as we kind of go through it like if I slide this up and down you can see it down here in the bottom so I'm gonna hit play yeah let's just make this a little bit bigger get a good view of the character so he's never really gonna be at this weird speed he's usually gonna be a zero or he's gonna be right around 1 or above 1 so you can see as this goes up and down kind of blends between the two now what you would usually do is also add in a walk here so that there's a little bit of threshold if you're going to have multiple speeds here we just have kind of like going and stopped it's good enough though all right so I'm gonna go back and we're gonna jump back into the scene view if I hit play now you'll see that the animation still plays we still get that idle but the you know moving around doesn't affect the speed if I go to the animator tab and set the value of 1 here go back to the game view you see now he's running permanently so what we want to do is adjust that speed variable in our code so let's jump back over to the code let's see that's right here well one second oh there it is okay so what we want to do is set a parameter on the the character's animator so we're going to need to get the animator and I'm going to do that in a wake again so review animator lowercase a cuz it's gonna be a new variable equals get component in children animator capital a and semicolon the parenthesis then I hit control period again generate the field and we'll see we have a field up here for animator and these don't have the serialized field attribute on them because we're not going to be editing them in the inspector we're just getting them in a wake and cashing them alright so down here right after we do the movement let's just add another space here and we call animator set float and we're gonna pass in the name of the float again case-sensitive here so I named it speed with capital S yours has to match whatever you named it so I'm passing in speed and the value I'm gonna pass in is actually going to be movement dot magnet so if I'm on a controller and I pushed kind of part way over my magnitudes not gonna be a full one if I go all the way on any edge or I'm holding the keyboard it's gonna be one maybe a little bit above one but it's not going to be no variable in between there unless you're using a controller so anyway this gets us the value the magnitude of that vector works great as a speed variable and I'm gonna hit a semicolon save and jump back into the project and we should be able to kind of play and watch our guy walk around stop walking start walking let's try it so you hit play and game view should pop up yeah let's see now we walk and as I walk you see he animates and when I stop he stops it's still not quite right though cuz he's not facing the right direction right I'll walk this way he's gonna running sideways pretty weird so let's fix that let's jump back over to the player movement script for one more time and here what we want to do is set the rotation of this guy now there are a couple ways we could do this we could get the new direction so you can do like quaternion new direction equals pattern e and dot look rotation and then we pass in the movement vector so this is going to get us a quaternion from the vector direction just a nice easy call to quaternion dot look rotation and then we can do something like transform dot rotation equals new direction save that off hit play and now we should see him kind of instantly snap to the correct direction any second now all right so if I go right he instantly snaps right I go down he snaps down I go left he snaps left and if I let go he actually snaps back to forward so that's kind of right am i doing what we want but we really want it to be a lot smoother and we also don't want that weird snapping back when I let go right so let's fix those two things the first thing to fix that snap all we really need to do is check here we go if movement dot magnitude is greater than zero then do this stuff so that way if we're not moving at all if we've let go of the keyboard magnitude is at zero we don't want to rotate we just want to leave you facing the same direction that you are so we'll only rotate if there is a magnitude here the other thing that we want to do is smooth out this rotation so to do that we'll go transform dot rotation instead of setting it to new direction we use quaternion slurp and then we'll pass in the existing current rotation transform that rotation then we'll pass in oh so net multiply then the target rotation which is the new direction and then we need to give it a float for how far to go and to do this what I usually do is time dot delta time times a mu or turn speed turn speed variable so this way we're not moving it all the way it's kind of we're just gonna slowly turn it and that speed of how fast we turn it will be based off of this turn speed variable turn speed doesn't exist so again control period in visual studio generate a field f12 to go to it and then I'm going to copy this serialize field after you have pasted it right up there and give it some default value I think five was a good value in my last test so save that off hit play one more time we've almost got a smoothly walking character let's see alright let's move him around yep he's rotating he looks good I think I want to turn up his move speed just a little bit though like 400 kind of lines up a little bit better with uh with his animation there perfect so now he's moving around the last thing I want to do on here is fix up the camera control so you may have noticed in the first video the camera was kind of focusing on him and moving around and it wasn't just uh you know looking straight forward the whole time so let's fix that to do that what I do is import the cinema Sheen asset from unity it's amazing I said and I did a whole video on it totally recommend it what sits in though we just go to the cinema Sheen menu and create a virtual camera now when you do that if it's your first one it's actually going to reset your camera so your main camera is now move to 0-0 just like this transform of this thing and it can be a little bit frustrating so you may want to copy your cameras position before you do that but if you're like me and you don't really care you're just kind of get into the position that you want your V cam at and you do ctrl shift F and that's gonna place it right here so now I've got the camera right there and it's not set to look at this guy yet so to do that all I need to do is grab the tune soldier drop him into the lookout field of the virtual camera and press play now watch what happens it's gonna track nicely along with this guy as he walks there we go and you see it's tracking actually at his feet because the route of this character is at his feet if I want to track a different part like maybe I want to keep it on his face or his head or something else I just need to grab that other piece and make that the child so let's say I wanted to go with let's grab his head right so get his face nice and close when he gets in there so I've got the head expanded I'm gonna go to the V cam sign the head and now I've got a good view of his face and this overlay here go away if you click off via the view cam so there we go now I can see him he's kind of centered right there and his face is what I see I'm gonna stop playing and I'm gonna lose that setting so I'm gonna reselect it reassign it and let's save now there are two other things I did in there that I haven't fixed yet so let's do those and then we'll end it and then go on to actually shooting and blowing up these cars in the next video all right in those two things the first is if I keep walking you see I can just kind of walk right through and run off the map not something I want to do so let me demo that real quick and then show you the super super simple way to fix it so if I grab and start walking and keep walking oh look I went to the edge I can even theoretically walk off the edge and fall down and well fall forever so to fix that the nice quick simple solution you probably already guessed just create a cube get it into position let's see let's get it right about here ctrl shift F remember to move things right - yeah I'll move it forward a little bit clear out that nonsensical rotation and then scale it so grab the scale tool drag it over here and then I'll just uncheck the mesh renderer now I've got a little invisible wall and my guy should not be able to walk through it I'm gonna play move him and make sure that it's far enough out there we go so this is as close as I can come to the camera I can't go past that perfect and I can still walk out here and a block but you know from falling off by all these buildings and stuff so I don't have to worry too much around that you could also put it in extra invisible walls depending on how your map is set up but here we don't really need it the last thing is this really crappy grey sky so that's just because this asset pack that I'm using had the sky set up with no skybox and a solid gray kind of weird but whatever let's just change that I'm gonna actually select here for the skybox material and I'll just grab the default skybox for now maybe in another video grab a prettier skybox but I think that already is much better than solid gray at the very least press play run around one more time and then I think we're kind of wrapped us up there we go so now I've got a character I can run around I can also again still use the controller run around he's nicely tracked stays in view and he's all ready to get his gun hooked up and start blowing stuff up alright so I said in the next video go on to details on how to blow stuff up maybe we'll keep expanding and start making things prettier put in some post effects add in some AI that walks in other fun kind of stuff like that hopefully you're able to keep up if you have questions or something wasn't clear on this part of the guide just let me know drop comment below be happy to answer and then you know if you want to see the next video don't forget to subscribe get alerts and all that so he had notified when it comes out my plan is to just release these no back to back next day so this should be quick um anyway thanks for watching hope you enjoyed it
Info
Channel: Jason Weimann
Views: 140,183
Rating: undefined out of 5
Keywords: Unity, Unity3D, Unity Shooter, Unity3D Shooter, Unity 3rd Person, Unity 3rd Person Shooter, Unity3D 3rd Person Shooter, Unity3d 3rd Person, Shooter Game, Build a Shooter, Build FPS, Unity FPS, Unity3D FPS, untiy build fps, Unity Third Person Shooter, Unity Third Person, Unity3D Third Person Shooter, Unity3D Third Person
Id: sLAyey2WDyc
Channel Id: undefined
Length: 30min 5sec (1805 seconds)
Published: Thu Feb 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.