![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Muneeb |
I added two Position2D(s) in my project and I want that when the character is facing right**($Sprite.flip_h == false)** the bullet moves toward the right and if the character is facing left ($Sprite.flip_h == true) the bullet direction is left. The problem is that it is not shooting while playing idle. This is the script that I am using.
func _physics_process(delta):
if Input.is_action_just_pressed("ui_focus_next"):
var fireball = FFIREBALL.instance()
if Input.is_action_pressed("ui_right"):
get_parent().add_child(fireball)
fireball.set_fireball_direction(1)
fireball.global_position = $Position2DRight.global_position
elif Input.is_action_pressed("ui_left"):
get_parent().add_child(fireball)
fireball.global_position = $Position2DLeft.global_position
fireball.set_fireball_direction(-1)
elif $Sprite.flip_h == false:
fireball.set_fireball_direction(1)
elif $Sprite.flip_h == true:
fireball.set_fireball_direction(-1)
and this is the code for Bullet
extends Area2D
const SPEED = 500
var velocity = Vector2()
var direction = 1
func set_fireball_direction(dir):
direction = dir
if dir == -1:
$BulletSprite.flip_h = true
func _physics_process(delta):
velocity.x = SPEED * delta * direction
translate(velocity)
$BulletSprite.play("shoot")
func _on_VisibilityNotifier2D_screen_exited():
queue_free()