Godot 3.2 - First Person FPS Character Controller Tutorial In 6 Minutes 3.1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you've played ten minutes of Rainbow six siege and now you think you have what it takes to be an operator in this Godot 3.2 slash 3.1 tutorial I will show you how to create a basic first person character controller that you can use to make all kinds of first-person shooters start with an empty scene and create a new kinematic body node call it whatever you want next create a collision shape node as a child of the kinematic body give it a new capsule shape then under the transform tab rotate it 90 degrees on the x-axis then click on capsule shape and set its radius to 0.5 and its height to 1.5 right click on your kinematic body node and create a mesh instance child give it a new capsule mesh rotate at 90 degrees on the x-axis then click on the small mesh preview window and set its radius to 0.5 and its height to 1.5 after that right click on your kinematic body node and create a spatial node as its child call it head right click on head and add a camera node as a child of it click on the head node and move it up to about where the head of your character should be make sense right now attach a new script to your kinematic body node and name it whatever you want we're going to create quite a few variables here so feel free to pause the video if you need time to copy all of this down we're going to write var speed equals 7 var acceleration equals 20 var gravity equals 9.8 var jump equals 5 for Direction equals vector 3 var velocity equals vector 3 var fall equals vector 3 on ready var head equals dollar sign head oh and I almost forgot var mouse sensitivity equals 0.05 the first thing we need to do is make the camera follow your mouse movement to do this we're going to create a new function called func underscore input event in this function anytime there is an input like if you press a button on your keyboard or you move your mouse or you do anything in real life to tell the game to do something that input is marked as an event first we're going to rotate the character horizontally so right if event is input event mouse motion so if the game detects mouse movement rotate underscore Y beg to read minus event relative x x mouse sensitivity this takes how much the mouse has moved horizontally multiplies it by the mouse sensitivity and applies that movement to the horizontal rotation of the character now you can use your mouse to rotate the character horizontally but we also need vertical rotation we'll go back into the script and write rotate underscore X Doug - rad - event relative y x mouth sensitivity will also go into a ready function which is where you put code that you want to run the moment the character is loaded into the game and right input that set mouse mode input Mouse mode captured this hides your cursor and locks it inside the game window so your range of rotation isn't limited by the window size now you can aim in all directions but you'll notice things start getting a bit wonky this is because rotating objects in 3d is complicated and rotating something on multiple axes can mess with the orientation of the other axes to fix this we need to keep the horizontal and vertical rotations separate and we do this by changing rotate underscore X to head rotate underscore X now we can aim our character correctly but if we rotate the camera too far vertically it will turn upside down so to fix this right head rotation X equals clamp head rotation X egg - rad - 90 Dec - rad 90 now you can aim all the way up and all the way down and you can't make the camera flip upside down next we're going to make the character move to do this we're going to create a new function func underscore process Delta this function updates every frame and it's where we're going to put our movement code we're going to go into project and project settings and in the input map tab we're going to right move forward in the action box and click Add we'll do the same for move backward move left and move right we'll also add an action called jump then we'll assign whatever key we want to each action by clicking on the plus on the right selecting key and pressing the key we want I use WASD and spacebar then we'll return to our process function and write if input dot is action pressed move forward by the way if you're wondering what this particular line of code does that's just a piece of code that makes the mouse cursor visible when you press the Escape key I was going to talk about it in the video but then I forgot to put it in the script so I have to talk about it now direction - equals transform basis Z so when the forward key is pressed the desired direction of travel is set to the direction the character is facing to move backwards it's pretty much the same thing right Elif because the character can either move forward or backwards but not both at the same time input is action pressed move backward direction plus equals transform basis Z for left and right movement it's pretty much a copy/paste of the previous two chunks of code but you'll be using move left and move right and you use transform that basis X instead of Z pause the video if you need time to write all of this down if we try to run the game now the character won't do anything and that's because all we've done is established which direction we want to move we haven't yet given the instructions on how to move to fix that first we need to write direction equals Direction normalize this is done so that the character doesn't move faster or while I'm moving diagonally after that we write moving slide direction times speed vector 3 dot this is wrong but let's see what happens if you run the game I can press a move button to move but now the character will keep moving forever because we've set a direction with nothing to set the direction back to zero when we let go of the button to fix this go to the top of the process function in right direction equals vector 3 this resets the direction to zero every frame except on the frames where you're actively holding down a movement key and setting a direction to move to make the movement more realistic we're going to add smooth acceleration by writing velocity equals velocity linear interpolate Direction times speed acceleration times Delta then we add velocity equals in front of moving slide and we change direction times speed to velocity now the character smoothly accelerates from zero to max P however feel free to ignore all of this if you like the Arcadia movement style and finally we're going to make the character jump to do this add a new line of code under Direction equals vector three if not is on floor fall dot y minus equals gravity times Delta and under that write if input is action just press jump fall dot y equals jump then underneath everything right moving slide fall vector three da watch my two minute video on gravity and jumping if you want more information about how this works congratulations you now have a first-person character controller that can move aimed jump and if you're smart enough and do your taxes and fill the empty void in your heart in the next video in this tutorial series I'll show you how to make your character teleport around like tracer from overwatch if you liked the video like it subscribe to share it Bella and comment it thank you have a nice day
Info
Channel: Garbaj
Views: 84,171
Rating: undefined out of 5
Keywords: Godot, Godot 3.2, Godot 3.1, Godot 3d, Godot Tutorial, Godot tutorials, Godot how to, game development, game dev, first person character controller, first person shooter, fps, rainbow six siege, rb6, overwatch, third person shooter
Id: J7AW8CLWsJc
Channel Id: undefined
Length: 6min 22sec (382 seconds)
Published: Wed Feb 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.