codeAPI Reference
Sandbox.Vector3
Sandbox.Vector3
3D vector for positions, directions, and offsets.
Overview
Vector3 is the primary type for 3D coordinates and directions in s&box.
Creation
CSHARP
Vector3 pos = new Vector3(100, 200, 50);
Vector3 dir = Vector3.Forward;
Vector3 up = Vector3.Up;Constants
| Constant | Value |
|---|---|
| Vector3.Up | (0, 0, 1) |
| Vector3.Down | (0, 0, -1) |
| Vector3.Forward | (1, 0, 0) |
| Vector3.Backward | (-1, 0, 0) |
| Vector3.Left | (0, 1, 0) |
| Vector3.Right | (0, -1, 0) |
| Vector3.Zero | (0, 0, 0) |
| Vector3.One | (1, 1, 1) |
Operations
CSHARP
Vector3 a = new Vector3(10, 20, 30);
Vector3 b = new Vector3(5, 5, 5);
// Math
Vector3 sum = a + b; // (15, 25, 35)
Vector3 diff = a - b; // (5, 15, 25)
Vector3 scaled = a * 2f; // (20, 40, 60)
Vector3 divided = a / 2f; // (5, 10, 15)Vector Math
CSHARP
// Length
float len = dir.Length;
float lenSq = dir.LengthSquared; // Faster
// Normalization
Vector3 normal = dir.Normal; // Unit vector
// Distance
float dist = (a - b).Length;
// Dot product
float dot = a.Dot(b);
// Cross product
Vector3 cross = a.Cross(b);Common Methods
CSHARP
// Lerp (linear interpolation)
Vector3 mid = a.LerpTo(b, 0.5f);
// Distance to
float d = a.Distance(b);
// Clamp length
Vector3 limited = dir.ClampLength(100f);
// With
Vector3 modified = pos.WithZ(0); // Set Z to 0Directions
CSHARP
// From rotation
Vector3 forward = rotation.Forward;
Vector3 right = rotation.Right;
Vector3 up = rotation.Up;
// To rotation
Rotation rot = Rotation.LookAt(direction);Snapping
CSHARP
// Snap to grid
Vector3 snapped = pos.SnapToGrid(100f); // 100 unit gridSwizzle
CSHARP
// Rearrange components
Vector2 xy = pos.xy;
Vector3 withZ = pos.WithZ(100);
Was this helpful?