Topics / Building a game

Physics

Raycast

A raycast shoots an invisible line from a point in a direction and reports the first thing it hits. It is how a game asks 'what is in front of me?'

Watch: see it in 30 seconds

Video coming soon
A 30-second visual explanation

Why it matters for your game

  • Line of sight, clicking on something in a 3D scene, checking whether a character is standing on the ground: all raycasts.
  • In a village game, clicking a villager is a raycast from the camera through the mouse position into the scene.
  • Raycasts are cheap one at a time and expensive by the thousand; ask before adding one to every object every frame.

How it comes up when you talk to your AI

  • 'Cast a ray', 'hit', 'layer mask' (which things the ray can hit), 'ray from camera through the mouse'.
  • If clicks land on the wrong object, the ray is hitting something invisible in front (a collider that is too big, or a UI layer).

Ask your AI about it

Paste this into the assistant you build with. It is written to get a useful answer for your own project, not a textbook page.

Explain raycasting in games with two examples: picking an object with the mouse, and checking line of sight between two characters. Show the code for mouse picking in the engine we use.

Related terms