KEY_RIGHT and others not recognized

Godot Version

4.5.1 stable (on Apple M1 Pro)

Question

Hey all, first time posting. I’m completely new to Godot and programming, so please bear with me.

I’m following a short intro to Godot course. (I realize now it’s going to make more sense for me to start by learning the basics of programming first, but I figured I’d see this course through before going that way.) Everything had been going well and we’re now at the part where we’re creating a quick little coin collector game to put everything we’ve learned together.

First up is player movement. Despite my code being exactly the same as the instructor’s, I cannot get the arrow keys to work at all when I run the scene. The character sprite will not budge.

Here’s the code:

extends CharacterBody2D

var speed : float = 100

func _physics_process(delta):
	velocity.x = 0
	velocity.y = 0

	if Input.is_key_pressed(KEY_RIGHT):
		velocity.x += speed

	if Input.is_key_pressed(KEY_LEFT):
		velocity.x -= speed

	if Input.is_key_pressed(KEY_UP):
		velocity.y -= speed

	if Input.is_key_pressed(KEY_DOWN):
		velocity.y += speed

	move_and_slide()

And in case that doesn’t show up properly, here’s a screenshot so you can see my formatting.

I tried assigning other keys instead of the arrows and still no dice. Is there something I’m overlooking?

Hi,

Your code works fine on my end. Are you sure that the code is executed at all? Try adding a print('hi') line at the start of _physics_process to make sure it is called correctly.

No idea what was going on, but I restarted Godot and it was suddenly working :woman_facepalming: Ugh. I should have checked that first. Sorry for adding to the noise with my dumb question!

Don’t worry, that’s okay. I don’t understand either why rebooting Godot would fix it but hey, that’s good to know.