Raycast
Line-of-sight shape tests from the camera, an entity, two points or a screen coordinate.
Line-of-sight shape tests from the camera, an entity, two points or a screen coordinate.
lib.raycast runs line-of-sight shape tests and returns a uniform RaycastResult. Aim from the
gameplay camera, an entity's forward vector, two world points, or a screen coordinate — without
hand-rolling StartShapeTestLosProbe.
local hit = lib.raycast.fromCamera(20.0)
if hit.hit then
print('looking at', hit.entity, hit.coords)
endEvery function returns a RaycastResult:
| Field | Type | Description |
|---|---|---|
hit | boolean | Whether something was hit. The other fields are only valid when true. |
coords | vector3 | Impact point. |
normal | vector3 | Surface normal at the impact. |
entity | number | Hit entity handle (0 if none). |
material | number | GTA material hash (asphalt, glass…), not a string. |
hitbooleantrue.coordsvector3normalvector3entitynumber0 if none).materialnumber| Function | Description |
|---|---|
fromCamera(distance, flags?) | From the gameplay camera along the view direction. Ignores the local ped. |
fromEntity(entity, distance, flags?) | From an entity along its forward vector. Ignores that entity. |
fromCoords(origin, target, flags?) | Between two absolute points. Ignores nothing. |
fromScreen(screenX, screenY, distance, flags?) | From the rendered camera through a normalized screen coord (0..1). For mouse-cursor pointing. |
fromCamera(distance, flags?)fromEntity(entity, distance, flags?)fromCoords(origin, target, flags?)fromScreen(screenX, screenY, distance, flags?)0..1). For mouse-cursor pointing.flags is the optional GTA hit bitmask (default -1 = everything): 1 map · 2 vehicle · 4 ped ·
8 object · 16 ragdoll · 32 glass. Combine them — 2 + 8 hits vehicles and objects only.
local front = lib.raycast.fromEntity(cache.vehicle, 50.0, 2 + 8)
if front.hit then print('obstacle ahead of the vehicle') end
local los = lib.raycast.fromCoords(GetEntityCoords(cache.ped), targetCoords, 1)
if not los.hit then print('clear line of sight') end
local cursor = lib.raycast.fromScreen(0.5, 0.5, 100.0)
print('center of screen:', cursor.entity)coords, normal, entity and material are garbage unless hit == true — always check hit.hit
first. The default flags = -1 hits everything, including foliage and decorative props, so filter
explicitly when you want game-relevant geometry. fromScreen creates and destroys a temporary camera
on every call — never run it inside a Wait(0) loop, throttle it to 50–100 ms.