← Pixel Dreamer Studio

Blink forward without going into or through walls

Sweep the character's own collision shape and stop at the safe fraction.

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.

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

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