0 votes

I want to animate my Enemy with the Animation Player. When Moving left i want the animation player to play the Run Left animation. How can i get the movement direction?

in Engine by (42 points)

3 Answers

+1 vote
Best answer

Thanks to p7f i got it working. For anyone who wants to know how you do it, here is the code:

if velocity.x < 0:
        AnimationPlayer.play("RunLeft")

elif velocity.x > 0:
        AnimationPlayer.play("RunRight")    

enter image description here

Example Video

by (42 points)
+1 vote

You can get the linear_velocity property. If linear_velocity.x < 0 that means you are moving left.

by (3,394 points)

it says that linear velocity inst declared I tired something similar but with my code the enemy got a seizure

enter image description here

if velocity.x < 0:
Runner_player.play("RunLeft")

            elif velocity.x > 0:
                Runner_player.play("RunRight")  

            elif velocity.y < 0:
                Runner_player.play("RunDown")

            elif velocity.y > 0:
                Runner_player.play("RunUp") 

If you are using a RigidBody (as i understood from your question) linear_velocity should be defined. Is the script of the rigid body where you are using that script?

my mistake, it's a kinematic body 2d. I mixed it up.

Ah, ok,
So from your code, i assume Runner_player is your animation player (you probably assigned something like var Runner_player = $AnimationPlayer or similar?), and velocity is a variable you defined to hold the velocity of the body. I dont see why your code wouldnt work.. would you share your full script? remember to use the code sample button for it.

i found the offender (i don't know how to say that in english, sorry)

i forget to remove:

sprite.flip_h = velocity.x < 0

thanks for your help, know it's working just fine. ^^

glado to help. You may add your own answer and select it so others can see its solved!

0 votes
if velocity.x < -abs(velocity.y):
    looking=("Left")
elif velocity.x > abs(velocity.y):
    looking=("Right")
if player.y < -abs(velocity.x):
    looking=("Up")
elif player.y > abs(velocity.x):
    looking=("Down")

The other methods made x the priority. So I came up with this way for my game.

by (14 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 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.