Revision Difference
util.TraceLine#561882
<function name="TraceLine" parent="util" type="libraryfunc">
<description>
Performs a trace with the given trace data.
⤶
<note>Clientside entities will not be hit by traces, however <page>ents.FindAlongRay</page> will detect them.
⤶
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.</note>⤶
Performs an infinitely thin, invisible Ray Trace (or "Trace") in a line based on an input <page text="Trace Structure table">Structures/Trace</page> and returns a <page text="Trace Result table">Structures/TraceResult</page> that contains information about what, if anything, the Trace line hit or intersected.
⤶
Traces intersect with the Physics Meshes of <page text="Solid">enums/SOLID</page>, <page text="Server-side">States</page>, <page text="Entities">Entity</page> (including the <page text="Game World">game.GetWorld</page>) but cannot detect Client-side-only Entities.
For a way to detect Client-side Entities, see <page>ents.FindAlongRay</page>.⤶
⤶
Traces do not differentiate between the inside and the outside faces of Physics Meshes. Because of this, if a Trace starts within a Solid Physics Mesh it will hit the inside faces of the Physics Mesh and may return unexpected values as a result.⤶
⤶
See Also:⤶
<page>util.TraceHull</page>⤶
</description>
<realm>Shared</realm>
<args>
<arg name="TraceData" type="table">The trace data to use. See <page>Structures/Trace</page></arg>⤶
<arg name="traceConfig" type="table">⤶
A table of data that configures the Trace. ⤶
⤶
For the table's format and available options see the <page>Structures/Trace</page> page.⤶
</arg>⤶
</args>
<rets>
<ret name="" type="table">Trace result. See <page>Structures/TraceResult</page>.⤶
⤶
Can return `nil` if <page>game.GetWorld</page> or its <page text="physics object">Entity:GetPhysicsObject</page> is invalid. This will be the case for any traces done before <page>GM:InitPostEntity</page> is called.</ret>⤶
<ret name="" type="table">⤶
A table of information detailing where and what the Trace line intersected, or `nil` if the trace is being done before the <page>GM:InitPostEntity</page> hook.⤶
⤶
For the table's format and available options see the <page>Structures/TraceResult</page> page.⤶
</ret>⤶
</rets>
</function>
<example>
<description>Using a function callback as filter.</description>
<code>
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 )
</code>
<output>The trace will only hit prop_physics or world.</output>
</example>
<example>
<description>Visualizes a trace.</description>
<code>
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 )
</code>
<output><upload src="70c/8dbdae6fcf05200.png" size="5612891" name="image.png" /></output>
</example>