Running into a problem with the 2D demo in the documentation.

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

For some reason, the player in the 2D demo in the documentation is moving diagonally when I press the left or right keys.

Here is the script I am using.

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.x, 0, screen_size.y)

I don’t see any problem with your code. Maybe check your input map to make sure you have not assigned two keys the same action.

magicalogic | 2022-07-11 15:48

Should the below line of code:

position.y = clamp(position.x, 0, screen_size.y)

Not be as follows? (your mixing x and y)?

position.y = clamp(position.y, 0, screen_size.y)

godot_dev_ | 2022-07-12 17:21

Ah. I bet that was what was throwing things off. That would effect the position. Thanks.

Krathoon | 2022-07-12 17:31