2025/06/20

- Type
- Learning Resource
- Format
- Study Guide
- Version
- Godot 4.x
- Subject Tags
- Created
- Updated
- 2025/05/13
- 2025/06/20
Raycasts are one of the most versatile tools in a game developer's toolbox, and you'll find yourself using them in many situations.
A raycast is an invisible line that detects and reports physics objects it hits. That's it! Despite this simplicity, raycasts are powerful in practice. You can use them to create lines of sight, handle ground detection, implement weapons, and much more.
In this guide, you'll learn:
A raycast is an invisible line with a direction and a limited length. It checks for collisions with physics objects along its path and gives you information about the first object it hits.
Think of a raycast like a laser pointer. It travels in a straight line until it hits something, and then it tells you what it hit and exactly where the collision happened.
When a raycast hits something, it reports:
You can use this information to create all sorts of gameplay mechanics - from spawning an impact visual effect where a bullet hits a wall to reflecting a beam of light that lands on a mirror.
Godot has two nodes for raycasting in 2D and 3D games, respectively:
Both nodes work similarly. The most important property of a raycast node is target_position
, which defines:
In 2D, this is a , while in 3D it's a Vector2 value. Vector3
The start position of the ray is controlled by the node's position in the scene, and the target_position
property is relative to this starting point.
By default, raycasts are enabled and check for collisions on every physics update automatically. You can turn them off by changing the Enabled property and call the force_raycast_update()
function to control exactly when the ray updates. This gives you more control over performance and precision.
Also, by default:
Let's look at an example of setting up and using a raycast in 2D where we make the ray point 500 pixels downward:
extends Node2D
@onready var ray_cast_2d: RayCast2D = $RayCast2D
func _ready() -> void:
ray_cast_2d.target_position = Vector2.DOWN * 500.0
func _physics_process(delta: float) -> void:
if ray_cast_2d.is_colliding():
var collision_point: Vector2 = ray_cast_2d.get_collision_point()
var collision_normal: Vector2 = ray_cast_2d.get_collision_normal()
var collider: Node2D = ray_cast_2d.get_collider()
There are four important raycast functions in this code:
is_colliding()
: Returns true if the raycast is currently colliding with something. Use it to check if the raycast hit something before trying to get the collision information.get_collision_point()
: Returns the position of the collision in global coordinates.get_collision_normal()
: Returns the surface normal of the surface at the collision point.get_collider()
: Returns the physics body or area that was hit by the raycast.Now, here's the 3D equivalent. In this example, we point the ray forward (remember that the negative Z-axis is the forward direction in Godot):
extends Node3D
@onready var raycast: RayCast3D = $RayCast3D
func _ready() -> void:
raycast.target_position = Vector3.FORWARD * 8.0
func _physics_process(delta: float) -> void:
if raycast.is_colliding():
var collision_point: Vector3 = raycast.get_collision_point()
var collision_normal: Vector3 = raycast.get_collision_normal()
var collider: Node3D = raycast.get_collider()
Here are some of the most common ways raycasts are used in games:
Don't stop here. Step-by-step tutorials are fun but they only take you so far.
Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…