Fire an invisible ray. s&box calls it a trace.

The problem
You need to know what you're aiming at.
Shooting, picking things up, checking for the ground. All of them need the same answer:
- What's in front of me?
- ?
- How far away is it?
- ?
- Where exactly did I hit?
- ?
The idea
Draw a line. See what it hits.
A trace runs a line from one point to another and reports the first thing it touches.
start→end=first hit
In code
Scene.Trace.Ray.
var start = WorldPosition + Vector3.Up * 64f;var end = start + WorldRotation.Forward * 500f; var tr = Scene.Trace.Ray( start, end ).Run(); if ( tr.Hit ) Log.Info( tr.HitPosition );
tr.Hit says whether it touched anything. tr.HitPosition is exactly where.
Watch out
It hits you first.
var tr = Scene.Trace.Ray( start, end ) .IgnoreGameObjectHierarchy( GameObject ) .WithoutTags( "trigger" ) .Run();
Start a trace inside your own body and the first thing it hits is you. Ignore yourself, and skip triggers.
The rule
Aim with a trace. Ignore yourself.
- Fire a ray
- Scene.Trace.Ray( a, b )
- Did it hit?
- tr.Hit
- Skip yourself
- IgnoreGameObjectHierarchy