[SOLVED] How do I knock back a player character when it collides with an enemy?

Hey all,

How do you set up a “Knockback” event in GD5? I thought I understood how to complete this, but it
doesn’t seem to work.

With my current setup, my hero does get knocked back, but keeps getting knocked back, even after it should stop being moved/you continue pushing forward.

For reference: I have a Hero_1 sprite and a hidden Hero_1_Hitbox for movement. Here’s my current method

Condition:
Enemies is in collision with Hero_1_Hitbox
Variable InvulnHit of Hero_1 = 0
Event:
Do =  1 to Variable InvulnHit of Hero_1

Condition: 
InvulnHit of Hero_1 = 1
Event:
Reset the timer "Knockback" of Hero_1_Hitbox
Add to Hero_1_Hitbox an instant force, angle: 220 degrees and length: 200 pixels

Condition:
The Timer "Knockback" of Hero_1_Hitbox is greater than 0.3 seconds
Event: 
Stop object Hero_1_Hitbox
Pause Timer "Knockback" of Hero_1_Hitbox
Do = 0  to variable InvulnHit of Hero_1

Here’s a screenshot of what I have above in GD5 itself:

Any/all input is greatly appreciated.

1 Like

You’re resetting the timer at every frame, because your variable is always 1.
Instead, move the time reset in the first event.

You’re also always resetting things when the timer reach the end, Instead, do it when when the player is being hit only. (InvulnHit of Hero_1 = 1)

In other words:

  • When the player is hit, and is not already hit (variable = 0), then you mark him as hit (you set its variable to 1) AND reset its timer.
  • While the player is hit, you push him (but you DON’T reset its timer!).
  • When the timer reach the end AND the player is hit (i.e: variable = 1) then you stop him, and make him vulnerable again (you set its variable to 0). You can pause or reset the timer if you wish (though not strictly necessary, it’s better to pause it/remove it)

Finally, in my solution you can see that you’re using twice the same condition (Variable InvulnHit of Hero_1 = 1).
This means that you can group them in a single condition in an event, and have two sub events (or you can make simply the third event as sub event of the second one).

In a way, this grouping is a way to say “These are the events that are run when the player is being hit”.

1 Like

Thanks for the input! I think this got me where I wanted. I added some logic to invert the knockback direction if you’re heading the other direction, ensured people weren’t using it to get higher jumps if they’re jumping when hit, and expanded out the timer logic to also allow for being invincible for a few moments after being hit.

Here’s my final result, in case you have any other input or anyone else wants to see it. Testing seems to indicate it does what I want.

2 Likes

Cool! Thanks for posting the whole events, will be surely useful for someone else!

Hey I’m new to this github thing and how can i do that hero_1_hitbox thing?

Hello! In general, please don’t bump 2 year old threads.

However, take a look at the built-in platformer example. It uses a custom hitbox object like my demo game does as well.

You can also download the completed demo I did (and game project) which includes the above events at Not-A-Vania by Silver-Streak

2 Likes