Why does your raycast miss?

You can't fix a line you can't see.

Why does your raycast miss?

The problem

An invisible line.

void Update(){    var origin = transform.position;    var dir = transform.forward;     if ( Physics.Raycast( origin, dir, out var hit, 50f ) )        Debug.Log( hit.collider.name );}

It logs nothing. Is the origin inside the floor? Is forward the way you think? You're guessing.

The fix

Draw the ray.

Debug.DrawRay draws a line in the Scene view, from a start point along a direction.

start+direction→Debug.DrawRay→a line you can see

The fix in code

Same numbers, now visible.

Debug.DrawRay( origin, dir * 50f, Color.red ); if ( Physics.Raycast( origin, dir, out var hit, 50f ) ){    Debug.DrawLine( origin, hit.point, Color.green );    Debug.Log( hit.collider.name );}

Red is where it looked. Green is where it hit. Watch the Scene view while you play.

Watch out

It draws the direction's own length.

A normalized direction draws a line 1 meter long. Multiply by the distance.

DrawRay( o, dir )
1 m long
DrawRay( o, dir * 50f )
the full 50 m
In the Game view
turn on Gizmos

The rule

Can't see it? Draw it.

Where it looked
Debug.DrawRay
Where it hit
Debug.DrawLine
Length
dir * distance