Player character being pushed away when hit/hurt by enemy

Hey everyone! I’m a n00b at Gdevelop. I’ve completed the two official tutorial games so I know some basic stuff. What I’m trying to accomplish is that a character (in top down style game) gets pushed away from an enemy when he is hurt. I guess this has something to do with the collision-system? I’ve managed to create walls (separate objects) but I can’t seem to figure out how to make the enemies push the character. Looking forward to any help available!

Thanks!

How did you create the walls.I was making a project and in this the player tends to stick to the walls

I used the “separate objects” action. You’ll find it under “Common for all objects” → “position”. And the condition was collision between player and wall. Sorry for the late reply!

Unfortunately this isn’t trivial, since the top down movement behavior doesn’t use physics directly. You need to simulate forces by taking over the controls of the player, temporarily increase the players movement speed and move him automatically by simulating key presses while also deactivating the direct controls.
So when the player collides with the enemy you need to analyze the movement angle of the enemy with which the enemy moves towards the player and decide based on the direction where to move it to: (Wiki article: The “angle” section shows a circle with the degrees)

On collision Enemy <> Player: 
  - Save the angle into a variable
  - Switch into a "player got hit by enemy" state (see link below)
  - Activate "Ignore default controls" (so that the player can't control the PlayerObject anymore)
  - Increase the player speed in the top down movement behavior to a high amount
  - Decide upon the angle:
    - The angle is close to "0°": Perform action "Simulate right key press"
    - The angle is close to "45°": Perform action "Simulate right key press" and "Simulate down key press"
    - The angle is close to "90°": Perform action "Simulate down key press"
    - The angle is close to "135°": Perform action "Simulate down key press" and "Simulate left key press"
    - etc.
  - Move the player for a couple of milliseconds (you can use a timer for that)
  - Change into the regular "movement" state
  - Reset the player speed to the normal amount
  - Activate default controls again (so that the player can control the PlayerObject again)

In order to keep the logic manageable I’d strongly advise you to use a state machine for your movement. You can also create a state machine based on variables instead of animations.