Raycast (tm.raycast)
Context: client only. Every function yields (it waits for the game to answer), so call it from a thread.
All functions return the same five values:
lua
local hit, entityHit, endCoords, surfaceNormal, materialHash = tm.raycast.fromCamera()| Value | Description |
|---|---|
hit | true or false (never the raw 0/1 of the native). |
entityHit | Entity handle, 0 when nothing was hit. |
endCoords | Where the ray ended (the destination when nothing was hit). |
surfaceNormal | Normal of the hit surface. |
materialHash | Material hash of the hit surface. |
A probe the game rejects, or one that does not resolve within 60 frames, reports no hit instead of blocking forever.
Functions
| Function | Description |
|---|---|
tm.raycast.fromCoords(coords, destination, flags?, ignore?) | Ray between two positions. |
tm.raycast.fromCamera(flags?, ignore?, distance?) | Ray from the gameplay camera along the view direction. distance defaults to 10.0 metres. |
tm.raycast.cam(...) | Same as fromCamera. |
tm.raycast.expensive(ignore?) | Camera ray of 1000 metres for far targets. Costs more, use it sparingly. |
tm.raycast.fromEntity(entity, distance?, flags?) | Ray straight ahead of an entity (the entity is ignored). |
tm.raycast.hasLineOfSight(coords, destination, target?, flags?, ignore?) | True when nothing blocks the line, or when the only thing hit is target. |
Defaults: flags is 511 (everything), ignore is the player ped. tm.raycast.flags holds common flag values (world, vehicles, peds, ragdolls, objects, all) that you can combine with + or |.
lua
local hit, entity, coords = tm.raycast.fromCamera(tm.raycast.flags.objects, PlayerPedId(), 3.0)
if hit and DoesEntityExist(entity) then
-- the player is looking at an object within 3 metres
end
if tm.raycast.hasLineOfSight(GetEntityCoords(PlayerPedId()), GetEntityCoords(targetPed), targetPed) then
-- direct line of sight to the target
end