I am a noob and I need help with directions in movement

Godot Version

4.2

Hello, I am new in godot and I am trying to learn to code in GDScript… I try to make a 2D character in a topdown view with movements but there is something that I do not understand… in Godot when Y < 0 should me up, right?

but why is working backwards?

This is what I am doing

and This is my code and everything works well:

extends Area2D

@onready var animation: AnimatedSprite2D = $AnimatedSprite2D
const PIXELS : int = 32
var tween : Tween
var moving : bool = false
var current_idle = “idle_down”

func _ready() → void:
pass

func _process(delta: float) → void:
var direction = Input.get_vector(“move_left”, “move_right”, “move_down”, “move_up”)

if direction && !moving:
	moving = true
	move_me(direction)
if !direction && !moving:
	animation.play(current_idle)

func move_me(direction):

var next_position : Vector2

#moveup
if direction.y > 0:
	next_position = position + Vector2(0, -PIXELS)
	animation.play("walk_up")
	current_idle = "idle_up"
	move_by_tween(next_position)

#movedown
elif direction.y < 0:
	next_position = position + Vector2(0, PIXELS)
	animation.play("walk_down")
	current_idle = "idle_down"
	move_by_tween(next_position)
	
#moveleft
elif direction.x < 0:
	next_position = position + Vector2(-PIXELS, 0)
	animation.play("walk_left")
	current_idle = "idle_left"
	move_by_tween(next_position)
	
#moveright
elif direction.x > 0:
	next_position = position + Vector2(PIXELS, 0)
	animation.play("walk_right")
	current_idle = "idle_right"
	move_by_tween(next_position)		

func move_by_tween(next_position : Vector2):
tween = create_tween()
tween.tween_property(self, “position”, next_position, 0.2)
tween.tween_callback(end_of_tween)

func end_of_tween():
moving = false
animation.play(current_idle)


So here I notice that when y > 0 and if I declare Vector2(0, PIXELS) this movement should be DOWN but Works backwards and vice versa. am I omiting something or understanding something wrong?

Excuse my english is really bad sorry.

You just contradicted your own code.

#moveup
if direction.y > 0:
	next_position = position + Vector2(0, -PIXELS)

You are checking if direction.y is a positive number. > 0 is more than zero…

Yes, that’s why I don’t understand why is working?

if direction.y > 0 #then should be
next_position = position + Vector2(0, PIXELS)

right? but when I do that the controls are inverted. Maybe I am a fool and not understanding something. lol

I think you are making it complicated on yourself by thinking in absolute terms of up/down/left/right when it would perhaps be easier to use relative terms north/south/east/west and just use whatever vector works for each??
Either way, Vector2 does define four constants for you to use.

Vector2.UP
Vector2.DOWN
Vector2.LEFT
Vector2.RIGHT

that should result in: Y > 0 then I should use Vector2(0, PIXELS)
adding movement to Y and this should move me DOWN, right?

but I did this and works well:

I am sorry if my english is bad and maybe I didn’t explain my self in a right way… :face_with_spiral_eyes:

I didn’t know about this, thanks. but I still don’t understand… maybe you are right I am making it complicated :face_with_spiral_eyes:

Ah, yes here is was causing that:

var direction = Input.get_vector(“move_left”, “move_right”, “move_down”, “move_up”)

you mixed argument position of moved_down and move_up

should be:

var direction = Input.get_vector(“move_left”, “move_right”, “move_up”, “move_down”)

1 Like

LOL! What? so the order should be always in the same way? interesting I’ll check the documentation. Thank youuuuuuuuuuuu so much! :sparkles:

1 Like