How to knock back an enemy on impact

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By xoheveras

2D platformer … The point is … when the player hit the enemy, he flew a couple of meters

:bust_in_silhouette: Reply From: gioele

Why don’t you try adding a Tween node to the enemy?
You can use it to interpolate the enemy position using a fixed offset to make it bounce away.

Sorry for the insolence, but … can you tell me how to do this? I’m just still a layman in this

xoheveras | 2020-08-29 02:56

First you need to add a Tween node in the scene tree using Godot GUI.

Then in script you can call the node this way:

$Tween.interpolate_property(self, 'position', global_position, new_position, 0.8, Tween.TRANS_QUAD, Tween.EASE_IN_OUT)
$Tween.start()

You could combine it with an animation if you want to emphasize the movement, maybe bouncing upwards. You could even use multiple interpolations to enable more complex movements (Tween can emit some signals and you can use a custom callback to trigger an action after a Tween has completed).

gioele | 2020-08-29 15:07