0 votes

So I'm trying to make the character change the sprite according to where the mouse is, so the character can "rotate" and be more realistic.

But somehow it's not working then the rot variable is positive. When it's negative it works properly.

Is it a bug in the engine or something?
And how am I supposed to fix it?

Thanks!

Here is the code:

func bow(rot):      
    $Bow.look_at(get_global_mouse_position())
    if Input.is_action_pressed("mouse_left"):
        $Bow.play("Charge")
    else:
        $Bow.play("Idle")

    #Facing Directions
    #Right
    if (rot >= -45) or (rot <= -315) or (rot == 0) or (rot >= 315) or (rot <= 45 and rot >= 0):
        facing_direction = Vector2(1, 0)
    #Left
    elif (rot >= 135 and rot <= 225) or (rot >= -225 and rot <= -135):
        facing_direction = Vector2(-1, 0)
        print("it's working")
    #Down
    elif (rot < 315 and rot > 225) or (rot > -315 and rot < -225):
        facing_direction = Vector2(0, 1)
    #Up
    elif (rot < 135 and rot > 45) or (rot > -135 and rot < -45):
        facing_direction = Vector2(0, -1)

    animation()

    print(rot)
Godot version 3.2.3.stable.official
in Engine by (77 points)

1 Answer

+1 vote
Best answer

Your problem looks like a logic bug to me. Here...

 #Facing Directions
    #Right
    if (rot >= -45) 

Any positive value of rot will always be greater than -45, so it'll always process that block of code when the value is positive.

by (12,475 points)
selected by

Additionally, you can probably simplify your logic by adding 360 to any rot value that's less than 0.

OMG!!! IT WORKED!!!

Thanks a LOT!!!

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.
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 webmaster@godotengine.org with your username.