Godot Version
4.7.1
Question
I followed a youtube tutorial called “GoDot Top-Down Action Game Tutorial - Part 1: Player Movement & Animations” by CodingWithRuss, I tried to copy him 1-to-1 as best I could, and my movement still isn’t working. My player won’t move even though I added the keybinds in the input map. I can’t figure out if I did something wrong or I just need to do something else with my current version, because the tutorial’s was older (4.5.1).
Here’s the code if needed.
extends CharacterBody2D
const SPEED = 300.0
var last_direction: Vector2 = Vector2.RIGHT
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(_delta: float) → void:
process_movement()
process_animation()
move_and_slide()
func process_movement() → void:
var direction := Input.get_vector("left", "right","up","down")
if direction != Vector2.ZERO:
velocity = direction * SPEED
last_direction = direction
else:
velocity = Vector2.ZERO
func process_animation() → void:
if velocity != Vector2.ZERO:
play_animation(“run”, last_direction)
play_animation(“idle”, last_direction)
func play_animation(prefix: String, dir: Vector2) → void:
if dir.x != 0:
animated_sprite_2d.flip_h = dir.x > 0
animated_sprite_2d.play(prefix + “_right”)
elif dir.y < 0:
animated_sprite_2d.play(prefix + “_up”)
elif dir.y > 0:
animated_sprite_2d.play(prefix + “_down”)
Can anyone else figure out the issue? I’m also on Linux (Lubuntu, specifically) if that matters.
Since your code has very little proper formatting and identation, I assume that’s the problem. Unless it looks completely different for you in your editor. You need to make sure you ident code properly, for example, code under an if statement or a function should be one tap further than the statement itself. A second if should have two tabs, etc…
Do you get any warnings or error messages, or nothing happens at all?
I don’t see any errors in your code.
Initially, I would add some print statements to understand what’s happening. For example, to know the value of “direction” when you press and release a key. Adding a breakpoint would be less useful, I think.
I would recommend to check this official guide to get understanding how works movement.
Hey kity.core,
I tested your code for movement:
extends CharacterBody2D
const SPEED = 300.0
var last_direction: Vector2 = Vector2.RIGHT
func _physics_process(delta: float) -> void:
var direction := Input.get_vector("ui_left", "ui_right","ui_up","ui_down")
velocity = direction * SPEED * delta
if direction != Vector2.ZERO:
last_direction = direction
else:
velocity = Vector2.ZERO
move_and_slide()
And it worked.
You can overwrite it with the code here, or use another CharacterBody2D to see for yourself.
Could it be that you tried moving with WASD instead of the arrow keys?
(If so try the arrow keys. That works here.)
PS:
If you renamed the keys in the Input map, like you said, then you need to do that also in this code, if you want to use it. So “ui_left” would be “left” and so on.