0 votes

I am trying set sprite based on direction of node facing an enemy.

#face nearest marble
        #  >>>>>  self.look_at(nearest_marble.position)- I changed to rotate for smoothness of movement
        var velocity = global_position.direction_to(nearest_marble.global_position)
        rotation = velocity.angle()
        print (rotation_degrees)
        if rotation_degrees <= 22.5 and rotation_degrees  >= -22.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(right)
        if rotation_degrees <= 67.5 and rotation_degrees  >= 22.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott_right)
        if rotation_degrees <= 112.5 and rotation_degrees  >= 67.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott)
        if rotation_degrees <= 157.5 and rotation_degrees  >= 112.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(bott_left)
        if rotation_degrees <= -22.5 and rotation_degrees  >= -67.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top_right)
        if rotation_degrees <= -67.5 and rotation_degrees  >= -112.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top)
        if rotation_degrees <= -112.5 and rotation_degrees  >= -157.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(top_left)
        if rotation_degrees <= -157.5 and rotation_degrees  >= 157.5: 
            $body/CollisionShape2D/shooter_sprite_button.set_normal_texture(left)

The only one that works right is for right. Maybe its my dyslexia but I cant see my mistake.

Or if there is a better way to do this?

Godot version 3.2.3
in Engine by (16 points)

i cant see a reason why right is working and the other not, are you sure you are not simply seeing a default right texture and actually the entire code is not working at all?
add some print() below the if condition to check if it enters there.

also, maybe change all the if into if/elif/else: it does not make sense to let the computer check for each condition if you know it will already match only 1 of them

I figured out what was wrong:

one I fixed the if to elifs.

Second I was rotating the texture_button! Which was incorrectly rotating the sprites. SO I added a child sprite, moved the rotation to child sprite, then added my muzzle spawn point to that child sprite and Viola I have my sprites functioning properly.

Please log in or register to answer this question.

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.