S&box Wiki



What are traces

Traces are imaginary lines. When you run a Trace you get a TraceResult - which tells you what the line hit.

So for example, if you, as the player, wanted to spawn a box. You'd run a Trace from the player's eyeball to 200 units in the direction that the player is looking. The TraceResult would show that it hit a point and now you know where to place the box.



Traces are constructed using the Trace static functions, and configured using members. You run the trace and get the result using Run().

var mytrace = Trace.Ray( startPos, endPos ); mytrace = mytrace.WorldOnly(); var result = mytrace.Run();

The configuration functions return a new object. This is a convenience thing, so you can format like this:

var tr = Trace.Ray( startPos, endPos ).Run();

or like this

var tr = Trace.Ray( startPos, endPos ) .Ignore( playerEntity ) .Ignore( playerVehicle ) .Size( 10 ) .Run();


In the example above the Size describes the size of an AABB to trace.

This means instead of tracing a simple line, you're tracing a bigger cube along the line.


The Trace Result is a simple struct. It gives you information on the hit.

Here you can see testing whether the trace hit and using the hit position to spawn an entity 10 units above it.

if ( tr.Hit ) { var ent = new ModelEntity(); ent.SetModel( "models/sbox_props/watermelon/watermelon.vmdl" ); ent.Pos = tr.EndPos + tr.Normal * 10; }


Explain how to trace using other shapes

Special Pages



Render Time: 69ms

DB GetPage 45
Generate Html 1
SaveChanges (1) 16
Render Body 0
Render Sidebar 4