How to get the movement direction of an ridget body /. Vector2

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rabbitnap

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?

:bust_in_silhouette: Reply From: p7f

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

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")	

Rabbitnap | 2020-07-30 13:27

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?

p7f | 2020-07-30 13:30

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

Rabbitnap | 2020-07-30 13:32

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.

p7f | 2020-07-30 13:38

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. ^^

Rabbitnap | 2020-07-30 13:49

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

p7f | 2020-07-30 14:26

:bust_in_silhouette: Reply From: Rabbitnap

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

:bust_in_silhouette: Reply From: Zelestial
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.