3D Movement in Godot in Only 6 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
making your first 3d game can be daunting with the additional third axis just trying to rotate your character can be a nightmare in this video you'll see how to create a third person character that moves relative to the camera and jumps we'll also take a look at rotating the character model towards the direction of movement there are two physics bodies we use to move objects in 3d rigid body and kinematic body rigid bodies have realistic physics they're quick to set up but hard to master kinematic bodies give you more control and they come with handy functions ideal for third person movement in a new scene we create a kinematic body with a collision shape we've added our own animated character to the scene but you can use any model or mesh instance instead two common collision shapes for characters in 3d are capsules and cylinders capsules have no hard edges making your character move smoothly on slopes and corners a character with a capsule can run up small stairs but it also slides off platforms when standing on edges cylinders can stay on edges but their movement won't be nearly as smooth on uneven terrain and stairs i recommend a capsule to get started they're generally easier to use with the collision shape selected we add a capsule shape and rotate it to fit our character we also reduce the shape radius to 0.5 to match our character better in 3d you need a camera to render the game world to make the camera follow and orbit the character we add a spring arm node to the player with a camera child the spring arm constrains the camera to the player at a distance it also makes the camera collide with the world preventing it from getting inside walls we'll get back to setting up the spring arm later when we rotate the camera the next step is coding the character movement we add a script to the kinematic body node kinematic body gives us the move and slide with snap function it handles movement on slopes for us keeping the character grounded while running we start with variables for movement speed jump strength and gravity the move and slide with snap function expects a velocity and a snap vector so we define these two variables next the snap vector points to the ground to ground our character when jumping or falling we turn off snapping by setting it to zero we cache the player model and the spring arm into on ready variables in physics process we start by calculating the player's desired direction to get precise inputs on controllers while supporting keyboards we use input get action strength the left forward back right and jump input actions are defined in the project's input map now because the camera can open around the character we also need to rotate the input direction to match the camera's look direction that way the character always moves forward back or sideways we rotate the move direction by the spring arm's y rotation this makes the move direction relative to the camera we normalize the move direction so going diagonally doesn't make us go faster we use the direction to set the velocity variable on the ground every frame we also want to pull the velocity down using gravity applying gravity lets us detect when the character is on the floor using the is on floor function we can use the snap vector and is on floor to check if we just landed if the character is on the floor and the player presses jump then they're jumping jumping should have priority so we set the y velocity to jump strength and we zero out the snap vector to turn off snapping when landing we point the snap vector down to snap the character to the ground next we use the velocity and snap factor in a call to move and slide with snap this function needs our velocity snap vector and the up direction we also tell it to stop on slopes without sliding down the function returns an updated velocity that takes collisions into account resetting the y velocity to zero when landing we store this value in our velocity variable before moving on to the camera script in process we update the spring arm to move with the character every frame you'll see why shortly now we've got code to move relative to the camera but the camera doesn't rotate yet first we set the spring arm's properties the spring length controls how far the camera moves away from the character you also want to increase the margin to 0.2 or larger to give the camera more room to collide the .01 default tends to clip inside walls next we attach a new script to the spring arm node to control the rotation we add an exported variable for rotation speed by calling set as top level true in the ready function the camera can move independently from the character it won't inherit its position rotation or scale that's why we added a line in the character script to reposition the camera every frame we capture the mouse cursor inside the window by calling input set mouse mode this hides and locks the cursor to the game window you can release the mouse by passing a different mode instead in unhandled input we check the event is mouse movement before continuing moving the mouse up and down on the screen pitches the camera up and down left and right mouse movement rotates the camera around the y axis the clamp function prevents the camera from rolling over you can change the degree amounts here to limit the camera's pitch meanwhile the wrap f function makes the angle wrap between 0 and 360 degrees so the rotation doesn't accumulate finally we want to rotate the character model to match its movement back in the character script we check the velocity length first so we don't face forward when we stop moving then we store the velocity z and x in a vector 2 as our look direction i know it looks backwards trust me we need the opposite of what it normally returns here finally we set the model's y rotation to the look direction angle the angle function turns a vector 2 into radians we assign this to the rotation y property so it faces the angle of movement and there you have it a 3d platformer character with a rotating player model and camera we're making a course to teach you and people you love how to make games from absolutely zero with godot along with the course we're developing a free and open source app that'll help everyone practice code right in their browser we're on kickstarter to make this project a reality you can get the course at half the final price but be quick because the campaign ends on october 31st check out the link in the description to get all the details be creative have fun and we hope to see you again in the next one
Info
Channel: GDQuest
Views: 23,313
Rating: undefined out of 5
Keywords:
Id: UpF7wm0186Q
Channel Id: undefined
Length: 6min 11sec (371 seconds)
Published: Sat Oct 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.