0 votes

I have a problem...

I have a 2D game with guns and stuff (pokemon 2D style not top down) and i have the problem that my body dont turns in the direction my gun is shooting at.

For the Player I have a normal Kinematic body and For the bullets I have an area that turns with mouse_position ( look_at(get_global_mouse_position)) )
And my Player only have animations for north, east, south and west...

Is it possible to shoot in the direction my character is looking at ( east, west, south or nord) ?

Godot version 3.4
in Engine by (16 points)

1 Answer

0 votes

Is this what you want to try to achieve? https://imgur.com/a/j0pUFUZ

If it's the case, you can measure the angle between player and mouse to determine which way they are facing. Then set the animation accordingly

func _process(delta):
    var angle_to_mouse = global_position.angle_to_point(get_global_mouse_position())
    angle_to_mouse = rad2deg(angle_to_mouse)
    angle_to_mouse = wrapf(angle_to_mouse, 0, 360)

    if angle_to_mouse < 45 or angle_to_mouse >= 315:
        animation = "left"
    elif angle_to_mouse >= 45 and angle_to_mouse < 135:
        animation = "up"
    elif angle_to_mouse >= 135 and angle_to_mouse < 225:
        animation = "right"
        animation = "down"

Then for the bullet, you can check which direction the character is facing, and set the direction vector to the bullet on instancing it.

func shoot():
    var bullet = bullet_scene.instance()
    bullet.global_position = global_position
    match animation:
            bullet.dir = Vector2.LEFT
            bullet.dir = Vector2.RIGHT
            bullet.dir = Vector2.DOWN
            bullet.dir = Vector2.UP

And here's the bullet script

extends Sprite

var dir: Vector2

func _physics_process(delta):
    position += dir * 10
by (90 points)
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 webma[email protected] with your username.