I’m making a game with a platformer movement system, but I’m having trouble figuring out how to make enemies knockback the player a short distance when they are hit. I run into the issue of the players velocity seemingly not changing after their knockback should end. Another issue which I believe comes from the same problem is that if the player is moving left, but a projectile coming from the right hits them, instead of the player being knocked back infinitely, the player just stops moving. My goal is that when the player is hit, they will lose control for a small moment, with them being knocked back a short distance depending on the speed of the projectile which hit them.
To make this work you’ll need to set the player to not be able to move for a brief movement. This is so the arrow keys won’t cancel out the knock back velocity. Then when the player is unable to move set the velocity to change in the direction you want the player to go. Also create a variable for the enemy that determines what direction it is facing and make it so the player will go in a different direction depending on the way the enemy is facing.
I tried that, but the problem I run into is that when I set the players movement controls to 0, then it disables any ability for changing the velocity to work. I did fix a few of the previous problems though, the normal knockback works properly, just not walking into a projectile from the opposite side.
https://makecode.com/_M8f0gHbWjUXx (this link is updated to disable player movement when hit, and to set the players velocity to 0 right before their velocity is edited. For some reason the second change causes none of the hitback code to work)
https://makecode.com/_4DqehADE6U5T (this link is almost the same as the one above, but instead it sets the players movement on the X axis to be 1 while they are being hit, this allows for the player to be knocked back unless they are walking into the projectile.)
(sorry if I entirely misunderstood what you said.)
I made a bunch of changes and put comments on everything I changed! I didn’t change how hitting the laser beam works, just the normal projectiles. I also added that screen boundary you needed. The code I changed is in On Start, one of the Forever loops, and one of the “On Sprite Overlaps OtherSprite” blocks (You’ll see the comments). Some stuff I left for you to change, because this is just a quick example of some changes that kinda work how you want them to.
I would also like to mention that these animations, sprite work, and this color pallet are all absolutely beautiful and I would really love to play this game once it’s finished!!
← Figure 1
This works almost like the move (sprite) with buttons but also uses some friction, which is useful for knockback. You can also make a speed variable for this too.
Note that you should remove the move (sprite) with buttons to avoid conflicts with said block and Figure 1 shown above. Also, Figure 1’s left/right velocities were swapped.
Thanks! I will change the knockback to be based on size, as that makes much more sense than the speed. I am thinking of releasing a sort of demo to get some feedback on combat/movement when I finish the basic combat/movement system.