How do I make my fireball shoot in whatever direction my player sprite is facing?

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

I’m still very new to Godot so apologies if this is a very newbie question.

this is my code to shoot a fireball from my dragon’s mouth:

enter image description here

I want it to shoot in whatever direction my player sprite is facing. At the moment I only want my player to be facing right or left but it cant still face up or down(this is on purpose).

like so: Screen capture - 04e00f65340d394fcb8ed492233ae1dc - Gyazo

but the fireballs only seem to come out of the right side no matter which way the character is facing.

shown here: Screen capture - 27f9d6636911ce649e07b1b141ab79df - Gyazo

Any ideas? Thanks in advance!

:bust_in_silhouette: Reply From: whiteshampoo

Just a wild guess:

do you really rotate your dragon?
Looks like you just flip it.
Then rotation(_degrees) is always 0.

You’re probably right. How would I implement that into my code? I’ve looked through the qa’s on here and I don’t see how I would fit them into my implementation. Here’s my code for my walking state:

func walk_state(delta):
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    input_vector = input_vector.normalized()

    var motion = velocity * delta

    if input_vector != Vector2.ZERO:
	    animationTree.set("parameters/Idle/blend_position", input_vector)
	    animationTree.set("parameters/Walk/blend_position", input_vector)
	    animationTree.set("parameters/Defend/blend_position", input_vector)"Walk")
	    velocity = input_vector * MAX_SPEED
    if Input.is_action_just_pressed("defend"):
	    state = DEFEND

    move_and_collide(velocity * delta)

rubix3d | 2020-06-09 19:13

Try to replace the line that sets the impuls to:

fireball_instance.apply_central_impuls(velocity * fireball_speed)

then replace input_vector by velocity

velocity is then already the direction where the dragon is moving AND it’s normalized.
you also might want to delte the second last line.

whiteshampoo | 2020-06-09 19:27

replacing the line with

fireball_instance.apply_central_impuls(velocity * fireball_speed)

results in… this?

Screen capture - 3486dc73060e23dd46209f59c3af2c4e - Gyazo

they no longer fly in a straight line or… at all

rubix3d | 2020-06-09 19:39

Lol, at least it is funny!

yeah, i forgot that velocity is (0, 0) if the dragon does not move.
Maybe Rigidbody2D is not the correct Node for this.
I guess you want the fireball to hit and destroy something, so an Area2D might be preferable. there you can detect entering objects (when it hits something).
There you just have to apply a variable with the direction/speed (Vector2) and add it to the position (* delta)

Hope this makes sense…

whiteshampoo | 2020-06-09 20:05

I could make you a demo-project, if you want/need it.

whiteshampoo | 2020-06-10 05:57