← Pixel Dreamer Studio

Knockback that the walk code does not cancel

Keep the push in its own vector, add it to the walk every frame, and let it fade.

Keep the knockback in its own vector, add it to the walk velocity every physics frame, and let it fade with lerp towards zero. Adding the hit to velocity once does nothing when the movement code sets velocity.x and velocity.z from the input every frame: in our Godot 4.7 test a standing character hit with a strength of 8 moved 0.00 m. With the script below it slid 1.07 m and stopped, and a character walking into the hit was still pushed back about 1 m.

The code

Use this as the script of your CharacterBody3D, or copy the parts into yours. Set move_input every frame from Input.get_vector() or your AI, and call knock(attacker.global_position, 8.0) when the character is hit. The push goes directly away from the attacker, along the ground.

extends CharacterBody3D
## A character that walks and can be knocked back by hits, without the walk code cancelling the push.
## Set move_input from your input or AI every frame, and call knock(attacker_position, strength) on a hit.

@export var speed := 5.0
@export var friction := 8.0  ## how fast the push dies away: higher = shorter slide

var move_input := Vector2.ZERO  ## x = right, y = back, like Input.get_vector()
var knockback := Vector3.ZERO


func knock(from: Vector3, strength: float) -> void:
	var dir := global_position - from
	dir.y = 0.0
	knockback += dir.normalized() * strength


func _physics_process(delta: float) -> void:
	var walk := Vector3(move_input.x, 0.0, move_input.y) * speed
	velocity.x = walk.x + knockback.x  # the walk is set fresh every frame, the push is added on top of it
	velocity.z = walk.z + knockback.z
	if not is_on_floor():
		velocity.y -= 9.8 * delta
	move_and_slide()
	knockback = knockback.lerp(Vector3.ZERO, 1.0 - exp(-friction * delta))

What we measured

A capsule on flat ground, walk speed 5 m/s, hit from the side with a strength of 8. We measured how far it moved in the second after the hit. Two runs gave the same numbers.

Why the push disappears

Most movement code sets the horizontal velocity from the input at the start of every physics frame. A hit usually arrives from a signal or another script between frames, so whatever it adds to velocity is replaced before move_and_slide() ever uses it. Keeping the push in a separate vector that the movement code adds on top means nothing overwrites it, and fading it each frame gives a slide instead of a jump.

What to change

More hit feel: hit flash on a 3D model and hit stop.

Effects for the hits: Battle VFX Bundle for Godot 4 has 169 effects from 5 packs: FPS gun effects, spell projectiles with cast, trail and hit, block and parry effects, AoE telegraphs and status effects such as burn, freeze, poison and stun.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Battle VFX Bundle for Godot 4 has 169 ready-made combat effects from 5 packs: gun effects, spell projectiles, block and parry, AoE telegraphs and status effects.

More how-tos for Battle VFX Bundle for Godot 4: Fast projectiles that never fly through enemies

More how-tos: Hazards, traps and attacks