The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

It's supposed to detect which direction the mouse is headed to from the players perspective and get a gun out from that direction. I've been trying so hard to fix it. I'd also really appreciate any tips on how could I improve this code.

if Input.is_action_pressed("MouseClick"):

    var mousePos = get_local_mouse_position()

    var direction : String

    if $Weapon.offset.y == 0:

        if abs(mousePos.x) - abs(mousePos.y) >= 0:

            if mousePos.x >= 0:

                direction = "right"
                $Weapon.rotation_degrees = 90

            else:

                direction = "left"
                $Weapon.rotation_degrees = -90


        else:

            if mousePos.y <= 0:

                direction = "top"
                $Weapon.rotation_degrees = 0

            else:

                direction = "bottom"
                $Weapon.rotation_degrees = 180



    while Input.is_action_pressed("MouseClick") and direction == get_direction_changed():
        print(direction)
        if $Weapon.offset.y >= -188:

            $Weapon.offset.y -= 4

            $WeaponGetOut.start(0.1)
            yield($WeaponGetOut, "timeout")

        else:

            $Weapon.play("MinigunSpin")

            if $Weapon.speed_scale < 60:

                $Weapon.speed_scale += 0.05
                $WeaponGetOut.start(0.1)
                yield($WeaponGetOut, "timeout")

    while (!Input.is_action_pressed("MouseClick") or direction != get_direction_changed()) and $Weapon.offset.y != 0:

        get_back_in()
        $WeaponGetOut.start(0.1)
        yield($WeaponGetOut, "timeout")


else:

    get_back_in()
    $WeaponGetOut.start(0.1)
    yield($WeaponGetOut, "timeout")

func getbackin():

if $Weapon.speed_scale > 3:

    $Weapon.speed_scale -= 0.1

else:

    $Weapon.playing = false

    if $Weapon.offset.y != 0:

        $Weapon.offset.y += 4

func getdirectionchanged():

var mousePos = get_local_mouse_position()

if abs(mousePos.x) - abs(mousePos.y) >= 0:

    if mousePos.x >= 0:

        return "right"

    else:

        return "left"



else:

        if mousePos.y <= 0:

            return "top"

        else:

            return "bottom"
in Engine by (20 points)

Sorry, could you say what the result you're getting right now is? That would make it a lot easier to answer.

1 Answer

+1 vote

i checked it fast and it could be implemented easier than this , you can replace the order of mouse_dir values if it's inverted

var player := KinematicBody2D.new()
var gun := Sprite.new()
func _process(_delta : float) -> void:
    var mouse_dir = get_global_mouse_position() - player.global_position
    gun.look_at(mouse_dir)
by (274 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 [email protected] with your username.