Team Fortress 2/Scripting/Script Functions/TraceLine: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Created page with "== Description == <code>bool TraceLineEx(table ''traceTable'')</code> Does a raycast along a line specified by two vectors, returning the first entity or geometry hit along t...")
 
Tag: New redirect
 
Line 1: Line 1:
== Description ==
#REDIRECT [[Team Fortress 2/Scripting/Script Functions/TraceLineEx]]
<code>bool TraceLineEx(table ''traceTable'')</code>
 
Does a raycast along a line specified by two vectors, returning the first entity or geometry hit along the way. Extended version of <code>TraceLine</code> and also serves as a script version of [[UTIL_TraceLine|UTIL_TraceLine()]].
Results are written to the passed-in table.
 
== Input ==
The only required information to make the trace is providing the start and end point. The rest are optional.
{| class="standard-table" style="width: 90%;"
! Name
! Type
! Description
|-
| start
| vector
| Point where to start the trace, in world coordinates.
|-
| end
| vector
| Point where to end the trace, in world coordinates.
|-
| mask
| int
| Optional contents bitmask to include or exclude common groups of geometry, such as fences, NPCs, playerclips, etc.<br>Default mask is <code>MASK_VISIBLE_AND_NPCS</code>. See constants page for a list of [[Team_Fortress_2/Scripting/Script_Functions/Constants#FContents|contents]] and [[Team_Fortress_2/Scripting/Script_Functions/Constants#MASK|common mask]] types.<br>
By default this will trace against the bounding box of models only. Add <code>CONTENTS_HITBOX</code> to the mask to instead perform precise hitbox tests
|-
| ignore
| entity
| Optional entity to ignore when tracing. [[worldspawn]] cannot be ignored.
{{tip|To ignore or filter multiple entities, see the [https://github.com/ficool2/vscript_trace_filter vscript_trace_filter] library.}}
|}
 
== Output ==
Output written by the function to the table.
{| class="standard-table" style="width: 90%;"
! Name
! Type
! Description
|-
| pos
| vector
| Point in world coordinates where the trace ended. Equal to end position if nothing was hit.
|-
| fraction
| float
| Fraction from the start to end where the trace ended. E.g. 0.0 is start, 1.0 is end.
|-
| hit
| bool
| Whether the trace hit something.
|-
| enthit
| entity
| If hit, the entity that was hit. This is not written to the table if <code>hit</code> was false.
|-
| startsolid
| bool
| Whether the trace started inside geometry. This is not written to the table if it's false!
{{tip|Define <code>startsolid</code> to false in the input table so you don't have to check the key's existence.}}
|-
| allsolid
| bool
| If true, plane information is unavailable (i.e. started and ended inside geometry or didn't hit anything). This is not written to the table if it's false!
|-
| startpos
| vector
| Starting position of the trace in world coordinates. Probably the same thing as <code>start</code>.
|-
| endpos
| vector
| Ending position of the trace in world coordinates. Same as <code>end</code> if it didn't hit anything, otherwise this is the hit point.
|-
| plane_normal
| vector
| If hit, the normal (unit) vector of the surface.
|-
| plane_dist
| float
| If hit, distance of surface plane from origin. Forms a plane equation with <code>plane_normal</code>.
|-
| surface_name
| string
| If hit, name of the surface's texture that was hit. Not available for displacements or models.
|-
| surface_flags
| int
| If hit, bitmask of the surface flags. See [[Team_Fortress_2/Scripting/Script_Functions/Constants#FSurf|FSurf]] constants.
|-
| surface_props
| int
| If hit, the [[surfaceprop]] of the surface. Note that is an index rather than a string. {{todo|Describe how to interpret this}}.
|}
 
== Return ==
''bool'' - False if the user didn't specify a valid <code>start</code> or <code>end</code>, true otherwise. You don't need to check this return usually.
 
== Example ==
<source lang=js>
local player = GetListenServerHost()
local trace =
{
    start  = player.GetOrigin() // start from feet
    end    = player.GetOrigin() + Vector(0, 0, -64) // trace 64 units down from feet
    ignore = player // don't hit ourselves
}
TraceLineEx(trace)
// visualize the trace
DebugDrawLine(trace.startpos, trace.endpos, 255, 0, 0, false, 10.0)
 
if(trace.hit)
{
    printl("Trace hit!")
}
else
{
    printl("Trace missed!")
}
</source>

Latest revision as of 11:54, 27 September 2024