I can not get my character to shoot in both directions. It starts out by shooting to the left and when i move to the left it shoots right. so i have both directions but when i move to the right again it continues to shoot right?
This is my script for the Player!
extends KinematicBody2D
const SPEED = 150
const GRAVITY = 10
const JUMP_POWER = -450
const FLOOR = Vector2(0, -1)
const FIREBALL = preload("res://Fireball.tscn")
var velocity = Vector2()
var on_ground = false
func physicsprocess(delta):
if Input.is_action_pressed("ui_right"):
velocity.x = SPEED
$Sprite.play("run")
$Sprite.flip_h = false
if sign($Position2D.position.x) == -1:
$Position2D.position.x = -1
elif Input.is_action_pressed("ui_left"):
velocity.x = -SPEED
$Sprite.play("run")
$Sprite.flip_h = true
if sign($Position2D.position.x) == 1:
$Position2D.position.x = 1
else:
velocity.x = 0
if on_ground == true:
$Sprite.play("idle")
if Input.is_action_pressed("ui_up"):
if on_ground == true:
velocity.y = JUMP_POWER
on_ground = false
if Input.is_action_just_pressed("ui_focus_next"):
var fireball = FIREBALL.instance()
if $Position2D.position.x == 1:
fireball.set_fireball_direction(1)
else:
fireball.set_fireball_direction(-1)
get_parent().add_child(fireball)
fireball.position = $Position2D.global_position
velocity.y += GRAVITY
if isonfloor():
on_ground = true
else:
on_ground = false
if velocity.y < 0:
$Sprite.play("jump")
else:
$Sprite.play("fall")
velocity = move_and_slide(velocity, FLOOR)
and this is my script for the fireball:
extends Area2D
const SPEED = 200
var velocity = Vector2()
var direction = 1
func _ready():
pass
func setfireballdirection(dir):
direction = dir
if direction == -1:
$AnimatedSprite.flip_h = true
func physicsprocess(delta):
velocity.x = SPEED * delta * direction
translate(velocity)
$AnimatedSprite.play("shoot")
func onVisibilityNotifier2Dscreenexited():
queue_free()
do anyone know what i do wrong?