Use 'Launch Character' for knockback effects and the player flies back when attacked in Unreal Engine

Video thumbnail

This configuration implements the classic platformer game behavior where, upon being hit by an enemy, the player is knocked back a short distance.

Player knockback is not just a visual effect but a crucial gameplay mechanic with a logical reasoning behind it:

  • Avoid Constant Damage: The push immediately removes the player from the enemy's attack area. Without this jump, if damage is applied by simple contact, the player would continue to take damage and could die instantly, even with a brief period of invulnerability.
  • Prevent Getting Stuck: A common physics issue is that, upon collision, the meshes (player and enemy) can get stuck inside each other, preventing the player's escape. Knockback ensures they separate.

Temporary Collision Deactivation:

  • To prevent the meshes from getting stuck, immediately after the hit is detected, a key action is performed:
  • Action: The enemy Mesh's collisions (the plant) are deactivated.
  • Effect: By looking up the Collision property in the Mesh properties and setting it to "No Collision," the plant becomes "transparent" at a physics level, allowing the player to be pushed back without getting trapped.

2. Knockback Vector Calculation

The fundamental part is determining the exact direction of the push, which must be the opposite direction of the enemy.

a. Get the Direction Vector

  1. Positions: We get the player's position and the enemy's position (the plant).
  2. Vector Subtraction: We subtract the player's position vector minus the enemy's position vector:
    1. {Direction Vector} = {Player Position} - {Plant Position}
  3. The resulting vector has three components $(X, Y, Z)$ and points from the enemy toward the player.

b. Normalization and Application of the Push

The dilemma with vector subtraction is that the magnitude (length) of the resulting vector varies based on the distance and scale of the objects. This would cause the jump to be unintentionally larger if the plant is farther away.

  • Normalization: The resulting vector is normalized, converting it into a vector with a length of 1.
    • This ensures that the vector only contains the pure direction.
  • Final Calculation: The normalized vector is multiplied by a controlled magnitude, which defines the final knockback force:
    • {Final Push} = {Normalized Direction Vector} x {Push Magnitude}

Importance: By normalizing, the distance or the scale of the Mesh (a large plant would generate longer vectors) no longer affects the direction, allowing for consistent control over the push applied to the player (for example, $1800$ for the vertical jump and a smaller value for the horizontal knockback).


And if you don't normalize, the jump would vary undesirably.

By normalizing, you always work with a vector of magnitude 1 and can better control the final calculation:

Nodes for knockback jump

In practice, this allows the player's jump to always be the same, regardless of the enemy's distance or scale.

So, in summary, that is the logic of the backward jump.

I also recommend disabling the enemy's collisions during the attack, to avoid errors or visual bugs at the moment of 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

🇪🇸 En español