0 votes

I have followed the code provided in the tutorial but the player stays still and does not move.

My player script looks as follows:

extends Area2D


# Declare member variables here.
export var speed = 400
var screen_size


# Called when the node enters the scene tree for the first time.
func _ready():
     screen_size = get_viewport_rect().size


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):

    var velocity = Vector2()

    if Input.action_press("ui_right"):
        velocity.x += 1
    if Input.action_press("ui_left"):
        velocity.x -= 1
    if Input.action_press("ui_down"):
        velocity.y += 1
    if Input.action_press("ui_up"):
        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)

Could someone please provide me with feedback on how to solve this issue?

in Engine by (24 points)

1 Answer

+1 vote
Best answer

Your input is wrong. Here is what the tutorial has:

if Input.is_action_pressed("ui_right"):
    velocity.x += 1
...

And here is what you wrote:

if Input.action_press("ui_right"):
    velocity.x += 1
...

It should be is_action_pressed, not action_press.

(action_press simulates the press of a key and returns nothing, it does not check if that key is pressed)

by (27,966 points)
selected by

Thank you for your help. It works correctly now.
Seems silly now that I did not notice it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.