0 votes

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
asked Jun 9, 2019 in Engine by yemi-jump (12 points)

What does the sign() function do?

it is a signal from Positon2D

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

Thanks for making it clear!

1 Answer

0 votes

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?

answered Jun 9, 2019 by Kaligule (47 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.