Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.set_acid_direction(1)
else:
acid.set_acid_direction(-1)
acid.position = $anchor/Position2D.global_position
acid.position = $anchor/Position2D.global_position
get_parent().add_child(acid)
func attack_state(_delta):
velocity = Vector2.ZERO
animationState.travel(“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():
queue_free()