Hello there and welcome. In this new series we're going to create a first-person shooter game in Unity. This game is going to have all the different features you usually find in FPS games.
We're going to be able to shoot different guns, we're going to be able to pick up weapons, pick up ammo, reload weapons, we're going to be able to ADS, we're going to have a bunch of different weapons and we're basically going to create some kind of generic weapon script. that you're going to be able to modify and create any weapon that you like. You're going to be able to use everything you learn in this series in other games.
So if you have some kind of open world game or a survival game, you can just take everything from this project and use it in any project that you want. Now, another thing that I want to mention is that this series will be a bit more fast-paced. It will be for people that already know Unity and used it. So if you're a beginner, you can definitely join and I'm going to explain the things that I do, but I'm not going to cover the basics. I'm not going to go deep into all of these basic things that you probably already should know.
In this episode, we're going to start by creating the project and create the basic movement scripts for the player. And then we're going to add all the other features an episode at a time. So we're going to start by creating a 3D URP game. You can use another template but this is what I'm going to use.
I'm going to use the 2022.1.23 editor version and I'm going to give it a name and I'm going to create a project. Now that the project is loaded we're going to organize a bit. We're going to move all of these things into the settings folder and we're going to create a bunch of other folders that we're going to need later. Now before we create the player we want to create something for the player to stand on.
So we're going to create a plane, we're going to rename it to be ground, and then we're going to create an empty object and name it player. Now we're going to add a character controller to the player and I'm going to change the settings a bit just so our player feels like a human being. Then inside the player I'm going to create a cylinder and I'm going to name it body. and I'm also going to change the scale a bit. Now inside the scripts folder we're going to create two scripts, the mouse movement script and the player movement script.
So we're going to start with the mouse movement, we're going to have a mouse sensitivity, an x rotation and a y rotation and inside the start method we're going to lock the cursor because we don't need a cursor in an FPS game. Maybe when we have an inventory, but right now we don't need it. In the update, we're going to get the inputs for the mouse. So if the mouse moves on the x-axis, it will get the mouse x. And if it moves on the y, it's going to get the mouse y.
So basically, if we move the mouse up and down, it will be the mouse x. If we move the mouse left and right, it will be mouse y. then we're going to rotate around the x-axis.
So our rotation around the x-axis is equals to the mouse y. So when we move up or down with the mouse, then our body will rotate around the x-axis. Now we're doing minus because if we move the mouse up, we want the body to rotate down. And if we move the mouse down, we want the body to rotate up.
You can change it to a plus if you want controls like a flight simulator or something like that. But in an FPS game, we actually want to do it this way. The next thing we're going to do is clamp the rotation. So if the X rotation, if we move the mouse too much to the top, we want to block the rotation.
So we're going to block it at 90 degrees and we're going to do the same at the bottom. So if we look at our feet, we don't want to be able to look between our legs. We're not that flexible. Next, we're going to do the same thing with the Y rotation.
So when we move our mouse to the left or to the right, it's going to rotate our body on the Y axis. So it's going to rotate around itself. And then we're going to apply the rotation. So we're going to move the transform, the actual body of the player, according to these rotations.
And this is the entire script. It will be more efficient if we don't hard code the actual clamping degrees, but we save them as a public float and then we're going to be able to change them in our inspector. So if we want to change it from being 90 degrees, then we don't have to go to the code, but we can do it from the inspector and we can test it around.
So we're going to place them instead of these hardcoded values. Next, we're going to move our main camera into the player because we want to look from the head of the player. We're going to position it a bit at the top of the player.
And something that I forgot is that the top clamp should be negative 90 because when we look up, it goes to a negative value. So make sure to make it minus 90. Now let's see how it works. So we can look up and down but the sensitivity is a bit low so let's increase it. Yeah it seems about right so let's also change the default value inside the code to be 500 and also make sure you change it in the inspector. Sometimes when you change it in the code it's not updating in the inspector and then you're getting confused so make sure it always updates in the inspector as well.
So we can look up and you can see that there is this clamping. We cannot go over these 90 degrees. And when we look down, the same thing.
Now we're going to create the player movement script. So we're going to have a speed, a gravity, a jump height. We're going to create a ground check because we want to be able to jump.
And we want to check if we're standing on the ground to be able to jump. We're also going to have a velocity for the jump. And we're going to have this Boolean that checks if we're grounded. And we're also going to store our last position because we want to know if we're moving or not.
And that's something we're going to use later on. So in the start, we're simply going to have a reference to our character controller. And I forgot to add it over here.
And inside the update, we're going to actually check if is grounded is true or not, if we're standing on the ground. So we're going to use this physics check sphere, and it's basically just creating a sphere from the ground check position, and simply checks if this ground check position is inside this distance of the ground mask of the layer. So it basically checks if the ground check position is touching the ground layer, which will be the actual ground, the floor, or whatever surface you're standing on.
Then we're going to simply reset the velocity y, because... If we're not jumping, so if we landed and the velocity is not bigger than zero, then we want to make it minus 2f. Because each time we jump, the velocity builds up. So we don't want to let these values build up. Each time we're landing back, we want it to be back at minus 2f.
Next, we're going to get the actual inputs for the y and z. Now after we get the input... We're simply going to store all of this input inside this new move vector. And we're going to multiply the actual input by the direction. The transform right is the red axis.
It's if we go left or right. And the forward, the transform forward, is if we go forward or backwards. And we're simply going to multiply it by the x and the z.
Now we're actually going to move the player. So we use... the character controller and we use this move method and we provide it with this move vector that we created. We will multiply it by the speed and by time delta time because we want it to be consistent with all the other frame rates. Now we want to check if the player can jump.
So if we press on the jump button, which is space, and we're actually standing on the ground, then we're going to create this equation that will make our player jump, right? Because we don't want to jump. if we're already in the air. Otherwise, we will be able to just jump infinitely.
We want to be able to jump only when we're actually standing on the ground. That's why we check if we're also grounded. Then when we fall down, we want to apply the gravity because this is what is going to make the player fall down.
And we can also change the gravity, for example, if we go to the moon or something like that. But the value that I'm using here for gravity is... close to the real Earth's gravity, so it will be more realistic. Next, we're going to actually make the jump.
So the same way we move the player, we're going to use the controller and move it using this velocity. Okay, so maybe we're jumping up and maybe we're falling down. Now we're going to check if we're actually moving and we're going to use this later for other things.
So we basically check if the last position that is saved is not equals. to the current position, then it means that It's not the same position. It means that we're moving, right? We also want to check if we're grounded because we don't want it to count if we jump because we can just jump in the air and it will think that we're moving, but we're referring to the movement of the player on the ground. And if it's not true, then it means that we're standing.
And again, we're going to use this later. And then we also want to set the last position to be the actual position when we stopped moving. So these are both of these scripts and now before we can actually run it we're going to create this ground check so we create an empty object and we name it ground check and we place it at the bottom of our player and we're going to drag a reference inside the player movement script and we also want to create a ground mask but we still don't have such a layer so we're going to create the ground layer and we're going to Set the ground mask to be the ground layer.
And now we need to actually set this layer on our ground. And our ground in this case is the plane that we're standing on. So if you have stairs or if you're inside a house, you want to set all the things that you stand on to be on this ground layer.
And now we can see that everything is working. We can move. We can jump. We can look up and down, left, right.
Now we can move our player. And in the next episode, we're going to start with actually shooting. So thank you for watching. Please subscribe.
And I'll see you in the next episode.