Join Michael House for an in-depth discussion in this video Ray casting, part of Advanced Unity 3D Game Programming.
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 project that should make this a bit easier to understand. On the left here we have the list of layers that are defined in Unity, with the layers option in the top right.
Here we can edit layers or change layers to be visible or not. For this scene, I've added three layers, Layer1, Layer2, and Layer3. We also have three layers of walls, with one wall being both Layer1, and two wall being Layer2 and three wall being Layer3. Here, I'm casting a ray from this cube along this forward vector. Typically rays are invisible, but I'm drawing the ray using some visual debugging from an early video, just to make it easier to see what's happening.
This ray currently has a bitmask of zero. Means it's not going to hit anything, making it a pretty uninteresting ray. Let's look at updating the layer mask. A combo box list here on the left is something we'll create in a later video, I'm using it here because it makes it easy to add or remove layers from our mask. As we check items in the list, our layer mask is updated, as well as our ray. We can see that updating it to include Layer1, means that it will now interact with Layer1 objects, which both of these one walls are set to Layer1.
And see that it goes straight through Layer2 and Layer3. We of course can add more than one layer, and interact with all three layers of the scene. We can easily visualize this layer mask with the camera. If we take a look at our camera, we see that it has a calling mask applied, and we can actually uncheck certain layers, and remove them from the scene. So when a ray has its layer mask set to three, this is essentially how it sees the scene. Of course we can also remove layers from the scene and still interact with them here.
Let's take a look at how this layer mask is updated. In Unity there is no built in way to get a list of all the layers, so I've created a get layers method here. That will go through and create a dictionary of the layer names along with their layer value. The Raycaster is going to get a list of all those layers and add them to our Combo Box so that when we select an item, we can or our current layer mask with the value of the layer mask selected. The layer value stored is the placement of the bit for that layer mask.
So we need to create a new integer by shifting one over at that value, and or it with our layer mask. So for example the default layer mask is zero. So by adding it to our layer mask we are going to take one and bit-shift it by zero, which means it is going to stay here at the first position. Transparentfx is going to have a value of one, so we're going to take a one, we're going to shift it into our layer mask by one, resulting in this one here. So how about hitting multiple objects, by default the array will stop at the first layer that it interacts with.
However there is an option to cast array. To collect all of the hits that array interacts with. We can see here, created a method for that. So we can now interact with multiple layers, and see all of the objects that have been hit. This might be useful if you're shooting a projectile that can go through walls. Further, one of the other uses I mentioned for Raycasting is path finding. And that might be to see if we can fit our object through a certain area. To do that, we might want to cast a sphere that's the size of the object, to see if it'll fit. Let's take a look at that.
Here we're going to cast a sphere, and we're going to see if that sphere will fit through a gap. Here we can see a cylinder acting at the edge. And there we see the ray is now cast through this gap, allowing the sphere to fit. It's a very tight fit. Additionally, just like the multiple hits for a single ray, there's also a multiple hit for the sphere ray. This might be useful if you're creating a very large projectile, and want to destroy multiple objects along it's path. In this video, I've showed you how to use the Raycasting functions in Unity.
We also discussed some uses for rays. Raycasting is a powerful tool when making decisions in your game. And with plenty of use cases, it's likely to make an appearance in your game. The Raycasting in Unity does use the physics system. So be sure to add a collider and layer information to any game object you'd like it to interact, with using a ray.
NOTE: This course requires Unity 4.5.5. The newer versions of Unity have done away with the GUI system used in this course, so the interfaces included for many of the scenes will not work with 4.6 and higher. You can download Unity 4.5.5 at http://unity3d.com/get-unity/download/archive.
- Enabling/disabling with scripts
- Translating, rotating, and scaling objects with scripts
- Working with mouse and keyboard input
- Creating custom GUI controls like progress bars
- Loading assets with scripts
- Saving games
- Creating prefabs dynamically
- Making remote procedure calls
- Synchronizing object transforms
- Finding slow code
- Optimizing data access
- Extending the editor