• Godot Help2D
  • Area2D not moving (Sources of the First 2D game tutorial)

Greetings!

I'm a very beginner and I'm trying to follow this tutorial: https://docs.godotengine.org/en/stable/getting_started/first_2d_game/03.coding_the_player.html

However my character doesn't move all at, may you help me?

extends Area2D
signal hit

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.

# 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(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()	
				
	if velocity.x != 0:
		$AnimatedSprite2D.animation = "walk"
		$AnimatedSprite2D.flip_v = false
	# See the note below about boolean assignment.
		$AnimatedSprite2D.flip_h = velocity.x < 0
	elif velocity.y != 0:
		$AnimatedSprite2D.animation = "up"
		$AnimatedSprite2D.flip_v = velocity.y > 0
		
	position += velocity * delta
	position.x = clamp(position.x, 0, screen_size.x)
	position.y = clamp(position.y, 0, screen_size.y)
		
		
func _on_body_entered(_body): 	
	hide() # Player disappears after being hit.
	hit.emit()
	# Must be deferred as we can't change physics properties on a physics callback.
	$CollisionShape2D.set_deferred("disabled", true)
	
	
func start(pos):
	position = pos
	show()
	$CollisionShape2D.disabled = false

Also an image of my project:

Thank you.

Did you set the keys in Input Map in project settings?

Also, you will find the player moving up regardless of pressing up or down, I encourage you to debug it yourself but here you go:
When you press up you should do velocity.y -= 1 , and when you press down you do velocity.y += 1 😉

    BroTa

    Yes, the keys in input map are ok and all of them are being triggered on the code.

    The code seems fine, but the Player (Area2D) is not moving at all.

    Oh I noticed you have a Camera2D attached to the player, try removing it and run again.
    When there is no background, and no other objects around the player, the camera will center on the player, and appear that it is "not moving"
    I hope this helps

    Make sure that script is attached to the top most node in the scene, and not to something like, the collision area. NVM I see you have it right

    You could try throwing some print () statements in there to see that things are being set they way you expect.

      stranger_anger
      True, whenever in doubt, print
      print(global_position)
      print(velocity)
      print("gg")
      print("nice")
      print("aaaaah this isn't supposed to be printed")
      and some more embarrassing prints 😆

      It would be easier if you shared the project.

      Where did the camera come from? That's not in the tutorial.