0 votes

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?

in Engine by (15 points)

Hi,
First of all, can you correctly format your code? like this:

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 _physics_process(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()
        fireball.set_fireball_direction($Position2D.position.x )

    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)

Second, you dont need to if something == true:, you can simply ask if something:

And third, can you share me the project so i can do some test? I cannot see a bug in the code you shared.. maybe testing would be easier. And also, where you are updating $Position2D.position? because that is a local position relative to its parent, it won't change when parents position change i think. And couldn't you use the state of flip_h to set direction instead of position?

thank you for the pointers. will look in to them as soon as possible. of course i can share it to you but perhaps a stupid question, how do i do it?

You can share it with google drive, mega, or any cloud storage and post the link here. Also, you could create a repo in github or similar ans share the link to it.

Thank you again for helping my here is the link to my project on google drive:

https://drive.google.com/open?id=1dvreCJEWla5yj7hQzmfbauSJKXjXQgK3

Have you by any chans had some time over to look on my projekt to see what could be wrong with it?

My hard drive broke, and i o my cell now.. ill replace it soon and have a look at it.

Sorry to hear about the harddrive of course and again Thanks alot for the help!

Hi, i borrowed my girlfriend's notebook and i think i solved this. I'll post it as an answer.. Tell me if it works!

1 Answer

+1 vote
Best answer

Hi:
Looking at your code, i made this changes:

if Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    $Sprite.play("run")
    $Sprite.flip_h = false
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    $Sprite.play("run")
    $Sprite.flip_h = true
else:
    velocity.x = 0
    if on_ground == true:
        $Sprite.play("idle")

if $Sprite.flip_h:
    $Position2D.position.x = -1
else:
    $Position2D.position.x = 1

The problem was that you only set position.x to -1 if the sign of that was negative. So, once you changed to +1 when you entered the other case, you would never again get negative position. I thought that using flip_h of sprite would be easier, so i made those changes.

by (3,505 points)
selected by

Wow thanks a bunch! that did it! a thousands time thanks!

You are welcome! You may select the answer if it worked so others see it's solved!

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.