How to Create SMOOTH Player Movement in Godot 4.0
Вставка
- Опубліковано 16 бер 2023
- In this Godot 4 tutorial I go over how to create smooth player movement step by step, and i believe having really good character movement is one of the most important things to have correct in your game. Good player movement can make your godot 4 game go from mediocre to crazy good.
----------------------------------------------------------------------------------------------------------------------------------------
Thank you so much for watching I really hope this video helped.
if you did enjoy then please go and click that subscribe button to help out the channel. I means so much and I love your feedback in the comments to let me know what it is that you enjoyed. Again thanks so much and I would love to see you again!
have a great rest of you day and of course be safe :)
- thanks DevWorm,
----------------------------------------------------------------------------------------------------------------------------------------
The code can be simplified with `Input.get_vector` and `velocity.move_toward`. You get the same effect with less lines of code.
hey im totally new to this, what lines would i replace with these two?
Thanks a lot!
@@JAMRIOT
velocity = Input.get_vector("left","right","up","down") * SPEED
move_and_slide()
This is so cool, thanks!
@@sheepcommander_ I do apologize but I am still having trouble figuring out what to do to simplify this code. If someone could provide a little more detail I'd be so grateful!
I am not finding the lines mentioned and no matter what I try to change it just bugs out.
Thank you so much! I been looking ages for a guide to set up a character body movement identical to rigid body. This is going to be a great foundation to play around with. You made my Monday evening a blissful one. :)
Thank you so much I cant tell you as someone who has just got into coding how much this has helped with my players movement your way of explaining things is amazing and I left and sub and like and about to go watch more of your vids! Keep up the good work!!
thanks bro, looking to upload many more tutorials on all godot topics
Really useful for creating slippery ice floor movement. I was setting new velocities instead of adding and substracting to it, which gave me a lot of issues on the slip behaviour when turning directions
Thank you for another great video. You have a knack for explaining things that is easy to follow and understand. I am looking forward to more of your videos on Godot 4.0. I really like that you explain what is different in Godot 4 from Godot 3 and I can't wait to see what you cover next. You be safe, and have a great day.
Glad it was helpful! Looking to make many more godot 4 tutorials, and if you need anything let me know!
Stumbled upon this when looking for something else, but damn, it helped me a lot.
i was worried about scaleability with my previous approach on player movement but your code eliminated that fear and got me excited to build on it.
Thanks a bunch for the great video!
so happy to hear that!
Your tutorial really helps me in the journey of learning to use Godot!
Thank you so much for this tutorial excellent descriptions and great code!!!
For people around still
Simplified code with just 7 lines of code:
func player_movement(input, delta):
if input: velocity = velocity.move_toward(input * SPEED , delta * ACCELERATION)
else: velocity = velocity.move_toward(Vector2(0,0), delta * FRICTION)
func _physics_process(delta):
var input = Input.get_vector("ui_left","ui_right","ui_up","ui_down")
player_movement(input, delta)
move_and_slide()
it says speed not declared
@@jiboodie5344 add the following lines after extends CharacterBody2D:
@export var SPEED = 100
@export var ACCELERATION = 200
the @export keyword allows you to change these values on the editor itself
Add a speed variable to your character @@jiboodie5344
Correct me if I'm wrong but if you're running your movement functions inside _physics_process shouldn't you go ahead and get rid of delta in move_towards?
Its because you are running it inside the physics process that you do want to have the delta there. Physics process is called every frame and delta is the time passed between the last frame and the current frame. If your computer is running at let's say 60 FPS, your delta value would be usually something like 0.01667, but if it was running at 120 it would be something like 0.083334.
If you dont actually put delta there, the player would be moving at the top speed, every frame, and instead of having that smooth movement the player would move extremely faster than it should.
Putting delta there guarantees that the player will have that smooth movement and that it will work as intended, no matter the FPS your machine is running at.
Was hoping for vids with godot 4, love it thx
many more godot 4 tutorials coming really soon!
your so good at explaining this stuff thanks !
I was able to use this and tweak it just a little bit, but man, this is really super smooth movement. Thanks for this!
So glad to hear that!!
Thanks, great tutorial. ❤
Glad it was helpful!
Thank you. This is the first coding tutorial I’ve followed that gave me a tangible result.
so glad it was able out!
i did every thing he did and i cant make the player move and i dont have any errors popping up what could i have done wrong
For anyone who wants to use animations with their sprite and is struggling, I added a few lines of code based off of dev worm's other video to help:
Declare at top:
var current_direction = Vector2.ZERO
var current_animation = ""
Add to existing code:
func _physics_process(delta):
player_movement(delta)
play_direction_anim(delta)
func play_direction_anim(delta):
var anim = $AnimatedSprite2D
var new_animation = ""
if input.x != 0 or input.y != 0:
current_direction = input
if current_direction.x > 0 and velocity.length() > 0:
new_animation = "walk_right"
elif current_direction.x < 0 and velocity.length() > 0:
new_animation = "walk_left"
elif current_direction.y > 0 and velocity.length() > 0:
new_animation = "walk_down"
elif current_direction.y < 0 and velocity.length() > 0:
new_animation = "walk_up"
else:
if current_animation.ends_with("_right") and velocity.length() == 0:
new_animation = "idle_right"
elif current_animation.ends_with("_left") and velocity.length() == 0:
new_animation = "idle_left"
elif current_animation.ends_with("_down") and velocity.length() == 0:
new_animation = "idle_down"
elif current_animation.ends_with("_up") and velocity.length() == 0:
new_animation = "idle_up"
if new_animation != current_animation:
anim.play(new_animation)
current_animation = new_animation
I appreciate you taking the time to explain what your code means.
happy to hear!! hope it helped! thank you!
Thank you soo much for the tutorial... Super easy to follow! I was following your other tutorial as well "how to create an rpg in godot" and I'm not sure on how to add the animations to this movement? If you could help I'd really appreciate it!
Thanks this is really helpful.
Thanks for tutorial!
my debugger goes off the charts when i try to move. i coded everything as you did and it all checked out. but when i try to play the character doesnt move
Super nice tutorial! Thanks!
of course anytime!
When I tried it. I could only move a certain amount before permanently decelerating. I would have to hit the arrow keys again to keep moving (and go even faster). It's cool, but it doesn't look like what you did in the video (or at least I don't think it does).
Thank you, it's a great tutorial!
so happy to hear that!! thank you!!
thanks for the tutorial, I learnt a lot but when I try to run the program I get the error:
invalid operands 'Callable' and 'Vector2'
I've double checked the code and don't think anything is wrong but it won't work, this error has been brought up in a previous comment as well, could someone please help?
very easy to understand thank you!!
so glad to hear!! thank you! happy it helped!
this was very well explained and easy to understand for a beginner
so happy to hear! thanks!
Yes man glad your back. Let’s get this goin
Btw my game is almost done just need to work my inventory
inventory is pretty complex but im working on a video right now for the inventory system and it should be very helpful for you.
@@dev-worm I Appreciate it
Cant describe the smile on my face after it worket thanks
so glad i could help :)
This is pretty neat
This whole code is without delta time, I know that move_and_slide function adds delta but only to the parameters inside right? So what are the places to put the delta if anyone konws?
Your video si already very clear, but what might make it clearer is adding a few comments along the way.
Basically when you define the if clause to handle the acceleration and the deceleration, adding a simple comment above the if and else to say "# handle acceleration" and "# handle deceleration" would help clarify things.
But that's just a bonus, the video is pretty clear as it is already, thanks !
Thanks🤘
You have this video on smoother movement, and it works! but you also have a video on animation in the other RPG series, how do i combine the two?
i want this guys movement and the characters animation.
hey bro, how would you put in code a way to change sprites or animations etc? like, for example, putting a walking animation when the character walks and such, I wrote the code and I didn't find a way to implement this "mechanic" of changing sprites and such, if you can answer me I'd appreciate it, thanks, great video
you want to declare the animations as a variable you can call so for me " @onready var _animated_sprite = $AnimatedSprite2D " then in your if function you call it to be played here is an example " if Input.is_action_pressed("right"): " and to idle you assign the idle like this " elif Input.is_action_just_released("right"):" you then repeat this for all your directions
_animated_sprite.play("walkRight") _animated_sprite.play("idleRight")
Thank you so much :D
thank you!! glad it helped!!
do you have any idea how to make sure the player can't leave the erea
it keeps telling me it cant find limit_length on base Vector2?
I'm looking very much towards tutorials on shaders for godot 4, and just more godot 4.0 tutorials in general :))
Going to be uploading like every other day for godot 4 tutorials
i love your video it my first time in godot and was awesome
thank you so much! happy to hear that!! if you ever need anything else then please let me know!! I am more than happy to help!
Thank you so much!!!!!
of course! anytime! thank you!
how to use a variable twice in one script ? plz telll me i need to know
How would i go about movement without diagonals?
the "velocity" variable at 11:00 is not declared anywhere, what am I missing?
I found the error, I updated to 4.0 and its fixed
Thank you very much
of course anytime.
im porting my scratch game to godot and this is gonna help so much!
Good luck!
is there a resource that goes over all of the built in functions? No matter how good you get at coding you cant be a mind reader and know whats in all of these functions to use them. And wtf is delta?
I have a very niche problem and I can't figure it out alone
In the game that im making my character can spin and move with key presses
I got to a point where I have this video's movement and the rotation working (pressing f to rotate to the left and g to rotate to the right) but if iI hold both rotating buttons at the same time i can only move up for some reason .
Can somebody help me with this?
edit: It was a hardware issue :]
I'm getting an error when I press play. It says: "Invalid Call. Non existent function "is_action_pressed" in base "Vector2". Tried to fix it but I don't know what to do.
I had the same thing happen, I just had to capitalize the i in Intput.is_action_pressed
sorry for commenting a whole year later, but i have a problem that needs to be fixed, i can't collide with anything, i tried using the move_and collide script, but it didn't work, how would i make it work?
do you have a collisionshape2d within the scene? you will still beable to collide with the environment even with move and slide as long as all the objects including the player have a enabled collisionshape2d!! hope this can help! if not let me know!
How do you make udlr movement in godot like in pixel rpg games
Thanks
BEST BRO
hey I have a problem my charakter is not moving up or down. When I press the up or down arrow I move for 1 or 2 pixels.
make sure you don’t have if input.is_action_just_pressed. because if you have the “just” pressed it only calls it once instead of calling it the entire time the button is pressed down
Tutorial about Minimap with objective locations and such 👉👈
EVERYTIME i start a game (super noob, still a hobby), i try to reinvent the wheel with controls. THIS is how to do it.. the other ways can feel different/fun, BUT the bugs it presents are endless down the line. Stick with this, make it feel how you want by modifying it, but if you are day one new..... just do this, it IS correct. Thats why for the 5th time ive tried to vary movement, i have found my way back to tutorials to debug. if i can offer any advice to other new people, just do this!
I'm pretty sure I'm using the same code but i keep getting "Invalid call. nonexistent function 'is_action_pressed' in base 'Vector 2'" any suggestions?
what input is within the is_action_pressed()... did you make sure to create that as a input in the input map within the project settings?
I get the error "invalid call. nonexistent function 'is_action_pressed' in base 'Vector2'."
Have the same issue - do you found a solution?
Nope
Great video , i have a question , velocity = velocity.limit_length(max_speed) here velocity is a Vector2d but max_speed is an integer . Why does this works and what is Godot interpreting ?
It's not limiting X and Y separately, but limiting the length of the vector.
It's the equivalent of normalizing the vector (dividing by its length), then multiplying by the limit value, effectively setting the length to the limit value.
It only does this when the length is greater than the limit value, otherwise the vector doesn't change.
Can somebody help me
I need to put an animation of walking diagonally to my character, but I don't know how to put the code
I want the animation to occur when pressing 2 keys
I went over this in the first episode of the survival series!! and I think it is exactly what you are looking for!! I hope it helps! ua-cam.com/video/eAEe_9jCV4s/v-deo.html
I tried using this code but for 3d and it works for the most part, but it seem as soon as you stop pressing an input the physics stop working, life you hold left and then let go it stops dead, but once you press a different input like down for example then it steers into it like you were holding left the entire time, it's like the game is paused when you stop pressing inputs, anyone know why that is, any help is appreciated thank you
Ooo im sorry i have no idea, have never played with 3d but if you check godots discord im sure someone would love to help
For me I ran into the same issue, but I was able to fix it by setting "friction" to a very small number. Granted, I had to set my max speed to a super small number as well (0.05), but setting my friction to around this number solved the issue for me
Happened to me too and I'm in 2d
Is it easy to animate the player with an animated sprite 2d and this type of movement?
yes
So how do I add animations in this code?
I cant get it to work :( can anybody help the charcter dosent show up whenever i press the play button. EDIT: i made the player move but it dosent wanna move no matter what i do
I doubt u'll see this, but just in case, ive been looking for a way to make the player sprite smooth, not the movement, but where's the sprite looking at, i cant find anything so this is like a last resort, thank u in advance (sorry for bad english)
hey devworm can you make a video on a platformer movement please man ❤
Hi, i tried to do it and my Godot is saying : Invalid call. Nonexistent function 'is_action_pressed' in base 'Vector2'... and i don't understand what to do now...
You need to make sure you have the text exactly as shown on screen. If anything is capitalized or lower cased in the wrong spot, the function won't work
@@TFAmbience
can you explain me how pressing a button changes the input and why does this
"input = Input.get_vector("move_left", "move_right", "move_up", "move_down")" fuocking work
The "move_left" etc. are part of Godot's built in input mapping. If you open Project Settings, then Input Map, then check "Show Built-in Actions" and you can see a bunch more too. These are such standard events, that Godot bakes them in. You can define your own stuff too, or remove the built ins.
did i miss something? when did you define velocity? I don't see it here. how come this isn't throwing an error?
you no longer have to define velocity on a characterbody2d in godot 4 like you did have to do in godot 3!!
Can some one help me pls... i did everything as in the video but i can movie neither with WASD , mouse, with the arrows
did you use "ui_up", "ui_down", etc? If so that should be the arrows and if you want WASD then you have to go into the Project settings > input map - and create new input actions. for example "right" and assign that to the "d" key. Then in the code use "if is_action_pressed("right"):"
I'm actually glad this script is broken down this way. Working on something like a top down flight game. It gave me insight on how I want to go about it now. I have to take into account airspeed and drift. Many thanks. My movement script was going to be an utter mess. Lol.
so happy it was able to help!! goodluck on your game!! keep me updated on it.. Im curious now!
Mine just crashes when I try to run the scene
Smooth
is there a way to not normalize the movement?
Yess Godot 4.0 Les gooo
Godot 4 is so good
you are going to make more tutorials on godot 4.0 and above?
yes working on a ton right now. going to upload one every other day starting the 12th
i am watching at 1080p and using upscale and i still cant see text clearly. is that I or i small? not clear.
really?? I'm looking back and it is visible clearly to me... is it still like that for you?
Yep. it is not real 1080p video. like the res is 1080 but i am sure the bitrate is not that of an actual 1080p video. I have seen 720p videos clearler than this. but it is fine. Thanks so much for your tutoiral. :)@@dev-worm
what programing languige was used here?
gdscript
how can i animat this ??
Godot 3.5 if i create button i can also use and access by mobile game or app touch input but godot 4 i create button and export to android i can't use or access that button why any fix available please tell me
I’m not sure I’ll look into it because many thing have been changed
@@dev-worm yes
@@dev-worm you wil find please make a video
How can I add the animationsprite in this case??
the animationsprite should be added the same way it was in godot 3, so instead of adding the normal sprite2d add an animatedsprite2d and just create the animations then call for the animations in the code based on the input variable
you can make tutorials for a donw top shooter game
is saying that my velocity is undefined and wont let me add a velocity function, variable or constant
are you in godot 4?? because that seems like an error you’d get in godot 3 before the velocity was a thing. Hoping that helps you a bit
@dev-worm no I'm in 4 100%
i'm getting an error message (Invalid operands 'callable' and 'Vector2' in operator '==')
same here, i'm not sure what to do about it, did you manage to fix it in the end?
@@pytho2909 pls share code for check
Can u pls make a vid about how to make a smooth jump in Godot 4
how do i move?
Look, I have this little amount of projects (maybe 3948), and I am always very anxious about the final product, and I forget that despite knowing programming, I have no idea what I am doing in a gaming creation engine. So I just threw away my pride (in a tin of pink trash) and I'm learning from zero all the basic mechanics of how to create a character. Thank you so much for helping me to create my perfect game, if I even complete a demo of the game I bring the link to those interested in knowing the project.
hey, glad i could help. I am really interested in knowing about the project btw
invalid operator: nil & int in operator "*". line 5 nani
I followed the tutorial and all but I can't seem to get the character moving like nothing crashes or any error's just the player movement itself doesnt work. Anyone can help?
did you make sure to add the move_and_slide func? cause if not player wont move at all
@@dev-worm I have the same issue and it still doesn't work even with the move_and_slide func
I need to spam it before i move and i move really slowly
its because you might be useing if Input.is_action_just_pressed(xxxxx) instead of the one im using inside of this tutorial
It irritates the absolute frick out of me when I perfectly attempt to recreate what a turorial show. No wrong code at all. Line for line, word for word, letter for letter. Absolutely perfect. And it just doesn't work for whatever reason godot decided to just not work.
Hey can you do a video how to do the wasd button pls bc i think thats better combos and i dont under stand how to do it thanks for an answear or a video
Go to Project Setting > Input Map Tab > Type in "right" in the "Add New Action" Section. Then next to the "right" action click the + and then whatever key you want to be associated to "right" so "d"... then in code instead of saying "ui_right" just put the name of your action so for this instance put "right"...... hope that helps!!
@@dev-worm thanks it works thank you
im having trouble getting this and animations to work at the same time. any tips?
check out a series on the channel like the "How to Create a Survival Series in Godot 4" or the "How to Make an RPG in Godot 4" Series.. in both of those I go over this pretty good!
@@dev-worm yeah i was trying to follow along on the rpg series. but you cant add the dir = "right" and so on in the same line as the input.x= . Basically i cant figure out how to map out the direction mapping to correspond to activating animations.
@@dylanwillyams Interesting. I'm currently wondering how I can get my character in the RPG tutorial to move a little more freely in all directions as right now its stuck on the "up,left,down,right" directions only and it feels pretty stiff. I'm currently following the smooth movement tutorial, but its hard to wrap my head around how I will update the "RPG" tutorial movement lol!
😎👍
ok godot just hates me i do everything correct doesnt work i copy and paste code of the internet doesnt work i look at more tutorials doesnt work like it isnt that hard to make top down movement but just why wont it work for me
copy of code?
I get an error that says: "Identifier "delta" not declared in the current scope". Is there any solution for this?
I fixed it. Sorry :)
My mistake was "func player_movement():". I should've typed "func player_movement(delta):"
Thanks for the comment, I made the same mistake and wouldn’t have noticed it myself
hold alt to resize both sides a once
thanks, good tip!
What kind of programming language is this?
this is gdscript! it is a language created to work specifically for the Godot engine
Hey, I spotted a little bug here! When I try pressing both left and right keys simultaneously, it seems like I can't move upwards. Any idea why this is happening? Great video, by the way! Keep up the good work!
This is usually caused by a hardware limitation, where some keyboards can only send up to 2 or 3 keypresses at a time and if any other key (in my experience this used to be a common 'feature' that only occurred with the arrow keys especially on older keyboards) but can also be driver related, though it is unlikely.