Unity Raycast Tutorial: Unity 3D and How to Use it for Games

unityraycastMost modern video games utilize ray casting. Ray Casting is forming a line or vector from a specific point to another point in a 3D plane. The purpose of the ray (vector) is to determine if it intersects with any colliders or other game objects. This function is most commonly used in first-person shooter games, where the main camera (the character) is the ray’s origin (starting point) and the ray is cast to interact with other objects in the game that may come in contact with the ray. It is important to understand a vector (ray) and the many functions it can perform, such as shooting in a first-person shooting game, or picking up an item within the game, but first it is important to understand how to create a ray cast.

Looking for a way to learn more about raycasting? Take a course that will teach you more about working with the Unity program in game design.

In order to create a ray cast in the game world, you must first create a new script, which is where you will type all the information for the ray cast to be created. Obviously, there must also first be objects within the game world, as they are the game objects that the ray cast may intersect or interact with. To create a command in the script, there is a basic setup of required information to generate the command.

There are five parameters to the structure of a command that include;

·         Origin: The starting point of the ray in world coordinates.

·         Direction: The direction of the ray.

·         Distance: The length of the ray

·         Layer Mask: Used to select objects to ignore when casting a ray

All of these parameters can be used in a command and also, only some of them may be used. It really depends on what you want the ray and objects to accomplish once they intersect.  After creating a script, and naming it (maybe by the function that will be performed by the ray cast), you must open the script, which is, by default, powered by JavaScript in Unity. The next step is to decide where the origin point of the ray will be, which in most games, consists of the main camera view. This is essentially, the main camera’s view of the game, or the way the person playing is looking at the game. To set the command for the origin point of the main camera view, you must create a variable for the ray. This is done as follows:

Var ray = Camera.main.ScreenPointToRay

The next step is to set the direction that the ray will go, which for first-person shooter games, is usually the position of the mouse. To direct the ray to follow the cursor the following must be added:

                                (Input. mousePosition)

For a command that looks like this:

Var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

All of this information should be added to the function update section of the script, in order for this command to continuously be updated within the game. The next step is to create a debug that will make the ray visible in the editor. This allows you to view what objects the ray will intersect with and the actual line that will connect them. This is done as follows:

Debug.DrawRay (ray.origin, ray.direction * 10, Color.cyan);

To break these commands down; the debug.DrawRay is the command that makes the line show up on the screen, the ray origin and direction pertain to the previously stated commands about the ray variable; the *10 pertains to the distance of the ray, and then the color is the last element.

Looking for a more in-depth tutorial? Try a course that teaches you to work through the Unity program, including more advanced help with raycasting.

The next step in creating a ray cast involves adding another variable. This variable will show that once the ray intersects with the game object, a hit occurs. This is written as follows:

Var hit : RaycastHit;

Then an “if” statement must be generated to determine what will happen once this hit occurs. This command can be whatever you like, however for this example the command will simply explain what is in the path of the ray.

If (Physics.Raycast (ray, hit)) {

                                Print (“I am looking at” + hit.transform.name);

                    } else {

                                Print (“I am looking at nothing!”);

Within the Unity platform is a console, which is where the information that is produced from these commands will show up. This command will show tell the object that the ray intersects unless there is nothing there; then it will return with “I’m looking at nothing!”.

New to game design? Unsure of how to apply all of this information? Get started on the right path to creating your best game.

Sometimes when developing a game, there will be objects that you may not want to be effected way to accomplish this, is by selecting the object in the Hierarchy window and then looking to the Inspector section select layer and then Ignore Raycast. This will let the game know that this object will be ignored by the generated effects of the raycast intersecting with the object selected. Much like the need to exclude some objects, there may be a need to send off an array of effects to the game objects. To send a barrage of effects the following command will be inputted:

Hit = Physics.RaycastAll (ray);

Then, as with most games, you may want to determine that the ray will be cast when the mouse button is pushed and not merely when it is placed over objects. To do this, the following “if” statement must be inputted:

If (Input.GetMouseButtonDown (0)) {

                    If (hit.length > 0) {

                                For (var I : int = 0; I < hit.Length; i++) {

                                Print (“I’m looking at “ + hit[i].transform.name);

The zero stands for the left mouse button by default in the Unity program. The second “if” statement defines that the length should be greater than zero for anything to be effected. While the remaining parts of the command explain what should be done with the information, once obtained.

Another unique function that can be very beneficial in creating a video game is measuring the distance. To do this, you must go back to the beginning when the variables were created. The following script will place a new variable to look for:

Var rayDistance: float;

This is placed before the function update to create a new variable, and then you must add some information to the “if” statement that was created for the “hit” variable and would look as follows:

rayDistance = hit.distance;

        print (“I’m looking at “ + hit.transform.name + “, “ + rayDistance + “ units away” );

This script will return, in the console, the distance between the raycast origin and the object it is intersecting with.

While this process may seem very extensive and confusing. When faced with the Unity program, there are many hints and suggestions the program can give, to assist the developer in deciding what they would like to make the objects do. There are many more commands that can be scripted, and while you may never remember them all, there is a catalogue that can reference all of the available commands that are built into the program. Once you have begun to utilize the features of the raycast in Unity, the various results could be endless.

Once you are already familiar with raycasting and Unity, take it to the next level with 10 killer tips and tricks for Unity.