How to make an Area2D node multi directional without using the mouse

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

My game uses only keyboard keys NO MOUSE, and I’m trying to get a projectile (Area 2d) to move in different directions based on the direction the player is facing (i.e. shot up, right, left, or down). Currently it only shoots to the right but does spawn from the correct directions based on the players facing direction, but the projectile always goes right.

Here is the players code for the projectile:
`if Input.is_action_just_pressed(“acid throw”):
state = ATTACK
var acid = ACID.instance()
if sign($anchor/Position2D.position.x) == 1:
acid.position = $anchor/Position2D.global_position
acid.position = $anchor/Position2D.global_position

func attack_state(_delta):
velocity = Vector2.ZERO“Attack”)

func attack_animation_finished():
state = MOVE`

Here is the projectiles code:
extends Area2D
var speed= 200
var velocity = Vector2()
var x_dir = 0
var y_dir = 0
var direction = 1
func _physics_process(delta):
velocity.x = x_dir
velocity.y = y_dir
position += transform.x * delta * speed
velocity.x = speed * delta * direction

func set_acid_direction(dir):
direction = dir

func _on_VisibilityNotifier2D_screen_exited():

:bust_in_silhouette: Reply From: sry295

your bullet moves only because this line

func _physics_process(delta):
    position += transform.x * delta * speed

you can try remove other line, it still runs the same
The problem is it doesn’t have any direction attached to it
you only increase its position every frame. that why it only moves right
the direction you have collected is in the variable ‘velocity’ but you didn’t do anything with it after that
make your position move by your velocity instead.

func _physics_process(delta):
    velocity.x = speed * delta * direction
    position += velocity

thanks for your help but it still only goes right I’m afraid

djskidds | 2022-04-10 14:07

try print dir value in set_acid_direction(dir) function to see if it got dir correctly
if you turn left it should be -1

sry295 | 2022-04-11 01:57

its getting dir because its printing “left” everytime i fire but still goes the same way for some reason

djskidds | 2022-04-12 00:42