From the course: Advanced Unity: 3D Game Programming

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Ray casting

Ray casting - Unity Tutorial

From the course: Advanced Unity: 3D Game Programming

Start my 1-month free trial

Ray casting

In this video, I'm going to go into more detail on Raycasting. We've touched on the subject with mouse input. So far, we've just been casting rays from the camera into the scene. However, it's just as easy to cast a ray from any position in the scene. Raycasting is used for lots of things in games, including projectiles, path finding, steering, visibility testing, and so on. Unity does provide some pretty robust raycasting with their physics system. However, this does require that objects have physics components, in this case, any kind of bounding volume, like a box collider. Raycasting has a few input parameters that allows to control the behavior of the cast as well. For example, we can define a maximum length and a layer mask. The maximum length is fairly self explanatory, where any hits beyond the maximum distance are ignored. The layer mask is slightly more tricky. It's a bit mask that allows us to filter what type of objects we want to interact with. I've created a sample…

Contents