Overview
Raycasting allows you to detect collisions along a line or query the physics world for intersections. Godot provides several query types for different use cases.Direct space state
Physics queries are performed through the direct space state:Ray queries
Basic raycast (3D)
Cast a ray from one point to another:Basic raycast (2D)
Ray query parameters
Collision masks
Filter which layers to detect:Excluding objects
Exclude specific objects from detection:Hit from inside
Detect collisions when the ray starts inside a shape:When
hit_from_inside is enabled and the ray starts inside a shape, the collision normal will be Vector3(0, 0, 0).RayCast nodes
For continuous raycasting, use RayCast nodes:RayCast3D
RayCast2D
Exclude parent
Automatically exclude the parent body:Add exceptions
Exclude specific objects:Shape casting
Cast a shape along a direction to detect collisions:ShapeCast3D
ShapeCast2D
Point intersection queries
Check if a point intersects with any physics objects:3D point query
2D point query
Shape intersection queries
Query for intersections with a specific shape:3D shape query
2D shape query
Motion queries
Test if a shape can move without collision:Common use cases
Line of sight check
Ground detection
Mouse picking (3D)
Projectile path prediction
Performance tips
Use collision masks
Always set appropriate collision masks to avoid unnecessary collision checks.
Limit query frequency
Avoid performing complex queries every frame. Use timers for periodic checks.
Prefer RayCast nodes
For continuous raycasting, RayCast nodes are more efficient than manual queries.
Batch queries
When possible, batch multiple queries together instead of spreading them across frames.
Next steps
Collision shapes
Learn about collision shape types
Joints
Connect bodies with physics joints