(Solved) Implementing simple knock back

Hi! Ive looked at the other available posts on this forum for help on how to implement knockback but have been unsuccessful in adding it in. Currently I am trying to use the impulse blocks since another post on knockback said to use impulse that is triggered when the enemy is hit either from its left or right side. I put this in to the logic for the slime enemy I have and I can see it runs once my player character hits the slime but nothing happens to the slime.

!Screen Shot 2021-04-13 at 11.04.56 AM|690x437
This is the code I put for the knockback that I modeled off of the other post I saw.


This is (mostly) the rest of the code for my slime creature (only excludes the walking animation but that’s not necessary for this) All I would want really for simplicity’s sake is to just have it be moved back a little, so nothing dramatic just an indicator that it was hit apart from an animation of it turning red. Does anyone know what I would need to fix for this to work?

Not sure if this would help but here’s the link to the game if you’d like to see the code for yourself. (Its very messy but this is my first run through a lot of this)

2 Likes

You could either turn the movement off for a second and then impulse the slime backwards, or just use a position block. The latter will be buggy, but it is easier.

Or just use a velocity, like a vector x, and a vector y, then it will move and if it’s affected by gravity it’ll fall smoothly.

Thanks for getting back!

What I did was put a switch in between the always and the get number for the velocity that switches off when the collision is triggered. This works most of the time but still a little glitchy. When the player hits the slime when the slime is facing it, everything works perfectly it goes back in the right direction. But, when the slime is facing away from the player and the player hits it, it’ll go in the direction it was hit not away if that makes sense. Im attaching a pic of what I added.

A solution that I’m thinking of would be to create a number block that represents the direction it’s facing (the slime) and have it alternate between 1 and 0, 0 representing right, 1 left. This var would start at left since the slime spawns left first and every time it flips it would update to 1 and vice versa. But this sounds a bit complicated and I’m not sure if it would implement perfectly.

1 Like

I’m not sure what this would look like and it might just be speaking to my lack of experience on this platform. Would it work in the same way you use velocity for walking in that you get the number on the trigger and set the velocity? The only way that makes sense to me would be if I would set the velocity to something super fast so it moves to that knock back position quickly and then have the line of reasoning switch off to the regular speed. Let me know if this is a misinterpretation :slight_smile: I’ll take all the help I can get on this.

That’s basically what I said lol

You can test for velocity and find if it’s going left or right that way.

yeah, filters or if it’s flipped, it can change the velocity from the output of the flip.

I used an extractor block to test the velocity and it works now! I did not know you could extract the velocity with these so this is way easier than my original idea. Thank you!

1 Like

No problem, glad I could help!