What is a raycast?
Raycasts answer "what is in this direction, and how far?" Here is how they work and the jobs they do in almost every game: shooting, sight lines, cameras and footing.

Key takeaways
- A raycast traces a line from a point in a direction and returns the first thing it hits, with the hit point and distance.
- It is the standard tool for hitscan weapons, enemy line of sight, ground checks, camera collision and tapping objects in 3D.
- Use layers or filters so rays ignore the shooter and decorations, and cap the ray length.
Like a laser pointer that reports back, a raycast sends an invisible straight line from a starting point in a chosen direction and tells the game what it hit first, where, and how far away. Games fire dozens of them every frame to answer questions about the world.
Point a flashlight across a dark room and the beam stops on the first thing in its way: a chair, not the wall behind it. A raycast is that beam, minus the light, plus a note saying "chair, 2.3 meters".
What you give it and what you get back
You give a ray an origin, a direction, a maximum length, and usually a filter saying which kinds of objects count. It returns either nothing, or a hit with: the object, the exact point of contact, the distance, and the surface normal (which way the surface faces). The normal is what lets a bullet decal sit flat on a wall or a ricochet bounce at the right angle.
Jobs raycasts do in almost every game
- Hitscan weapons. Lasers and most fast guns do not fly a bullet; they cast a ray from the barrel and damage whatever it hits that frame.
- Line of sight. An enemy sees the player only if a ray from its eyes to the player hits the player first, not a wall. Stealth games run this several times a second per guard (line of sight and stealth).
- Ground checks. A short ray (or two, from the left and right feet) straight down tells a character whether it is standing, and the normal tells how steep the slope is.
- Camera collision. A third-person camera casts from the player to where the camera wants to be and moves in if a wall is in the way (third-person camera).
- Tapping in 3D. A tap on the screen becomes a ray from the camera into the scene, which is how you select a unit or a tile.
- Grapples and lasers. The grappling hook attaches where the ray lands.
Pitfalls
- 01Hitting yourself. A ray starting inside the shooter's own collider reports the shooter. Start it just outside, or filter the shooter out.
- 02Hitting decorations. Grass, particles and invisible triggers block shots unless rays use a layer mask that includes only solid things.
- 03Too many, too often. Ten guards casting at 60 Hz is 600 rays a second. Checking sight 5 to 10 times a second is usually enough and nobody notices.
- 04Thin rays, fat targets. A ray is infinitely thin and can slip between two crates. For bullets that should feel generous, use a sphere or shape cast, which sweeps a small ball along the line.
Raycasts when building with Startbase
The agent reaches for raycasts on its own for aiming, sight lines and ground checks. In 3D games, Rapier physics can do the casting when it is switched on. Your job is to describe the rule in plain words ("guards see 12 meters in a 90 degree cone, walls block sight, tall grass does not"), because that sentence turns directly into ray length, angle and filter.
Guards see the player within 12 meters inside a 90 degree cone. Check sight with a raycast 8 times a second from eye height; walls and closed doors block it, tall grass and glass do not. Add a debug toggle that draws each guard's sight rays, red when they hit the player.
Common confusions
- Raycasting vs ray tracing. Ray tracing is a rendering technique that simulates light for graphics. Gameplay raycasts just query the world.
- Raycasting vs old-school raycaster games. Early first-person games drew 3D-looking walls by casting one ray per screen column. Same idea, different job.
- Raycast vs collision. Collisions notice touching; rays look ahead. See collision detection.
Frequently asked questions
What is a raycast used for in games?
Mostly aiming instant weapons, checking whether enemies can see the player, testing whether a character is on the ground, keeping cameras out of walls, and picking objects by tap or click.
Are raycasts expensive?
One is cheap. Hundreds every frame against complex scenes add up. Lower how often you check, cap ray length, and filter which objects are tested.

