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.
- Teleport on
body_entered: 29 and 30 teleports in one second at 60 physics ticks, 71 and 73 at 144, about one every two ticks. Which portal the player was on at the end of the second changed between runs. - A 0.5 s cooldown per body: 1 teleport, and the player stayed at the exit. But stepping off the exit and back on 0.3 s after arriving did nothing, and the player stayed on that side.
- The script: 1 teleport. Stepping off the exit and back on 0.3 s later sent it back through, and standing on that portal for 2 s did not send it again.
- Landing 3 m from the exit, outside its shape: 1 teleport, the lock was cleared, and walking into the exit afterwards sent the player back.
- Two players stepping on at the same time: 2 teleports, one each, and both stayed at the exit.
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
- A smooth follow camera that slides across the level after the jump: follow camera after a teleport.
- A short-range blink that must not end inside a wall: blink teleport with ShapeCast3D. Areas that should count what is still standing on them: a pressure plate.
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
- 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
- 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
- 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