Godot 4: 3D Character Controller

KidsCanCode · Intermediate ·🧒 Coding for Kids ·2y ago

Key Takeaways

This video teaches how to create a 3D character controller in Godot 4 game engine

Full Transcript

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

Original Description

In this series of videos, we look at how to import, setup, and use 3D models in Godot, whether you downloaded them or created them yourself. You can download the art packs we're using from Kay Lousberg at itch.io: https://kaylousberg.itch.io/ Text version and link to code: https://kidscancode.org/godot_recipes/4.x/3d/assets/character_controller/ Want to learn Godot 4? Check out my new book, Godot 4 Game Development Projects. Available now! https://amzn.to/3qcOZ4E Support me on Patreon: https://www.patreon.com/kidscancode
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 0 of 60

← Previous Next →
1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →