How to make knockback with kinematic Body 2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ismaelgame7
:warning: Old Version Published before Godot 3 was released.

Hello,

I wanted to know how to do a knockback with a kinematic body?
I need to do this for my 2D platform game, but I have no idea how to do it. :\

Can someone help me please?

Do you mean something like a small rebound when hitting a wall?

RandomShaper | 2017-07-26 22:34

I think he means like when hitting an enemy

manokara | 2017-07-27 19:53

:bust_in_silhouette: Reply From: RandomShaper

It heavily depends on how you are coding your character movement.

But a general idea would be to have a vector variable that stores the “impulse” and:

  • when the knockback has to happen set the impulse it to the direction your character has to rebound toward with some magnitude (*);
  • on each frame:
    • add it to whatever movement the character would normally do;
    • damp the impulse, for instance, by multiplying it by some number (*) lower than 1 until it reaches almost zero length, setting it to true zero then.

*: Both the initial magnitude and the damping coefficient may be determined with some test and error.

UPDATE: @eons’ suggestion about using a Tween or an animation is very relevant. I don’t know if that’s what he means, but they may allow you to avoid implementing the damping yourself and instead use a curve from those. You have to consider the tradeoffs, but that’s an option.

Usually, a tween or animation is enough, but these tend to mess up collisions, is possible to do it collision-aware, though.

Using a state machine should help you a lot on isolating the knockback behavior.

eons | 2017-07-28 11:27

:bust_in_silhouette: Reply From: the_dirtiest_rat

here’s my version of knock back system. It can be used in variety of ways in 2d form.

This is were the knockback happends:

 var collision = move_and_collide(velocity * delta)
    			if collision:
    				var reflect = collision.remainder.bounce(collision.normal)
    				velocity = velocity.bounce(collision.normal)
    				reflect = move_and_collide(reflect)
    			velocity = velocity.move_toward(Vector2.ZERO, 90 * delta)
    			
    			if velocity == Vector2.ZERO:
    				states = (It returns back to your previous states  after the knock back)

This is the function in were you put the direction on were to knock back:

func knockback(_point_from_knockback : Vector2):
	var knock_point = _point_from_knockback - global_position
	velocity = -knock_point.normalized() * 80
	states = (whatever state knock back is)