Can't Move in Dodge the Creeps

Godot Version

4.7.2

Question

I’m a newbie doing the Dodge the Creeps tutorial, and I can’t figure out how to make the character move?
Here’s my code:

extends Area2D

@export var speed = 400
var screen_size

func _ready():
	screen_size = get_viewport_rect().size

func _process(delta):
	var velocity = Vector2.ZERO
	if Input.is_action_pressed("move_right"):
		velocity.x += 1
	if Input.is_action_pressed("move_left"):
		velocity.x -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	if Input.is_action_pressed("move_up"):
		velocity.y -= 1

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
	position += velocity * delta
	position = position.clamp(Vector2.ZERO, screen_size)

Thanks in advance :smiley:

Try adding print(velocity) as the last thing in the function. What kind of output do you get?

You could also put a print() inside the move code:

    if Input.is_action_pressed("move_right"):
        velocity.x += 1
        print("R")
    [...]

Are "move_right” et al actually hooked up in the input system, and hooked up the way you expect?

The output just says

Godot Engine v4.7.2.stable.official.ed1daf0bf - https://godotengine.org
D3D12 12_0 - Forward+ - Using Device #0: AMD - AMD Radeon(TM) Vega 8 Graphics

Could you post a screenshot of your scene-tree? Do you have this script attached to a node?

Have you mapped the inputs to the keys in project settings yet?

Yes I have

Sorry but what’s a scene tree?

At the end of your if statement block, you can put print(velocity) and paste the output as a reply. A screenshot of the scene tree would also be great for debugging (the branches of nodes on the left-hand side)

Also try removing the line where you clamp position. That may have unintended side effects, but I haven’t used Godot in a hot second so I may be wrong :P

Whoops, nevermind.