How Do I Add KnockBack

I’m wondering how to add knock back to my game in my platformer?
I want to so that my shotgun will give the player a massive knock back and will launch the player, this will be your way of jumping. I want to know how will this be possible, i want it so that when you go flying the speed will stop when you hit the floor.
If you need any more pictures then I can add as many as you want

These are more screenshots since it doesn’t allow new users to use more than 1 image for some reason

Here’s a potential solution. Add a variable called “KnockBack_Force” to the player character, and use the following:

I would say it helps
Desktop 2025.01.08 - 21.58.09.02
but when the gun is facing certain points it goes into the direction that I don’t want it to go? Do you know a fix on this?

Instead of checking the horizontally flipped status, check whether the cursor is to the left or right of the player, and apply the force accordingly.

1 Like

thank you! here is what I have for the code!

Since you already have the angle of the shotgun, why not just apply force in the opposite direction (i.e. shotgun.angle + 180)

1 Like

I’ll have to try that

the only problem is that If i were to shoot an object that is directly under me (180 deg) it wouldn’t go as high if i shot at 250 deg, I’m trying to re work it so that depending where you shoot the shotgun would determine how high or how far you would travel

I’m not sure what you mean, my suggestion is to apply force opposite from the firing angle, which will always be (angle +/- 180). If this is done with basic Newtonian math then firing straight down will give you the highest jump, and firing down at a 45 degree angle will give you the longest jump.

The events that I’m seeing above do not make use of trigonometry so they will not emulate real world physics. You need cos() and sin() in order to get the proper values for X and Y forces.

If that is not something you want to learn about, you might want to consider using the Physics2 behavior which provides (semi) realistic physics. (just be aware that Physics2 does not play nice with Platformer behavior, if you try to use both at the same time then they will interfere with each other)

that is what I’m looking for! the only problem is that I’m no good with math so cos and sin seem a bit worrisome, do i add Shotgun.angle+/-180 to X and Y events? also if I were to use physics2 what would be my issues? and what would I need to replace?

I’m only asking since I added quite a bit of code to this and I’m not to sure what I need to replace (and would I need to get rid of the platformer behavior?)

1 Like

THANK YOU!!! I Will be experimenting with this more but so far it is good thanks!

1 Like

The reason i put that angle var into text object is for you to see at what angle of player to cursor
Player will be pushed where

So if for example you do not want player to be pushed into the ground (so like sliding when shooting diagonally up) then you can in add force condition that angle var is less or greater than some value

Same as to prevent player from air jumping with knockback

You can add to it condition player is on the floor
Or check if player is in the air then limit at what angles what force is added

Same as reset it to 0 as soon as from not being on the floor state player hits the floor

A LOT of ways you can adjust to your needs you just need to test what and when should happen and limit that to numbers you see

Other thing is while force var is above 0 you could change player falling speed and/or block player controls

AGAIN A LOT OF CRAP you can do limited by just how much effort you want to put into this

1 Like