S&box Wiki
Home
/
Edit Traces
View
Edit
History
No Category
Developer Overview
The Project System
Publishing To Asset Party
Getting Started With Hammer
Mapping Basics
Mapping Entities
Advanced Mapping Techniques
Getting Started with Modeldoc
Animgraph & Animation
Physics
Modeldoc Nodes
Advanced Modelling
UI Basics
Styles & Stylesheets
Razor Templates
Game Menus
Materials
Built In Shaders
Shaders
Shader Reference
Sounds & Audio
Particles
Getting Started
Making Games
Input
Networking
Physics
Rendering
Editor & Tools
VR
Misc
Playing Guides
Console Commands & Variables
Dedicated Server
Log in to edit
Traces
<cat>Code.Physics</cat> <title>Traces</title> # 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. <upload src="ab118/8dc2c97c380c3f2.png" size="14274" name="8d9152587ef3390.png" /> # Tracing Traces are constructed using the [Trace](https://asset.party/api/Sandbox.SceneTrace) static functions, and configured using member methods. You run the trace and get the result using [Run()](https://asset.party/api/Sandbox.SceneTrace/Run). ``` var mytrace = Scene.Trace.Ray( startPos, endPos ); var result = mytrace.Run(); ``` All configuration methods return a new `Trace` object. This is a convenience thing (known as [builder pattern](https://en.wikipedia.org/wiki/Builder_pattern)), so you can format traces like this: ``` var result = Scene.Trace.Ray( startPos, endPos ).Run(); ``` or like this: ``` var result = Trace.Ray( startPos, endPos ) .IgnoreGameObject( player ) .IgnoreGameObject( playerVehicle ) .Size( 10 ) .Run(); ``` ## Size In the example above the `Size()` call 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. Please note that this cube would be axis aligned, meaning it cannot "rotate". ## RunAll Instead of ending your Trace with `Run()` function, you can use the [`RunAll()`](https://asset.party/api/Sandbox.SceneTrace/RunAll) function. Using `RunAll()` will tell your Trace that you don't want to stop at the first entity who's been hit. Instead it'll return an array containing a TraceResult for each entity that was hit along the path of the trace. An example use case for this function would be for bullets that penetrate through objects. ``` // Will return TraceResult[] that you can iterate over var tr = Scene.Trace.Ray( startPos, endPos ) .RunAll(); ``` # TraceResult The Trace Result is a simple structure. It gives you information such as whether and where the Trace hit, what it hit, etc. Here you can see testing whether the trace hit and using the hit position to spawn an entity 10 units above it. ``` //go = gameobject reference if ( tr.Hit ) { go.Clone(tr.EndPosition + tr.Normal * 10); } ``` # Box, Sphere, Capsule, PhysicsBody traces In addition to ray/line Traces, you can also trace with other shapes, using other static methods of the `Trace` class, for example an Axis-Aligned Bounding Box trace: ``` public void TraceVisualizer( GameObject player, GameObject camera float maxDist ) { // This function should be called every frame ( for example in Update() ) var startPos = camera.Transform.Position; var dir = camera.Transform.Rotation.Forward; var box = new BBox( new Vector3( -30, -10, -30 ), new Vector3( 30, 0, 30 ) ); // Can also do Scene.Trace.Ray( .. ).Size( box ) var tr = Scene.Trace.Box( box, startPos, startPos + (dir * maxDist) ) .IgnoreGameObject( player ) .IgnoreGameObject( camera ) .Run(); // Check to see if the ray hit a surface if ( tr.Hit ) { // Draw the line from start pos to hit pos Gizmo.Draw.Line( startPos, tr.HitPosition ); // Move the box to the hit position, and adjust for weird shapes, and draw the final position. var finalBox = box.Translate( tr.HitPosition - box.Center ); Gizmo.Draw.LineBBox( finalBox ); } } ``` <upload src="b573a/8dcac29792af95a.mp4" size="8476599" name="sbox-scene-trace.mp4" /> Note how the box shape does NOT rotate with the player's view. The hit position will always be in the center of a given shape. Other shapes, such as spheres and capsules, work in the exact same way. `Trace.Body` allows you to trace any `PhysicsBody`, and therefore any custom shape. It **DOES** preserve the body's rotation.
S&box Wiki
Development
Developer Overview
6
Editor Overview
General FAQ
System Requirements
The s&box wiki
Troubleshooting
Useful Links
The Project System
4
Adding Assets
Creating a Game Project
Project Settings Window - Games
Project Types
Publishing To Asset Party
2
Uploading assets
Uploading projects
Hammer
Getting Started With Hammer
3
Getting Started With Hammer
Making Your First Map
Mapping Resources
Mapping Basics
7
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Using Entities That Require a Mesh
Mapping Entities
2
Creating a Door
Light Entities
Advanced Mapping Techniques
8
Collaborating With Prefabs and Git
Instances
Prefabs
Quixel Bridge Plugin
Tilesets
Tilesets-Advanced
Tilesets-Proxies
VIS Optimizations
Models & Animation
Getting Started with Modeldoc
7
Automatic Model Setup
Breakpieces
Creating a Model
Guide to Models
Importing Rust Weapons
LODs
ModelDoc FAQ & best practices
Animgraph & Animation
4
Animations without Animgraph
AnimEvents, AnimGraph Tags, Attachments
Animgraph
Delta Animations
Physics
3
Cloth Physics
Collisions, Physics & Surface Types
Jiggle Bones
Modeldoc Nodes
1
Custom ModelDoc nodes
Advanced Modelling
6
Bodygroups
Citizen
First Person
IKChains and Stride Retargeting
Morphs
Vertex Normals
User Interface
UI Basics
6
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
UI Basics
UI with Components
Styles & Stylesheets
1
Video Backgrounds
Razor Templates
4
A Razor Overview
Aliases and SetProperty Attributes
Generic Components
Templates
Game Menus
1
Making a Custom Pause Screen
Materials & Shaders
Materials
5
Guide to Materials
Material Attributes
Material Resources
Texture Settings
Using Dynamic Expressions
Built In Shaders
2
Foliage Shader
Glass Shader
Shaders
4
Compute Shaders
Constant Buffers
Material API
Shading Model
Shader Reference
5
Anatomy of Shader Files
Getting rid of Tex2D macros
Shader Reference
Shader States
Texture Format Cheat-Sheet
Other Assets
Sounds & Audio
4
Guide to Sounds
Sound Events
Sound Occlusion
Soundscapes
Particles
5
Creating animated sprites
Creating your first particle effect
Understanding Particle Editor
Using custom sprites
Using particle systems from C#
Coding
Getting Started
5
Cheat Sheet
Learning Resources
Setting up Rider
Setting up Visual Studio
Setting up Visual Studio Code
Making Games
2
Components
GameObjects
Input
4
Commands
ConVars
Input System
Speech Recognition
Networking
7
Auth Tokens
Http Requests
Lobby System
Networked Types
Networking Basics
RPCs
WebSockets
Physics
5
Collisions
Hitboxes
Joints
Traces
Triggers
Rendering
3
Render Tags
RenderHooks
Scenes
Editor & Tools
5
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
10
Asset Types
Attributes and Component Properties
Backend API
Code Accesslist
CPU Performance Profiling
DisplayInfo
package/find
Setting Up A Navigation Mesh
Threaded Tasks
TypeLibrary
Playing
Playing Guides
3
Default Keybinds
Proton
s&box on macOS (Experimental)
Console Commands & Variables
1
Launch Arguments
Dedicated Server
1
Dedicated Servers