← Pixel Dreamer Studio

A pair of portals that sends you through once

Let a portal ignore the body that just came out of it, until that body steps off.

When portal A moves a body into portal B, B sees a body enter and sends it straight back, then A does the same, so the body bounces between them every couple of physics ticks. In our Godot 4.7 test two portals 10 m apart teleported a player who stepped on once 29 to 30 times in one second at 60 physics ticks, and 71 to 73 times at 144. Mark the body with the portal it came out of and have that portal ignore it until body_exited: 1 teleport, and stepping off and back on sends it through again.

The code

Save it as portal.gd on both Area3D portals, each with a CollisionShape3D, and set exit on each to the other one. arrive_offset moves the landing point in the exit portal's own space: in our test Vector3(0, 0, 3) put the player 3 m from the exit along its Z axis. teleported fires once per trip.

extends Area3D
## Two-way portal: a body that steps in comes out at the partner portal once,
## instead of bouncing back and forth between the two.
## Give it a CollisionShape3D child and set exit to the other portal, which runs this script too.

signal teleported(body: Node3D)

@export var exit: Area3D
@export var arrive_offset := Vector3.ZERO  # where it lands, in the exit portal's own space


func _ready() -> void:
	body_entered.connect(_on_enter)
	body_exited.connect(_on_exit)


func _on_enter(body: Node3D) -> void:
	if exit == null or _locked(body, self):
		return  # it just came out of this portal: wait until it steps off
	body.set_meta(&"portal_lock", exit)
	body.global_position = exit.global_transform * arrive_offset
	teleported.emit(body)
	# landed outside the exit's shape: it will never step off it, so unlock it.
	# An Area3D reports a body 2 physics ticks after it moves in, so wait 4 before checking.
	for i in 4:
		await get_tree().physics_frame
	if is_instance_valid(body) and _locked(body, exit) and not exit.overlaps_body(body):
		body.remove_meta(&"portal_lock")


func _on_exit(body: Node3D) -> void:
	if _locked(body, self):
		body.remove_meta(&"portal_lock")


static func _locked(body: Node3D, portal: Area3D) -> bool:
	return body.has_meta(&"portal_lock") and body.get_meta(&"portal_lock") == portal

What we measured

Two Area3D portals with a 1 m sphere, 10 m apart, and a CharacterBody3D capsule moved onto the first one. Two runs gave the same results, except the bounce counts, which moved by one or two.

Why it happens

Setting global_position is a jump, and the exit portal cannot tell a body that jumped in from one that walked in: both are a body entering, so it sends it on. The area reports a body about two physics ticks after it moves in, which is why the bounce rate rose with the tick rate. A cooldown hides the bounce with a time limit, so it also blocks a real second trip that comes too soon. The lock asks what actually matters: has this body left the portal it came out of? A body that lands outside the exit never enters it, so the script checks the exit four ticks later and unlocks it if it is not there.

What to change

The portals themselves: Dark & Shadow VFX for Godot 4 has Void_Portal in Void, Crimson and Necro colors as looping effects to place on both portals, and Shadow_Step one-shots to play() when teleported fires.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Dark & Shadow VFX for Godot 4 has Void Portal in Void, Crimson and Necro colors, plus Shadow Step one-shots to play() when a body goes through.

More how-tos: Hazards, traps and attacks