Sweep the character's own collision shape forward with a ShapeCast3D and move it by get_closest_collision_safe_fraction() of the distance. In our Godot 4.7 test a character with a capsule 0.8 m wide blinked 5 m towards a wall 3 m away. Setting the position straight to 5 m ahead put it on the far side of the wall. A raycast from the middle of the body stopped at the wall's face and left the body inside the wall, and it let the body through a 0.7 m gap between two pillars. The script stopped 0.4 m short of the wall, and at the pillars, both times touching nothing.
The code
Add this as a child of your CharacterBody3D, which should face -Z, Godot's forward. Call blink() from your input. It copies the first CollisionShape3D it finds on the character, so the check always matches the body's real size.
extends Node
## Blink or shadow step: teleports a CharacterBody3D forward, but never into or through a wall.
## Add it as a child of the CharacterBody3D (facing -Z) and call blink().
## It sweeps the character's own collision shape, so it also stops at gaps too narrow for the body.
@export var distance := 5.0
var _cast := ShapeCast3D.new()
func _ready() -> void:
var body := get_parent() as CharacterBody3D
for c in body.get_children():
if c is CollisionShape3D:
_cast.shape = c.shape
_cast.position = c.position
break
_cast.enabled = false # only checked when blink() asks
_cast.add_exception(body)
body.add_child.call_deferred(_cast)
func blink() -> void:
var body := get_parent() as CharacterBody3D
var dir := -body.global_basis.z
dir.y = 0.0
dir = dir.normalized()
_cast.target_position = body.global_basis.inverse() * (dir * distance) # the cast's target is in its own local space
_cast.force_shapecast_update()
var f := 1.0
if _cast.is_colliding():
f = _cast.get_closest_collision_safe_fraction() # how far the body can go before touching anything
body.global_position += dir * distance * f
What we measured
A capsule 0.4 m in radius and 1.8 m tall, blinking 5 m forward. After each blink we asked the physics engine how many shapes overlapped the body where it ended. Two runs gave the same numbers.
- A wall 0.4 m thick, its face 2.8 m ahead. Setting the position: ended 5.00 m ahead, behind the wall. Raycast from the middle of the body: 2.80 m, overlapping the wall. The script: 2.38 m, overlapping nothing.
- Two pillars with a 0.7 m gap, narrower than the body. Setting the position and the raycast both ended 5.00 m ahead, past the pillars, because the ray went through the gap. The script: 2.60 m, against the pillars.
- Open ground: all three 5.00 m.
- The character turned 90 degrees to the left, with a wall on that side: the script went 2.38 m that way, overlapping nothing.
Why a raycast is not enough
A ray is a line with no width. It tells you where the middle of the character would hit a wall, but the body is wider than that line, so putting the character at the hit point leaves half of it in the wall, and a gap the ray fits through may be too narrow for the body. A ShapeCast3D sweeps the whole shape, and get_closest_collision_safe_fraction() gives how far it can go before it touches anything.
What to change
distance: how far the blink goes on open ground.- To blink in the direction the player is pressing instead of the way the character faces, replace
dirwith that direction. - Leave out walls the player should be able to pass, such as a ghost wall, by putting them on a collision layer the ShapeCast3D does not check.
A pair of portals instead of a short step: a two-way portal that does not bounce you back.
Effects for the start and the end of the step: Dark & Shadow VFX for Godot 4 has 44 presets, including Shadow Step, Void Portals, Soul Drain, Binding Chains, Curse Marks and Shadow Auras, in shadow, void, crimson and necrotic colors.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Dark & Shadow VFX for Godot 4 has 44 ready-made dark magic effects, including Shadow Step, Void Portals, Soul Drain, Binding Chains and Curse Marks.
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
- 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