Flatten the direction to the attacker onto the ground, normalize it and the character's forward direction, and compare their dot product with the cosine of your block angle. Without normalizing, the dot product grows with distance: in our Godot 4.7 test a check of dot > 0.5 blocked an attacker 5 m away at 80 degrees to the side, and did not block one straight ahead at 0.4 m. The script below, with a 60 degree block angle, gave the expected answer at all seven positions we tried.
The code
Add this as a child of the character, which should face -Z, Godot's forward. When a hit lands, call can_block(attacker.global_position) and play your block effect instead of the hit if it returns true.
extends Node3D
## Lets a character block only hits that come from in front of it.
## Add it as a child of the character (facing -Z, Godot's forward) and call can_block(attacker_position).
@export var block_angle := 60.0 ## degrees to either side of straight ahead
func can_block(from: Vector3) -> bool:
var to := from - global_position
to.y = 0.0 # compare on the ground, so an attacker on a ledge still counts as in front
if to.length() < 0.001:
return true
var ahead := -global_basis.z
ahead.y = 0.0
# both directions normalized: the dot is then the cosine of the angle, whatever the distance
return ahead.normalized().dot(to.normalized()) >= cos(deg_to_rad(block_angle))
What we measured
A character at the origin facing -Z, blocking up to 60 degrees to either side.
- Straight ahead, 0.4 m away:
dot > 0.5without normalizing said no. Script: blocked. - Straight ahead, 1.5 m: both blocked.
- 75 degrees to the side at 3 m, and 80 degrees at 5 m: without normalizing, both blocked. Script: not blocked.
- 45 degrees at 1 m: both blocked. 150 degrees, behind, at 2 m: neither.
- Straight ahead on a ledge, 1 m away and 2 m up: normalized but with the height left in, not blocked, because that direction points 63 degrees upward. Script, which flattens it first: blocked.
- With the character turned 90 degrees to the left, an attacker 3 m to its left was blocked and one 3 m to its right was not.
Two runs gave the same answers.
Why normalizing matters
The dot product of two directions is their lengths multiplied together and by the cosine of the angle between them. If the direction to the attacker is not normalized, its length is the distance, so a far attacker at a wide angle passes a check meant for a narrow one, and a very close attacker straight ahead fails it. Normalized, the dot product is the cosine alone, so the same angle gives the same answer at any distance. Flattening the height first means the answer depends only on where the attacker stands around the character.
What to change
block_angle: 60 covers a 120 degree arc in front. 90 blocks anything in the front half.- If your model faces +Z instead, use
global_basis.zforahead. - The same check finds backstabs: an attack from outside the arc behind the target is one it could not have blocked.
Ready-made block and parry effects: Combat Defense VFX for Godot 4 has 29 presets in Stylized and Retro PSX looks for souls-like and action games, including Parry Spark, Perfect Parry, Block Wood, Block Metal, a hex Block Magic shield and Guard Break, plus a 3D afterimage for dodges.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Combat Defense VFX for Godot 4 has 29 ready-made defensive combat effects in Stylized and Retro PSX looks, including Parry Spark, Perfect Parry, Block Wood, Block Metal, Block Magic and Guard Break.
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
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- 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