menu_bookDocumentation
Tracing - s&box Physics Documentation
Tracing
Tracing (raycasting) is the primary way to query the physics world in s&box. It allows you to cast rays or shapes through the scene to find collisions with colliders.
Simplest Trace
The most basic trace is a ray from point A to point B:
CSHARP
var tr = Scene.Trace.Ray( startPosition, endPosition ).Run();
if ( tr.Hit )
{
Log.Info( $"Hit {tr.GameObject.Name} at {tr.HitPosition}" );
}Using Collision Rules
Filter traces using your project's collision rule matrix:
CSHARP
var tr = Scene.Trace
.Ray( start, end )
.WithCollisionRules( "player", "enemy" )
.WithoutTags( "trigger", "invisible" )
.Run();Shape Traces
Traces aren't limited to rays - you can use shapes:
CSHARP
// Sphere trace
var tr = Scene.Trace
.Sphere( radius, start, end )
.Run();
// Box trace
var tr = Scene.Trace
.Box( bounds, start, end )
.Run();Trace Results
The SceneTraceResult contains detailed information about what was hit:
CSHARP
var tr = Scene.Trace.Ray( start, end ).Run();
if ( tr.Hit )
{
// The GameObject that was hit
var hitObject = tr.GameObject;
// The exact position of the hit
Vector3 hitPosition = tr.HitPosition;
// The surface normal at the hit point
Vector3 normal = tr.Normal;
// The distance from start to hit
float distance = tr.Distance;
// The physics body that was hit
var body = tr.Body;
// The collider component
var collider = tr.Collider;
}Advanced Filtering
CSHARP
var tr = Scene.Trace
.Ray( start, end )
.IgnoreGameObject( this.GameObject ) // Don't hit self
.IgnoreGameObjectHierarchy( player ) // Don't hit player or children
.WithTag( "solid" ) // Only hit objects with this tag
.WithoutTag( "water" ) // Ignore objects with this tag
.UseHitboxes() // Use hitbox collisions
.Run();
Was this helpful?