Make An FPS in Godot 4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to the gato engine first person shooter project we're going to be building an FPS from scratch walking through every step of making a modern FPS on the gato engine each episode is going to be covering a different feature and we're going to be covering the basics like movements things that get left out like saving or creating settings and gameplay features that you see in modern games like Halo or Call of Duty My Scene will use a solid floor mesh a couple collidable object meshes and a world environment scene you can copy this layout yourself or just create your own just make sure you have a floor and at least one mesh to test Collision I'm also using my custom prototype grid material which is available on the GitHub repo project finally I set up another scene that holds a world environment node and set up a new environment and Sky using an hdri Sky image from polyhaven that is also included in the repo or you can just grab your own with a test level in place the first step is to create a new player controller scene when creating a scene you'll need to set the parent node type in this case we'll use the character body 3D node it has some built-in functionality that will make creating the controller easier and it's designed for first and third person controllers next we'll add a collision shape 3D node and a mesh instance 3D node the Collision node will provide Collision geometry for our controller and the mesh will for now be a placeholder mesh so we can see our controller placement for both we'll use a capsule shape with a height of 2 meters and a radius of 0.5 meters which is a good standard size for a player controller with our character collision and mesh in place let's save our scene and drop our new player character scene into our test level we can then add a material to our placeholder mesh I'm just using a basic red Albedo with a lowered roughness to give it a little bit of a visual interest to run the scene head up to the top right of the editor and click the play button it will ask to set a default scene to run and we want to run our test level of course nothing will actually be shown because we are missing one key node our camera foreign [Music] head back to our player controller where we'll be adding another node this time a node 3D node as the appearance of our camera controller when working within the scene tree it can be helpful to organize your nose to decrease clutter to make the components of your scene easier to reference and sometimes the hierarchy of your nodes will be important here we want the camera controller to act as a container for our camera 3D node it's also important to note that these nodes can be renamed to make referencing them easier within your scripts after you add your camera 3D node you'll see the camera widget in the viewport but once you adjust the height of the camera controller not the camera to match eye level just a bit more we want to keep the camera's local position at zero zero zero so it doesn't move incorrectly when rotating it the vertical placement of the camera controller can heavily depend on the look you're trying to get within your game so feel free to play with this height I've set mine to 1.5 meters now that we have a camera run the scene again and you should now see your scene if you don't see anything make sure you have a camera a world environment node in your scene and a directional light to act as a light source Okay so we've got a Running Scene and a player character but it's static so we need to create a script so we can begin moving around for now we're just going to cover keyboard movement but controller movement will be added in a future episode I like to keep scripts organized near their purpose so we'll create a script folder in our controller folder then go to your player controller scene and right click on your parent character body 3D parent node and Gatto each node can hold a script where you can choose the language if you're using c-sharp for example what node functionality it inherits as well as some settings like class name template and its path we're going to create a script for our player controller using the basic movement template to get us started hit create and go to the script tab the basic movement template gives us some stuff to work with right away including basic gravity a jump action and a movement script we can use these right now but we need to adjust the input mapping references first go to project project settings input map and here it will create five new inputs move forward move backward move left move right and jump these will represent the standard wasd keys and spacebar for the jump with those added head to the controller script and adjust the input mapping to match our new inputs these are referenced by a string using the name you used for your inputs after those are added save the script and run your test scene you should now be able to move using your wasd keys and jump and if you added Collision meshes to your scene you can also test your player's collision with objects now if you find that your player is falling through the floor or running through objects check to make sure you're seeing meshes have a static body 3D node and a collision shape 3D node in addition to the mesh instance node and before we add our Mouse movement for our camera I'd like to add a couple quality of life additions first a way to quickly close the game window when testing to do this add another input map called exit and tie it to the Escape key then head to our controller script and we'll add our first function here the underscore input function fires whenever the engine gets an input event we'll check if the event is the same as our exit input event we created and if true get our scene tree of our level and quit with our new function we can simply press the Escape key to close the window next we'll add some visual polish to our scene by adding a reflection probe node to our player controller camera this will add in real-time Reflections for our materials add the node as a child of our camera node and set the update in the inspector tab to always this will update our Radiance or reflection map every single frame and because our camera will be constantly moving it gives us much better reflection effects alright so movement is working but it ain't no FPS if we can't look around [Music] to begin moving the camera with the mouse we'll first need to set our Mouse mode to capture setting the mouse mode Alters how the game treats the cursor some modes will lock the mouse or hide the mouse and some are better for movement and some are better for UI here we'll use the captured setting which sensors the mouse and hides it next we'll capture how much the mouse moves along the X and Y axis on the 2D plane of our screen so when we move the mouse left our camera will move left and so on first create the following variables these will hold the Boolean for our Mouse input AKA whether the mouse is moving or not a rotation or x value and our tilt or Y value next create a function using the built-in unhandled input function this is similar to the input function we used earlier but it's called later on the reasoning for this is due to UI and mouse interaction which is more important for another episode for now just know that it will fire every time we move our Mouse we'll first check if our event is actually the Mouse Moving And if our Mouse is in captured mode back in our unhandled input function add an if statement if Mouse input is true where we'll set our rotation and tilt to equal the negative relative X and Y movement of our Mouse then let's add a print function so we can test our new code when you run your scene you should now see our horizontal and vertical movement when you move the mouse [Music] now we can take that Mouse movement and apply it to our camera by adjusting its rotation add three more variables to our script this time using export export variables are variables we can adjust right in the editor inspector and are great for making quick adjustments to a script's settings the Tilt variables will let us clamp the vertical movement of our camera to straight up and straight down using the degrees to radian function our camera controller variable will use the camera 3D type hint variables in gato can have type hints which tells the engine how to treat the variables and gives better autocomplete information we're using camera 3D here because we are going to reference our camera 3D node and our export variable we'll create a vector 3 variable named Mouse rotation and another function called update camera with a Delta parameter that will call every frame first we set our Mouse rotation x value to equal the Tilt input times Delta then we'll clamp that vertical rotation using our tilt limits we set earlier our Mouse rotation y value will equal our rotation input multiplied by Delta then we set our camera transform to the rotation of our Mouse make sure our Z rotation is always zero so we don't end up twisting our camera and reset our rotation and tilt inputs then add our update camera function to our process function and make sure you have Delta here as a parameter now when we run our scene we can move the mouse around to change our camera's rotation you'll also notice that we can't rotate more than 90 degrees in the up or down Direction of course moving the camera doesn't change our player's Direction so our movement is not relative to the camera also our camera movement is just way too fast to be usable let's fix our Mouse movement first by adding a mouse sensitivity export variable this is often a setting the user can adjust so we want to expose it I set a value of 0.5 then in our unhandled input function that gets our Mouse input simply multiply each relative value by our mouse sensitivity variable okay so with our camera movement we have a little bit of a problem our directional movement doesn't react to our rotational movement now you might think you could just rotate the player instead of the camera but if you tilt the player you end up changing the orientation so much that forward can become up or straight down now what we want to do is rotate our entire player character left or right first this maintains our forward Direction when moving then we want to rotate only our camera controller up or down allowing our full rotation in any direction but it keeps our player perpendicular to the ground to do this we need to add two more variables player rotation and Camera rotation where we'll split our Mouse input rotation into two separate rotations X and Y axis again we are ignoring the z-axis here next we'll alter our existing update camera function code by adding these lines here setting our player Rotation by isolating the y-axis of our Mouse movement and our camera Rotation by isolating the x-axis then replace our Mouse rotation here with our camera rotation variable so our camera will tilt and add this line to set our player controller transform remember this script is attached to our parents to our player rotation so our player rotates running the scene you should now be able to move rotate Collide and jump like a basic FPS controller if you want to make your player move faster or change the jump velocity you can change the speed and jump velocity variables to export variables and then adjust them in your inspector if you have any issues at all with the tutorial you can grab the entire basic FPS setup project for my GitHub to reference or use later and what I'm going to do is actually keep this project entirely open source so you guys always have something to build off of in the future so in the next episode we're going to be taking a look at adding a Crouch movement future episodes are going to be a lot shorter so information is going to be more digestible and if you would like to sponsor this entire project which would mean a lot you can do so by becoming a GitHub sponsor first off every tutorial video is going to be absolutely free during this process but if you want to get access to these Source projects as we go along you can do so by becoming a sponsor you can also get access to the private Discord Channel early access to the tutorial videos and a chance to vote on what FPS mechanics get covered in the future and honestly guys any support goes a really long way in helping me keep the channel going making content like this and I just really appreciate it so as always thank you and keep creating this is bothering anybody else this little glare thing we'll figure it out
Info
Channel: StayAtHomeDev
Views: 51,139
Rating: undefined out of 5
Keywords: godot 4, godot engine, game development, godot, godot 4.0, godot 4.0 3d, stayathomedev, godot 4.1, godot game engine, godot fps, godot fps controller, godot fps tutorial, godot fps movement, godot fps camera, godot fps camera movement, godot 4 tutorial fps, godot fps game tutorial, godot engine fps tutorial, godot 4 fps, godot 4 fps controller, godot 4 fps tutorial, godot 3d tutorial, godot tutorial, godot 4 tutorial
Id: N-jh8qc8tJs
Channel Id: undefined
Length: 13min 25sec (805 seconds)
Published: Tue Oct 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.