How to raycast from code in Godot 4: PhysicsRayQueryParameters2D
By Eduardo Orellana · Updated
Build the query with PhysicsRayQueryParameters2D.create(from, to) and pass it to get_world_2d().direct_space_state.intersect_ray(query); the result is a Dictionary with collider, position and normal, or empty when nothing was hit. Godot 3's intersect_ray(from, to) fails to parse with Too many arguments for "intersect_ray()" call. Expected at most 1 but received 2.
AppImage, x86_64. Ubuntu 22.04, Debian 12, Fedora 39 or newer.
Intel or AMD Chromebook, with Linux turned on.
What Godot is checking
Godot 4 moved a ray's options (collision mask, exclusions, whether to hit areas) into a query object. intersect_ray() takes that one object, so two points are too many arguments and the first is the wrong type.
Query the space state in _physics_process(), when the physics space is in a consistent state. A RayCast2D node does the same every physics frame without code.
Reproduce it
A Godot 3 ray query, in a Godot 4 project.
extends Node2D
func _physics_process(_delta):
var space = get_world_2d().direct_space_state
var hit = space.intersect_ray(Vector2.ZERO, Vector2(200, 0))
print(hit)
What Godot 4.7.2 prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
SCRIPT ERROR: Parse Error: Too many arguments for "intersect_ray()" call. Expected at most 1 but received 2.
at: GDScript::reload (res://answers/how-to-raycast-from-code-in-godot-4/repro.gd:5)
SCRIPT ERROR: Parse Error: Invalid argument for "intersect_ray()" function: argument 1 should be "PhysicsRayQueryParameters2D" but is "Vector2".
at: GDScript::reload (res://answers/how-to-raycast-from-code-in-godot-4/repro.gd:5)
ERROR: Failed to load script "res://answers/how-to-raycast-from-code-in-godot-4/repro.gd" with error "Parse error".
at: load (modules/gdscript/gdscript_resource_format.cpp:46)Fix it
A ray from the origin to the right, toward a StaticBody2D named Wall at x = 120.
extends Node2D
func _physics_process(_delta: float) -> void:
var space := get_world_2d().direct_space_state
var query := PhysicsRayQueryParameters2D.create(Vector2.ZERO, Vector2(200, 0))
var hit := space.intersect_ray(query)
if hit:
print("hit ", hit.collider.name, " at ", hit.position, ", normal ", hit.normal)
else:
print("nothing")
set_physics_process(false)
What the fixed script prints
Godot Engine v4.7.2.stable.mono.official.ed1daf0bf - https://godotengine.org
hit Wall at (110.0, 0.0), normal (-1.0, 0.0)Verified on Godot 4.7.2
Both scripts on this page were run headless on Godot 4.7.2 (4.7.2.stable.mono.official.ed1daf0bf) on 18 October 2026, and the output above is what Godot printed, unedited.
Where the check lives in Godot's source: [gdscript_analyzer.cpp line 3484, the message](https://github.com/godotengine/godot/blob/4.7.2-stable/modules/gdscript/gdscript_analyzer.cpp#L3484), [PhysicsRayQueryParameters2D in the class reference](https://github.com/godotengine/godot/blob/4.7.2-stable/doc/classes/PhysicsRayQueryParameters2D.xml).
Questions
- How do I make the ray ignore the player?
- query.exclude = [player.get_rid()], or set query.collision_mask to leave out the player's layer.
- What is the 3D version?
- PhysicsRayQueryParameters3D.create(from, to) with get_world_3d().direct_space_state.intersect_ray(query).
Related
- How to get collisions after move_and_slide() in Godot 4
- Invalid argument for "…()" function: argument 1 should be "…"
- Every verified Godot 4 answer
- RayCast in Godot 4: it is RayCast3D now
- PhysicsDirectSpaceState in Godot 4: it is PhysicsDirectSpaceState3D now
- Can Nemotron 3.5 Super VL write working Godot 4 code? 11 of 15 on the first try
- Can Grok 4.3 write working Godot 4 code? 11 of 15 on the first try
- Can Laguna S 2.1 write working Godot 4 code? 9 of 15 on the first try
Flockbay's agent reads Godot's debugger output and fixes errors like this one in your project, on the official Godot.
Download Flockbay free