How to Create SMOOTH Player Movement in Godot 4.0

preview_player
Показать описание
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.

redawgts
Автор

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()

marcosmachado
Автор

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 and velocity.length() == 0:
new_animation = "idle_right"
elif and velocity.length() == 0:
new_animation = "idle_left"
elif and velocity.length() == 0:
new_animation = "idle_down"
elif and velocity.length() == 0:
new_animation = "idle_up"

if new_animation != current_animation:
anim.play(new_animation)
current_animation = new_animation

coenraadgreyling
Автор

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.

dajoma
Автор

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. :)

davekudrev
Автор

Happy to have found you- getting started on my first project ever, and tutorials like this are so freakin' helpful.

schuepbachr
Автор

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

covereye
Автор

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!!

williambrodzinski
Автор

Thank you. This is the first coding tutorial I’ve followed that gave me a tangible result.

TmasterYT
Автор

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!

arukon
Автор

I appreciate you taking the time to explain what your code means.

blackjacked_xiii
Автор

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 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!

completelyrandomlyhandlee
Автор

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 !

Miiite
Автор

Was hoping for vids with godot 4, love it thx <3

cuppixd
Автор

Your tutorial really helps me in the journey of learning to use Godot!

root......
Автор

hi! starting to learn godot, your vids are very helpful! hope you can make a part 2 video with the sprite animations added to this code!

HagDeLioncourt
Автор

I was able to use this and tweak it just a little bit, but man, this is really super smooth movement. Thanks for this!

enterthearcade
Автор

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).

productionsinparadise
Автор

Hey Bro. Love the video. I entered in the code just like you said however the input.x and input.y lines my int(Input doesn’t look right. On your code, int is red and Input is green. But both int and Input are green. What did I do wrong? I loaded my world and by dude didn’t move at all.

williampoole
Автор

Yes man glad your back. Let’s get this goin
Btw my game is almost done just need to work my inventory

IronModeOfficial
welcome to shbcf.ru