0 votes

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: https://gyazo.com/04e00f65340d394fcb8ed492233ae1dc

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

shown here: https://gyazo.com/27f9d6636911ce649e07b1b141ab79df

Any ideas? Thanks in advance!

asked Jun 9 in Engine by rubix3d (23 points)

1 Answer

0 votes

Just a wild guess:

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

answered Jun 9 by whiteshampoo (976 points)

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)
        animationState.travel("Walk")
        velocity = input_vector * MAX_SPEED
    else:
        idle_state(delta)

    if Input.is_action_just_pressed("defend"):
        state = DEFEND

    move_and_collide(velocity * delta)
    move_and_collide(motion)

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.

replacing the line with

fireball_instance.apply_central_impuls(velocity * fireball_speed)

results in.... this?

https://gyazo.com/3486dc73060e23dd46209f59c3af2c4e

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

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...

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

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.