#1 FPS Movement: Let's Make a First Person Game in Unity!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
g'day everyone and welcome to this video this  is going to be the start of a series of videos where we work together to create a first  person game in the unity game engine while some elements of this series are targeted  towards creating more of a first-person shooter there are many skills that we will learn that  can be applied to any kind of first person game we'll cover topics ranging from interaction to  enemy ai dynamic music post processing and a whole bunch more in this video we're going to create  our own character controller using unity's new input system i'm really excited  to share this process with you so let's get straight to it in a new  project let's begin by creating a capsule and we'll rename this to player over in the  inspector we'll assign up the player tag as well we'll delete the capsule collider and  we'll add in a character controller now let's grab our main camera and  parent it to the player game object and we'll set its position to be 0 on the  x-axis 0.6 on the y-axis and 0 on the z-axis this will position the camera around about  where our player's eyes should be now we'll head down into the project window create a new  folder and call this prefabs and we'll create a prefab for our player by dragging our player game  object into that folder now we want to import the input system so let's head up to window and we'll  open up the package manager we want to navigate to the packages kept in the unity registry so then  we can scroll down and import the input system we'll get this warning popping up asking if we  want to disable the old input manager for this video we're going to build our controller  from the ground up so we want to hit yes and this will restart unity and we'll make sure  that we save our scene before the program restarts now not all games will need to use the new input  system for simple control schemes you can get away with just using unity's built-in input manager but  there are a lot of great features that come with the new input system that allow us to easily  create professional control schemes and we'll cover a lot of these as we move through the series  once unity is back up and running we can close the package manager and we'll head over into assets  in here we'll create a new folder called input and inside that folder we'll right click go create  and all the way down the bottom here we've got our input actions so let's create a new input actions  and we'll call this player input if we double click and open that it'll open up a new window  which is our action editor window and you can see that this window is divided into three sections  we've got action maps actions and properties so first up let's look at action maps an action  map is a set of actions that the player can do depending on the current state of the game for  example we could have an action map called on foot and this would contain every action that  the player could do while walking around then if we had a driving mechanic in our game  and we want our character to jump in the car and speed off we would need to create a new action  map and we can call this driving this action map would contain a whole new set of actions and  properties but now we'll just focus on creating a basic action map for our player so let's get  rid of driving and we'll just keep on foot so if we select on foot we can see that in our  actions tab we have a new action here so let's start by setting up our player's movement action  we can rename this new action here to movement and if we open this up we can see  that it currently has no bindings an action can have multiple bindings in order for  it to be triggered and we can add a new binding by clicking the plus button up here which will bring  up a list of different bindings to choose from or if we right click on the action we can  see that the list includes one more option and that is a 2d vector composite this will  return a vector 2 that has been normalized between negative 1 and 1 so it's great for any kind of  directional input so let's add that in there and we can see that it already has spots where we can  set up bindings for up down left and right this first 2d vector we'll use for our wasd keys and  in order for it to work with our gamepad as well we can just duplicate this action and rename it to  left stick lastly we'll delete this empty binding up the top here because we don't need that now  let's look at the properties tab this is where we can set up the key bindings to trigger an  action we'll start by setting up the keyboard bindings for our movement action here we want the  up direction to be assigned to w on the keyboard and we can just search that by typing it into the  drop down menu here we want down to be set to s we want left to be set to a and we want right to  be set to d now we can do the same thing for our left stick for our up direction we want that to be  set to left stick up and we want to make sure that we select gamepad for all of these ones so that  we're not targeting a specific device we will be targeting all gamepads that are compatible  with unity down we want set to left stick down left will be left stick left and right we want to set to left stick right  okay so that's all of the bindings set up for our movement action now let's go ahead and  create a new action and this will be for our jump so let's call it jump and again we want  two bindings for this action so we'll create a new binding by pressing the plus button on the  action itself and we'll just select add binding the first binding we want to set to space and the  second binding we want to set to button south on the gamepad so again this is really handy because  we're not actually targeting a specific controller instead button south will just return the lowest  button on the right hand side of a controller so a for an xbox b for switch and cross for  playstation now at the top of the action editor window you can see that there is a little asterisk  next to our player input this is because our asset hasn't been saved yet so we can click save asset  and then head over into the inspector and hit generate c-sharp class we'll click apply and that  will create a c-sharp class that we can reference from inside of our scripts now if we look at the  player input action we open this up down here you can see that we've got two extra sub files here  we've got one for on foot jump and one for on foot movement so any new actions that we add into our  action map here will also be created as a subfile of our input actions asset okay so that is all  we're going to be doing with the action editor for the moment we can go ahead and head back to our  scene view and while we're here we might as well set up a little scene so i'll just quickly set  up a floor and some cubes to surround the player and i'm just going to dim the light a  little bit as well alright with that done we'll head into our assets folder and  we'll create a new folder for our scripts in here we want to create a new c-sharp script  and we'll call this input manager and this script will essentially be a central point where  we can channel all of our inputs through at the top of the script we'll make sure  that we're using a unity engine.input system we're going to set up some values at the top of  the class here we want a private player input and we'll call this player input and this is  a reference to the c sharp class that we just generated next we want to have a private layer  input dot on put actions and this is a reference to of course the on foot action map so we'll  name this on foot next we want to change start to awake and then here we want to create a new  instance of our player input class so player input is equal to new player input and on foot is  equal to player input dot on foot now in order for us to use any of these inputs we need to enable  our action map so we can do this in on enable we'll type in private void on enable and all we  need to do is call enable on our on foot actions we want to do the same with disable so private  void on disable and then on foot dot disable now we'll save that script head back into unity just realized i popped my prefabs folder inside  the scenes folder so i'll just fix that up there and we want to head into our scripts folder and  create a new c-sharp class called player motor this script will contain all of  our player movement functionality so we'll open up that script at the top  of the class we want a private character controller we'll just call this controller we  want a private vector3 for our player velocity and we want a public float for our speed and we'll  set this to equal something like five now we want to assign our controller in start so control  is equal to get component character controller now we want to create a new public  void method and we'll call this process move this will take in a  vector 2 which we'll call input now what this function is going to do is  receive the inputs from our input manager script and apply them to our character controller  so in here we want a local vector3 and we'll call this move direction and we can set this to vector3.0 then we want  to set move direction dot x to equal input dot x and we want move direction dot z to equal  input dot y so what we're doing is we're grabbing the y component of the 2d vector and  applying that to the z-axis of our character so we're translating that vertical  movement into forward backward movement then all we need to do is call the move  function on our character controller so controller dot move and then here we want  to pass in transform dot transform direction in this we'll pass in our move direction  and then we'll multiply the result of that by speed times time dot delta time now we'll  save that head back into our input manager script and we're going to create a property for our  player motor script so at the top of the class we want a private player motor and we'll call  this motor we'll assign it in the awake function motor is equal to get component player motor and  then we want to change update to fixed update and in here we'll tell the player motor to move  using the value from our movement action we can do this by typing motor dot process move and in  here we'll pass in on foot dot movement and we can read the value by typing in read value and the  value we are going to be reading in is a vector 2 and there you go we'll save our script head back  into unity we can drag our scripts onto our player now let's head up to window under analysis we'll  open up the input debugger and this will open up a window that's going to show all of the devices  you have currently attached to your computer on mine i've got keyboard and mouse a wacom  pen and an xbox controller so if i open up the keyboard window over here and hit play will move  forward you can see that it's registering that i pressed the w key now if i move backwards you can  see it registers the s key so the input debugger is an incredibly helpful new feature which  displays the current inputs unity is receiving so now if i open up the xbox controller  window and move forward on the left stick you can see that the player is  moving with the controller as well perfect but not quite because if i close these  windows head into the scene view and lift our player just above the ground you can see that they  are not falling back down so we're not finished yet we still need to add in gravity so we'll  stop playing there head back into our player motor script and at the top we'll create a new  private boolean and we'll call this is grounded now we also want a public float for our  gravity gravity is usually set to negative 9.8 now we'll head back down into our process move  function and we want to set the player velocity dot y plus equal to gravity times time dot  delta time now this will apply a constant downward force to our player but that  force will keep accumulating every frame even when we are grounded so we can test this out  by calling move on our controller once more and passing in player velocity times time dot delta  time we'll also debug our player velocity.y to see how much force is being applied to our player  so we'll head over into console and hit play you can see that the downward  force is just growing and growing now that's not an ideal functionality because  if we're trying to jump with a force of 10 then a downward force of negative 70 is just  going to keep our player stuck to the ground so to fix this we want to make sure that we are  assigning is grounded to equal controller dot is grounded so we are getting that every single frame  then in between applying gravity and telling our controller to move we can check if is grounded  and our player velocity dot y is less than zero then we can just set our player velocity  dot y to a small negative value like negative two now if we save that and go back and test again  we'll hit play you can see that the downward force in our player is at a constant value of negative  two and that's great that's exactly what we want we'll head back into our player mode script  and at the top we'll create a new public float for our jump height we'll set this to  be a value of 3 and then we can head all the way down to the bottom of the script and  create a new public void function called jump now in jump we first want to check if our player  is grounded then we can set our player velocity dot y to equal math f dot square root and  in here we'll pass in jump height times -3 times gravity now we can save that head over into  our input manager and subscribe our jump function to the jump action to do this in our awake  function we can just type in on foot dot jump dot performed is plus equal to ctx and then we'll  create a pointer to our motor dot jump function now this syntax might be a little bit  confusing if you're not familiar with events but basically what we're doing  is anytime our onfoot.jump is performed we're using a callback context or  ctx to call our motor.jump function all of our actions have three states that we can  subscribe code to we've got performed we've got started and we've got cancelled so depending on  what type of functionality you're going for with a particular action we'll change the callback  context that you are listening for where our jump function we're listening for performed so  we'll save that and we'll head back into unity and if i hit play now we can walk around and we  can jump oh the jumping is a little bit too high so we can lower our jump height  here down to something like 1.5 okay awesome now we want to give our  player the ability to look around back in our player input asset we'll create a  new action and we'll call this look we want to change the action type to be value and we want  to change the control type to be a vector 2. we'll add in an extra binding and we'll set  the first binding to be the delta of the mouse position and we'll set the second binding to  be the right stick of the gamepad we'll save our asset and back into our project folder  and we'll create a new c sharp script called player look we'll make sure to  drag this script onto our player and we'll open it up in our code editor now at  the top of the class we want a public property for our camera and we can just call this cam  a private float for our x rotation and we'll set this to equal zero f now we also want  two public floats one for our x sensitivity we'll set this to equal 30  and one for our y sensitivity and we'll set that to equal 30 as well  we'll get rid of start and update and we'll replace that with a public  void function called process look this just like our process move function will  take in a vector 2 and we'll call this input and in here we want to create a local float  for our mouse x we'll set this to equal input.x and a local float for our mouse y which will  equal input.y now we can calculate the camera rotation for looking up and down to do this we  can type in x rotation is minus equal to mouse y times time dot delta time and then we can multiply  the whole thing by y sensitivity and we also want to clamp the x rotation so x rotation is equal  to math f dot clamp and the first parameter is the value we're clamping the second parameter is  the minimum value that we want it to be which is minus 80 and the third parameter is the max so  we're clamping the x rotation value between -80 and 80. now we can apply this to our camera's  transform we'll get our camera dot transform dot local rotation and we'll set this to equal  quantum dot euler and the value we're going to be passing is the x rotation on the x-axis and 0  for the other two axes now finally for our process look function we just need to rotate our player to  look left and right and because this script is on our player game object all we need to do is type  in transform.rotate vector3.up and we'll times this by mouse x times time.deltatime and then  we'll multiply the whole thing by x sensitivity okay that's all we need for our process look  function we'll save that we'll head back into our input manager we'll create a private property  for our player look script call it look we'll make sure that we assign that in the awake function  here look is equal to get component player look and then we can do basically the same thing  that we did with our motor dot process move but instead of doing it in fixed update we want  to do it in late update so void late update so we can just duplicate this line of code  paste it into late update and we can change motor to look change process move to process  look and change our movement action to be our look action so now we're passing  in the value from our look action into our player look script and running all of our  code in that script there and that's it let's head back into unity and pop our camera into our player  look script hit play and let's see if it works all right awesome so now we've  got a character controller that can walk around that can look around and they  can jump as well so that's everything that we're going to cover in this video but i've also gone  ahead and given the player the ability to sprint and crouch using this code here so have  a go at setting this up for yourself i hope you found this video useful remember  if you have any questions or difficulties just let me know in the comments section  and i'll do my best to help you out and in the next video we're going to continue on by  creating a really robust interaction system so stick around and i'll see you in the  next video thank you so much for watching
Info
Channel: Natty GameDev
Views: 781,947
Rating: undefined out of 5
Keywords: FPS, Unity, First Person, Shooter, Tutorial, Unity3d, Game Development, Games, Gaming, Beginner, unity fps movement, beginner fps movement unity, how to make fps movement, unity fps movement tutorial, first person, movement, rigidbody movement unity, unity character controller, development, character controller, programming, unity wallrun, unity tutorial, unity fps, character, move
Id: rJqP5EesxLk
Channel Id: undefined
Length: 21min 38sec (1298 seconds)
Published: Wed Dec 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.