Use 'Launch Character' for knockback effects and the player flies back when attacked
Content Index
I'm going to show you an interesting setup that, although it doesn't really have a name, was used a lot in platform games. It's that typical behavior where, when a character is attacked—in this case by the plant—they do a small jump backward.
There are several interesting details here, because there's a pretty logical reasoning behind it.
I think it's mainly done to get the player out of the attack area. Do you understand? If the character doesn't back up, they continue to take damage.
And depending on the type of interaction you're trying to achieve (for example, if damage is dealt only by touching the enemy), without that jump and without an invulnerability time, the player would die almost instantly from constant contact.
Common Collision Problems
A common problem is that when two Meshes collide, one can get stuck inside the other. This can cause instant death, as the player wouldn't be able to escape, and that's something we usually don't want.
So, that backtracking is precisely to avoid those scenarios.
Basic Configuration
In short, we have the plant and the player (as you can see in the photo in this post), which are the ones we want to interact with. But this can be applied to any type of Mesh.
Usually, it's just that: the player and an enemy. But if you have a more complex game, with AI-controlled NPCs, you could also use this logic.
Then, when both bodies collide, an action can be executed.
The first thing I do is disable the plant's collisions.
This prevents the Meshes from getting stuck when they collide.
When the attack occurs, the plant becomes, so to speak, "transparent."
You can easily see this in the Mesh properties by searching for Collision.
If you set it to "No Collision," you'll see that the plant no longer collides.
Calculating the Recoil Vector
Here comes the "magic" part. It's a simple setup.
We get the position of the plant and the player.
Suppose this is the player and this is the plant. We want to calculate the distance between them, which in programming is represented as a vector.
We subtract their position vectors.
Since this is a 3D environment, the resulting vector has three components: height, length, and width.
This vector is what we need to calculate the jump direction.
Vector Normalization
What's the dilemma? Depending on who is on the left or right, the vector can be positive or negative.
That's why it's normalized, that is, it becomes a vector of length 1.
Then we multiply it by a magnitude to obtain the jump's thrust.
In my case, when the character jumps, I use a value of 1800 upwards.
For this backward jump, I use a smaller value.
Importance of Normalization
Normalization is key because it allows you to maintain the same direction regardless of scale or distance.
For example, if the plant is farther away, the vector would have a greater magnitude.
Without normalization, that jump would be larger simply because of the distance.
It also matters if you scale the object.
A large plant would generate longer vectors.
And if you don't normalize, the jump would vary in undesirable ways.
By normalizing, you always work with a vector of magnitude 1 and can better control the final calculation:

In practice, this allows the player's jump to always be the same, regardless of the enemy's distance or scale.
So, in short, that's the logic behind backflipping.
I also recommend disabling enemy collisions during attacks to avoid visual errors or bugs upon contact.
I agree to receive announcements of interest about this Blog.
Learn how to push the player back when an enemy plant attacks in Unreal Engine 5 using Blueprint to enhance gameplay and add reactions to your enemies. Ideal for action or adventure games.
- Andrés Cruz