Why does my projectile only shoot when facing left?

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

For some reason my projectile only is only shot when my player is facing left, why is that?

Player.gd:

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)
const FIREBALL = preload("res://Fireball.tscn")

var velocity = Vector2() # takes 2 values (x,y)

var on_ground = false

func _physics_process(delta): # happens  every fram
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
		$AnimatedSprite.play("run")
		$AnimatedSprite.flip_h = false
		if sign($Position2D.position.x) == -1:
			$Position2D.position.x *= -1
	elif Input.is_action_pressed("ui_left"):
		velocity.x = -SPEED
		$AnimatedSprite.play("run")
		$AnimatedSprite.flip_h = true
		if sign($Position2D.position.x) == 1:
			$Position2D.position.x *= -1
	else:
		velocity.x = 0
		if on_ground == true:
			$AnimatedSprite.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 sign($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 is_on_floor():
		on_ground = true
	else: 
		on_ground = false
		if velocity.y < 0:
			$AnimatedSprite.play("jump")
		else:
			$AnimatedSprite.play("fall")
		
	velocity = move_and_slide(velocity, FLOOR)

Fireball.gd:

extends Area2D

const SPEED = 150
var velocity = Vector2()
var direction = 1


func _ready():
	pass # Replace with function body.
	
func set_fireball_direction(dir):
	direction = dir
	if dir == -1:
		$AnimatedSprite.flip_h = true


func _physics_process(delta):
	velocity.x = SPEED * delta * direction # translate doesn't multipy delta automaticaly
	translate(velocity)
	$AnimatedSprite.play("shoot")


func _on_VisibilityNotifier2D_screen_exited():
	queue_free() # object no longer exists

What does the sign() function do?

Jowan-Spooner | 2019-06-09 12:32

it is a signal from Positon2D

yemi-jump | 2019-06-09 15:20

sign is the signum function. From the dicumentation:

float sign( float s ): Returns the sign of s: -1 or 1. Returns 0 if s is 0.
sign(-6) # returns -1
sign(0) # returns 0
sign(6) # returns 1

Kaligule | 2019-06-10 14:55

Thanks for making it clear!

Jowan-Spooner | 2019-06-11 16:40

:bust_in_silhouette: Reply From: Kaligule

This is probably about this code:

    if Input.is_action_just_pressed("ui_focus_next"):
        var fireball = FIREBALL.instance()
        if sign($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

I suggest you to add some debug-prints here, just to get to see where your problem might be.

    if Input.is_action_just_pressed("ui_focus_next"):
        print("Creating a fireball") # for debuging, remove later
        var fireball = FIREBALL.instance()
        if sign($Position2D.position.x) == 1:
            print("Fire in direction 1") # for debuging, remove later
            fireball.set_fireball_direction(1)
        else: 
            print("Fire in direction -1") # for debuging, remove later
            fireball.set_fireball_direction(-1)
        get_parent().add_child(fireball)
        fireball.position = $Position2D.global_position

Now try firing in both directions and look at the console. Did your code come to the point where it should fire?

If this didn’t help you find the bug, I suggest replacing the if:else: part with a single statement:

fireball.set_fireball_direction(sign($Position2D.position.x))

Perhaps that helps?

changing code to fireball.set_fireball_direction(sign($Position2D.position.x)) doesn’t work. When trying to debug the code, the console prints that I am firing in the correct direction so I really don’t understand why the fireball will only be shot when my player is facing left.

yemi-jump | 2019-06-09 15:04