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.
- Standing still. Adding the hit to
velocityonce: 0.00 m. The script: 1.07 m. - Walking towards the attacker. Once: 5.08 m forward, as if nothing had happened. The script: 4.02 m forward, about 1 m less.
- Walking away. Once: 5.08 m. The script: 6.15 m, about 1 m more.
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
- The strength in
knock(): withfrictionat 8, a strength of 8 slid about 1 m. Double it for about 2 m. friction: higher makes a shorter, sharper push.- To stop the player from walking out of a big hit, scale
move_inputdown whileknockback.length()is large.
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
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over