Need help with "player movement" code from a guide

Godot Version

4.3

Question

howdy, godot noob here.

i’ve been following this written guide - Learn Godot 4 by Making a 2D Platformer - but i’m already stuck at the beginning, the player movement.

my player just does not move.
i’ve copied the exact code from the guide and repeated all the steps as described multiple times, but it still doesn’t work.

any idea why that might be?

Paste here the code you have in your scripts by using the preformatted text with ```
And also screenshots of your scene setup.
We can’t magically figure out what’s wrong if we can’t see what you have in your project :slight_smile:

ty for the quick reply!

so, that is the code from the guide and i have the exact same in my project.

extends CharacterBody2D

@export var speed = 100
@export var gravity = 200

func _physics_process(delta):
	
	velocity.y += gravity * speed
	horizontal_movement()
	move_and_slide()
	
func horizontal_movement():
	
	var horizontal_input = Input.get_action_strength("ui_left") - Input.get_action_strength("ui_right")
	velocity.x = horizontal_input * speed

and here’s the scene:

scenescene

You have 2 mistakes. First mistake:

velocity.y += gravity * speed

should be

velocity.y += gravity * delta

Second mistake:

var horizontal_input = Input.get_action_strength("ui_left") - Input.get_action_strength("ui_right")

should be

var horizontal_input = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")

Everything else works on my end, so should on yours too.

works indeed, thanks!

man, i feel so stupid now -.-

1 Like

Don’t worry, confidence will come with experience :slight_smile:
Mark my previous reply as a solution to your issue, so others know the issue has been resolved, if they ever encounter a similar problem.

1 Like