[SOLVED] Change Enemy Direction Animation

Hi everyone, I’m fairly new to Gdevelop and this is my first post! :smile:

I’m using 4 directional top down behaviour, there’s a Player object and there is an Enemy object.
The Enemy object will follow the Player object.
I have created 4 frames of animation for the Enemy object which shows if it is facing up, down, left or right.
The Enemy object is supposed to change the animation that it uses depending on the direction FROM the Player object so that it appears to be facing them as it chases them. For example, if the Enemy is to the right of the Player, the Enemy will be using animation 3 and looking left, if the Player moves up, in front of the Enemy, the animation switches to 1 so that the Enemy is facing up.
Unfortunately, I cannot figure out how to do this as nothing I have tried has worked.

Here are a few examples of conditions that I have tried that do not seem to work, some may seem illogical, the experience has been very frustrating for me - I feel I’m not seeing something very simple.
Sometimes some animations change, some do not and in almost every test I have ran, no more than 1 changes correctly.
It’s worth noting that I have also played with inversions and I have tried a number of these by making 4 conditions and setting the degrees to 0, 90, 180 and 270 respectively and/or < >for the X’s and Y’s.
Following that in the action I have - Change the number of the animation of Enemy: set to 1. If there’s a change in the condition, I change the animation number respectively.

  • Enemy is rotated towards Player
  • Enemy distance to Player is below Enemy.Direction() pixels
  • Enemy distance to Player is below Enemy.AngleToPosition(0, 0) pixels
  • Enemy distance to player is below Player.AngleToPosition (0, 0) pixels
  • Enemy distance to Player is below Enemy.AngleToObject(Player) pixels
  • The direction of Enemy = Player.AngleToObject(Enemy)
  • The X position of Enemy > Player.X()
  • The X position of Enemy > Enemy.SqDistanceToPosition(Player.CenterX(),Player.CenterY())
  • Enemy distance to Player is below Player.XFromAngleAndDistance(0, 4000)
  • The X position of Enemy > Player.XFromAngleAndDistance(0, 4000)
  • The angle (in degrees) of Enemy = Player.TopDownMovement::Angle()
  • The angle (in degrees) of Player > 0

If anyone can help me learn how to make the Enemy switch to the right animations as they chase the player I’ll be very happy and grateful. Thank you for your time!

My way to make it is:

  • Name your enemy animations like Walking0, Walking90, Walking180, Walking-90, each one facing toward such angle. (Strangely for staying the angle is 270, but for walking it works with -90)
  • Use the event: Change animation by name to “Walking”+ToString(round(Enemy.Angle()/90)*90)

The round(EnemyAngle()/90) returns a value between 0 and 3, (-1 and 2 for walking), multiplied by 90, gives you the right angle.

If you don’t want to use names, but use animation number, you can use the change animation by number event, and set it to round(Enemy.Angle()/90).

The one with the names works really good, it is the one I use the most.

In my case, I use a text variable named Status that holds values like “Idle”, “Walking”, so I can use the same code to change the animations.

I hope it helps you

2 Likes

Hi here are my solutions.

1-Is using the conditions of the keys pressed

2-Is using the conditions of the positions of the player

If you have other problems with these solutions, I can help you

2 Likes

Thank you very much, both. I’ll let you know how I get on with each of these potential solutions shortly! :grin:

1 Like

I’ve done it! Maybe in a bit of a long-winded way but it’s working…! Unfortunately I failed to get any of the suggestions you guys made to work for me.

When I tried the method of multiplying and dividing the angles, I could only get my top facing animation to play… I suspect I was lacking sufficent understanding of linking to successfully do it this way. I do really like the thought of switching between different animation types via a text variable.

The key press method resulted in all of the Enemy objects on the screen turning at the same time as the Player object which isn’t quite what I have in mind, the next method was very close, the left and right worked perfectly and for a moment I felt rather giddy but unfortunately the top and bottom wouldn’t switch and play at all, no matter what variation I tried…

What I went with in the end was creating 4 collision objects, each the size of the resolution. Combined, the 4 objects made up the shape of a filled in X. I then set the collision masks to match that triangular shape they each have. In the Events I set it so that when the Enemy object collides with each of the animation objects, the animation switches to the correct one. I gave each of the objects the tween behaviour and set the opacity to 0, adjusted their directions so that the center of the X shape is over the Player object so that as the Player object moves around, each of the invisible 4 collision objects follow the player around to keep the Enemy animations in the right places as they spawn and chase the Player object.

If anyone out there goes through what I went through and are up for trying anything, I hope Google sends you here! :grin:

1 Like

Awesome, another way to change animations :slight_smile: . Glad to hear you make it work.

By the way, I use the linked condition because I start doing the game logic with very basic shapes (Det_enemigo), and then put the real graphics over them (Enemigo), and link them together.

1 Like