Working with Directional Raycast
Challenge: Applying a Raycast in an Object to check collision
So far, we have worked with Raycast within our Mouse input and using the Camera screen as our target. But what happens if we want to apply our raycast on other objects? Such examples are used within Racing Games where the AI opponents car is constantly using raycast to scan for collision and other AI cars to keep a set distance. Or in another game such as FPS where there are air drops of rations or other useful packages from a falling parachute, that package has a constant raycast scanning downward to check the ground.
In this challenge we will apply that game logic by setting up a raycast on a sphere, and when the sphere makes a collision via the raycast, it will change the material color of the sphere.
Prepping the Scene:
First we are going to create a sphere and apply a material with a color. Then we are going to create a floor using a stretched out cube. (I also gave the floor a tag called “Floor”)
Next we are going to apply a Rigidbody component to the sphere, this will allow the sphere to have physics and be able to control gravity.
After the rigidbody, we can create a new C# script and call it “Sphere_Drop” and apply that script to the sphere.
In the new script we are first going to get access to the rigidbody component that we added on the sphere.
We can do the Serialized Field method or we can do the Get component method, both are valid but just remember to slot in the rigidbody component back in Unity. In the start method I am also going to set the rigidbody is Kinematic to true. The reason behind this is because I want to be able to control when the sphere falls. Therefore at the start of the game, the spheres are NOT moving.
Next I am going to use an new method called “Start Ball Drop”. Inside this method I am going to trigger the swapping of the rigidbody status. So once I press the space key, the sphere is going to gain gravity and start falling.
Since we are using physics it is best to use “Fixed Update” instead of the regular Update. In the Fixed Update method, I am going to drop in my “Start Ball Drop” method. I am also going to have a Raycast Hit called hitInfo to store information of what I hit. Finally I am going to do a visualization for the raycast so I can see them during game runtime. I accomplish this by doing a Debug Draw Ray.
Finally we are going to cast our ray, we can use transform position because the script is directly attached to the sphere. Then we just need a direction which is the Vector3 down, with and out for the hitInfo. The float at the end is the distance I am scanning. If I am scanning for a longer distance I can up this float value.
Finally I add another IF statement for a quick compare Tag function to check for the floor tag. Only when the sphere hits the Floor will it then change color, and also revert back from gravity to is Kinematic.
The End Result:
The End result is that whenever I press the space key, the spheres will drop. Since we have a visual for the raycast due to the draw ray debug, we can accurately see when the raycast is going to hit the floor.
If we serialized the float variable for the raycast distance we can manually set different distance for each sphere.