Godot tutorial: the character only moves UP or DOWN

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lamzik88

Hey Godot community!

I am going through the tutorial and I am under “Your first 2D game » Coding the player” section.

The code is here (seems to be the same as in the tutorial but might be missing something):

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_up"):
		velocity.y -= 1
	if Input.is_action_pressed("move_down"):
		velocity.y += 1
	
	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite.play()
	else:
		$AnimatedSprite.stop()
		
	position += velocity * delta
	position.x = clamp(position.x, 0, screen_size.x)
	position.y = clamp(position.y, 0, screen_size.y)

and the result is that, firstly, the character moves only up and down and, when starting it, the player is located in a weird space (like on the top left corner of the window).

Not sure what can be the issue here.

Thanks in advance!

Are your left and right keypresses being registered?

SteveSmith | 2023-01-15 09:47

You mean via Project Settings? Yes, they are there

lamzik88 | 2023-01-15 11:29

Have you added a breakpoint to check their bit of code is being run? And check what size screensize is?

SteveSmith | 2023-01-15 11:34

Edited to fix code formatting. Please use the {} button to format code in future posts.

jgodfrey | 2023-01-15 16:13

As per the tutorial the screen size is 480x720.

I tried using breakpoints but with no luck…

I modified the code in this way (changing + and -)

if Input.is_action_pressed("move_right"):
		velocity.x -= 1
	if Input.is_action_pressed("move_left"):
		velocity.x += 1

and now when I click on my “left” button, the character is moving right. When I am pressing my “right” button then nothing happens

lamzik88 | 2023-01-15 17:52

If you add a print statement inside each of those input blocks, do they all register when you press the associated key? For example:

if Input.is_action_pressed("move_right"):
    print("move right")
    velocity.x += 1

jgodfrey | 2023-01-15 17:58

In addition to my previous comment (immediately above), the most recent code you posted has an indention problem. So, this:

if Input.is_action_pressed("move_right"):
        velocity.x -= 1
    if Input.is_action_pressed("move_left"):
        velocity.x += 1

Should be:

if Input.is_action_pressed("move_right"):
    velocity.x -= 1
if Input.is_action_pressed("move_left"):
    velocity.x += 1

Though, the add and subtract appear to be backwards…

jgodfrey | 2023-01-15 18:00

Thanks a lot for your efforts here!

I do not know why it is showing the snippet here with the wrong indention as it should be the correct one.

So, I have added the “print” statements and put back the correct x/y actions (when pressing the right button then velocity.x += 1 etc.) and discovered that now, for some reason, it is not registering the right button click. All other buttons seem to be working though.

lamzik88 | 2023-01-16 16:41

Are you sure there’s not a typo in the name of the move_right action? There must be a trivial mistake somewhere.

jgodfrey | 2023-01-16 16:44

I am sure that there were no typo or something but replacing the name of action move_right to moveRight has helped.

Thanks a lot for your support!

lamzik88 | 2023-01-17 07:20