Garry's Mod Wiki

util.TraceLine

  table util.TraceLine( table TraceData )

Description

Performs a trace with the given trace data.

Clientside entities will not be hit by traces.

When server side trace starts inside a solid, it will hit the most inner solid the beam start position is located in. Traces are triggered by change of boundary.

Arguments

1 table TraceData
The trace data to use. See Trace structure

Returns

1 table
Trace result. See TraceResult structure.

Can return nil if game.GetWorld or its physics object is invalid. This will be the case for any traces done before GM:InitPostEntity is called.

Example

Using a function callback as filter.

local tr = util.TraceLine( { start = LocalPlayer():EyePos(), endpos = LocalPlayer():EyePos() + EyeAngles():Forward() * 10000, filter = function( ent ) return ( ent:GetClass() == "prop_physics" ) end } ) print( tr.HitPos, tr.Entity )
Output: The trace will only hit prop_physics or world.

Example

Visualizes a trace.

local color_red = Color( 255, 0, 0 ) local color_blu = Color( 0, 0, 255 ) local mins, maxs = Vector( -24, -3, -2 ), Vector( 24, 3, 2 ) hook.Add( "PostDrawTranslucentRenderables", "trace_visualize", function() local eyePos = Entity( 1 ):EyePos() + Entity( 1 ):GetRight() * -5 local eyeDir = Entity( 1 ):GetAimVector() local tr = util.TraceLine( { start = eyePos, endpos = eyePos + eyeDir * 10000, filter = Entity( 1 ) } ) render.DrawLine( eyePos + LocalPlayer():GetRight() * -5, tr.HitPos, color_red, true ) -- Show that the traceline is a line, not a hull render.DrawWireframeBox( tr.HitPos, Angle( 0, 0, 0 ), mins, maxs, color_red, true ) eyePos = Entity( 1 ):EyePos() + Entity( 1 ):GetRight() * 5 local tr2 = util.TraceHull( { start = eyePos, endpos = eyePos + eyeDir * 10000, filter = Entity( 1 ), mins = mins, maxs = maxs } ) render.DrawLine( eyePos, tr2.HitPos, color_blu, true ) render.DrawWireframeBox( tr2.HitPos, Angle( 0, 0, 0 ), mins, maxs, color_blu, true ) end )
Output:
image.png