13 · Physics & collisions · 2D and 3D

Raycasting

An invisible line that tells you what it hits first.

A ray is a question: "if I draw a line from here in this direction, what do I hit first, and where?" Games ask it constantly: is the floor under me, can the enemy see the player, what is the mouse pointing at, did the bullet hit. It is cheap, precise and works the same in 2D and 3D.

←→ aimSpace fire↑↓ ray lengthTap or drag to aim and fire

Try this: Aim past a crate at the coin behind it, then shorten the ray with ↓ until it stops short.

rayhit pointsurface normal

Hit nothingDistance -Coins 0

Aim with the arrows or the pointer.

Prompt bit

Works for 2D or 3D in any engine (Unity, Unreal, Godot, web). Paste the short one for a quick fix, or the detailed one when you want it done properly.

Use raycasts for line-of-sight and hit checks: cast from the origin along a normalized direction with a max distance and a layer mask that excludes the caster, take only the closest hit, and cache results for 0.1 s when many enemies check every frame.

44 words · one sentence to paste as is

Where it's used

Common mistakes

Starting the ray inside the caster so it always hits itself, passing an unnormalized direction or infinite distance, assuming "hit all" results are sorted, and using a thin ray where a sphere cast is needed so characters squeeze through gaps.

Writing this with an AI assistant? See our best AI coding assistants for game developers.

Missing a concept? Request one.

Related concepts

New concepts, in your inbox.

One short email when we add new concepts, plus our weekly roundup of AI tools for game devs. Unsubscribe any time.