Godot 4: 3D Character Controller

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to your recipes in this episode we're going to create a character controller for our 3D animated character to begin we're going to assume you've already imported your art assets and set up your animation tree if you haven't done that already there's a separate video and text tutorial going over that so check that out first and then come back here when you're all set up and ready to control the character so we have our inherited scene from our character and the first thing we'll notice is since we set our root node to be a character body 3D it's complaining about not having a collision shape so we want to add that and we'll add a collision shape 3D and we're going to use a capsule now because the characters origin is set to its feet which is a really useful thing to do we're going to need to move the capsule shape up and you can position it but the values I've used here I'm making a height of 2.4 leaving the radius at 0.5 and I'm going to move its position up to 1.2 meters and that should give us a nicely positioned capsule shape that covers the character okay the other thing you might have noticed if you're familiar with godot's 3D orientation is that forward should be the negatives the axis and so our character is facing the wrong direction and everything will be reversed in our code so we need to flip this guy around and we're going to do that we're going to use the rig to control the player's rotation so I don't want to set that so I'm going to go inside the rig here and then the skeleton 3D I'm going to set the Y rotation to 180 degrees so our character is facing to the negative Z axis and then that way in our code we can rotate the rig node 3D to change the characters facing direction another thing we need to do is set up our input map so I've added actions here for forward back left right and jump left and right will be strafe we're going to rotate the camera with our Mouse but we'll get to that a little bit later and now let's talk about the camera so I want the camera to follow the player around but of course we don't want the camera to clip into walls and get blocked by things being between the camera and the player so our camera should be able to zoom in and out but it should also detect obstacles and zoom in automatically if there's an obstacle in the way and we can do that if we add a spring arm 3D spring arm 3D is a node that projects a raycast out and whatever is at the end of the raycast is its child and it will move it automatically closer if that raycast collides and so we're going to set the spring length of this to five so that it's nice and long and then we're going to make the margin 0.1 that's how close it will allow things to get to obstacles and we want the camera to be to not be at the Collision point because then it'll be clipping into the wall a little bit and then we're going to set its position up above the player's head so I'm going to set the Y here to 2.5 and there you can see that yellow line there is that camera raycast and so to attach the camera to it we just add a camera 3D as a child and you'll see in the scene the camera is positioned right here but the spring arm will automatically move it to here and then move it forward if there are any collisions in the way all right and that's our node setup our next step is to start writing some code so let's add a script to the night and we're going to talk about the first part of the movement which is just the wasd forward and back movement so here in the script I've got a few different variables set up for speed acceleration and jump speed this will control those values and we've exported them to make them easy to set we're going to grab Gravity from the project settings and we're gonna have a variable to keep track of whether we're jumping and then we have some on ready variables to give us some references to the nodes we want to talk to the spring arm which we just added the rig which is our player model itself which like I said we're going to rotate and then two references to things in the animation tree now if you click on the animation tree you'll see over here under parameters these grounded and jumping conditions and the blend position of the iwr and those are the things we set up here in the animation tree the grounded and jumping transitions where those triggers that would trigger the transitions between the jump uh parts of the jump animation and the iwr is that blend position between idle walking strafing and everything that we would set we're going to set this 2D Vector here based on our player's horizontal motion and so that reference here will let us access these then we also have a reference called anim State that's a reference to that whole tree and that's how we're going to call those transitions between animations you know if we want if we're in this particular animation we want to transition to interact That's How we'll call it next I have my physics process where we're going to add gravity we're going to get the input and then we're going to call move and slide not much to talk about there and then how do we get our input well we're going to apply our input to the horizontal motion which is x and z we don't want it affecting y because gravity is taking care of that so we're going to temporarily zero out our y velocity so that we can just add the vector set the vector directly to our horizontal motion so we get the input vector we apply it to our to get our Direction and we rotate it by the direction that the spring arm is facing because we want to always move forward in the direction the camera is facing and so we're going to get that rotation of the camera which doesn't rotate yet but it will we will interpolate our velocity using our acceleration so that we'll come up to speed and then we'll set that y velocity back so that we haven't lost whatever accumulated gravity we've gotten now let's look at camera control so I'm adding another variable here to control how sensitive the mouse control of the camera is and then down here we're in unhandled input we're going to look for Mouse motion events if we get a mouse motion event we're going to rotate the spring arm around its x-axis which is going to tilt It Up and Down based on the Y movement of the mouse and then we're also going to clamp that angle so it doesn't go all the way around upside down so this will be negative 90 will be looking straight down at the player and 30 degrees will be looking from the ground up and then we're also going to rotate the around Y which will change the facing direction of the camera based on the left to right movement of the mouse so let's take a look at what that looks like and now I can move my camera back and forth I can tilt it up I can tilt it down we get down near the ground and it's clipping into the ground a little bit we'll fix that and now we can see that if we press forward I'm going to no matter which way the camera faces pressing forward is going to go in the direction that that camera faces now we need to rotate our player to look in that direction so what we're going to do here is we're going to say if we're moving we're going to rotate the model which is that rig and we're going to interpolate it by that rotation speed so that we don't instantly snap to that direction and that's just going to make us turn right and face in the direction of the camera like this so now no matter which way the camera is facing I'm going to turn and face in that direction now we need to apply this velocity that we are moving at to our blend space so we play the right animation and so I'm going to take this velocity and since velocity is in global space but we're rotating the player model we need to transform that using the model rotation so that we get the Rel the Velocity in model space and then we're going to set that blend position to it's a vector 2 so we need to set its X and Y and we're going to use the X of the velocity for x and the Z of the velocity for Y and since forward Z is negative in 3D space but in 2D space our blend space positive y was the moving forward we're going to negate that and then we need this to be a value between negative one and one so we'll divide by our Max Speed so if we're going maximum speed this will be one if we're going backwards at the maximum speed it would be negative one and so on and that is going to set the animations when we start running so from above we're running forward we're strafing to the left we're strafing to the right we're walking backwards I mean how about the attack animation so if I go over here to my project settings I'm going to add an attack action which we're going to use the left Mouse button for and then in our animation tree we had three separate attacks so I'm just going to make a list an array here of the names of them and that way in my unhandled input if I get an attack action then we're just going to tell the animation state to travel to a random one of those attacks and if we run it here we'll look at if I every time I click I'm going to play one of those animations so now let's talk about jumping so first of all if we're standing on the floor and we press jump then we're going to jump and and we're going to set those parameters in the animation tree jumping and grounded by the way when you type anim tree.set these will Auto suggest you can also drag them from the inspector over if you want to fill those in so we're going to set jumping to true or set grounded to false because remember grounded will be the setting to that true will be the transition to hit back on the ground now the thing is we need to know also what are is on floor status was the previous frame so we've added a variable here called last floor that we're going to set every frame to be is on floor and that way we can detect if we're on the floor now if we're on the floor now and we weren't last frame then we finished jumping so we should set jumping to false and set grounded to True which is going to trigger that transition to The Landing animation and then there's also another condition we could have which is if we're not on the floor and we are not jumping then we must have just Fallen we stepped off the ledge or something like that so we want to jump straight to that jump idle animation and set grounded to false right so that's that's going to call oops animation tree that's going to call this transition right instead of bending our knees and jumping up we walked off the ledge so we're going to jump straight to jump idle okay so over here in the test scene I've put the player up on top of the wall so we can test that out so if we play this we'll see when we're standing here if we jump we see the jump animations play but if I run off the wall right it starts to fall and you might have noticed if I play it again when you walk off the wall oh that that sudden transition to the falling animation that's not that great so if we go to our animation tree and we pick this transition we can give it a small Crossfade time so that it doesn't look so jarring and that's going to look a little better when we run off the wall by the way if you're experiencing that glitching when you get down low that the camera is snapping inside the player's helmet that's actually happening because the camera is or the raycast from the spring arm is hitting the capsule shape of the player and what we really want is we don't want the camera to pay attention to that at all so really what we need to do is put the Knight on a separate Collision layer because the spring arm is only checking Collision mask layer one and when we set up the full game we'll assign these layers so different things will be on different layers and we can really be organized about this but for right now you just want to make sure your player is on a different layer than the layer that the spring arm is checking and then that's going to fix that because now you can go all the way down to the camera and it doesn't clip into the player and that's it that's the start of our character controller now you can start laying out your dungeon with all of the dungeon assets and give yourself something interesting to walk around and explore and in future episodes we will talk about how to add enemies how to add doors that we can open and close chests combat all that kind of stuff so make sure to like And subscribe so you will get notified as soon as I add the next part I'll see you next time you can find this in many other good old recipes tutorials examples and tips at good or recipes.com don't forget to like And subscribe on YouTube to get the latest video tutorials and if you'd like to help support these efforts please consider clicking the patreon link thanks
Info
Channel: KidsCanCode
Views: 17,177
Rating: undefined out of 5
Keywords: programming, coding, tutorial, lesson
Id: AW3rT-7J8ag
Channel Id: undefined
Length: 14min 50sec (890 seconds)
Published: Sat Sep 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.