When creating interactive FiveM servers, one of the most powerful tools you can use is raycasting. Raycasting allows you to detect objects, vehicles, peds, and surfaces in the game world by “casting a ray” from a point in a specific direction. This is essential for building features like shooting mechanics, targeting systems, interaction prompts, and custom game logic.
In this post, we’ll cover:
- What raycasting is in FiveM
- How it works with client scripts
- Common use cases for raycasting
- Best practices for implementing raycasts
What is Raycasting?
Raycasting is the process of projecting an invisible line (or “ray”) from a source point in a specific direction and detecting what it hits. Think of it as shining a laser pointer in the game world and seeing what objects it intersects.
In FiveM, raycasting is used to detect:
- Players or NPCs (peds)
- Vehicles
- Props and objects
- Ground surfaces
By using raycasting, you can make your scripts more precise, responsive, and interactive.
How Raycasting Works in FiveM
FiveM provides several native functions for raycasting, with StartShapeTestRay being the most common. This function lets you cast a ray and retrieve information about what it hits.
Example: Basic Raycast in Front of the Player
local playerPed = PlayerPedId()
local pos = GetEntityCoords(playerPed)
local forwardVector = GetEntityForwardVector(playerPed)
local rayLength = 10.0 -- distance in GTA units
local hit, hitCoords, entityHit = StartShapeTestRay(
pos.x, pos.y, pos.z,
pos.x + forwardVector.x * rayLength,
pos.y + forwardVector.y * rayLength,
pos.z + forwardVector.z * rayLength,
-1, -- mask (-1 = all)
playerPed,
0
)
local _, hitBool, endCoords, surfaceNormal, entityHit = GetShapeTestResult(hit)
if hitBool then
print("Hit entity: " .. tostring(entityHit))
end
In this example:
- A ray is cast 10 units in front of the player.
GetShapeTestResultreturns the entity hit, coordinates, and surface information.- You can use this information for interactions, shooting mechanics, or targeting.
Common Uses for Raycasting
Raycasting is versatile and can be applied in many ways:
- Player Interactions – Detecting what object or ped a player is looking at to trigger actions (e.g., opening a door, picking up an item).
- Shooting Mechanics – Checking if a bullet hits a target or registering hits in combat systems.
- Vehicle Detection – Detecting obstacles, parking spots, or nearby vehicles for custom driving scripts.
- Environment Scanning – Determining if the player is aiming at the ground, a wall, or a prop for placing objects or decals.
Best Practices for Raycasting
- Optimize for performance – Avoid casting rays every frame for many entities; limit checks to necessary intervals.
- Use correct masks – Masks determine which types of entities the ray can hit; this helps reduce unnecessary hits.
- Combine with events – Trigger server events when specific ray hits occur for multiplayer synchronization.
- Account for distances – Longer rays may hit unintended entities; adjust length based on context.
FiveM Development Course
Raycasting is an essential tool for FiveM developers who want to build interactive and immersive gameplay. By detecting objects, peds, vehicles, and surfaces in the world, you can create features like targeting systems, dynamic interactions, and custom mechanics.
In our Learn FiveM Development course, you’ll practice using raycasting in hands-on projects, ensuring you understand how to implement it effectively for both client and server scripts.