codeAPI Reference
Sandbox.Ray
Sandbox.Ray
Infinite ray for intersection tests.
Overview
Ray represents an infinite line starting from a point and extending in a direction. Used for raycasting and intersection tests.
Creation
CSHARP
// From position and direction
Ray ray = new Ray(origin, direction);
// From camera
Ray ray = Scene.Camera.ScreenPixelToRay(mousePos);Properties
Intersection
CSHARP
// Intersect with plane
float? dist = ray.IntersectPlane(plane, out Vector3 hitPoint);
// Intersect with box
float? dist = ray.IntersectBox(box, out Vector3 hitPoint);
// Intersect with sphere
float? dist = ray.IntersectSphere(center, radius, out Vector3 hitPoint);
// Intersect with triangle
float? dist = ray.IntersectTriangle(v0, v1, v2, out Vector3 hitPoint);Point on Ray
CSHARP
// Get point at distance along ray
Vector3 point = ray.Project(distance);
// Closest point to another point
Vector3 closest = ray.ClosestPoint(targetPos);Raycast
CSHARP
// Physics raycast
var tr = Scene.Trace.Ray(ray, 10000).Run();
if (tr.Hit)
{
Vector3 hitPos = tr.EndPosition;
}Usage
CSHARP
// Weapon firing
Ray aimRay = new Ray(gunPos, gunRotation.Forward);
var tr = Scene.Trace.Ray(aimRay, 10000).Run();
// Mouse picking
Ray mouseRay = Scene.Camera.ScreenPixelToRay(Input.MousePosition);
Was this helpful?