Unity Raycast : How and When to Use

3 mn read

Now you may be wondering, “What is Raycasting in the first place?” If you already know the definition of the term, how about a quick revision. It is the development that takes place in order to shoot an invisible ray from one point to another, the direction is specific and a predetermined function whose role is to detect any upcoming article from the path of shooting. This process is popularly found in shooting games; first person shooter, third party shooters, bullet hell.

This process is also found to be useful in adventure games, it allows the fired bullets or elements to be traced from point A (the starting) to point B (the ending) So the player knows and can predict how to manoeuvre away from getting shot or injured. Sometimes the behavior of trace elements or bullets get difficult to do so according to the game developer or a gradual shift in level difficulty.

On a more magnified scale, Unity Raycast is used in two dimensions of gameplay, 2D and 3D. Since the physical layout of a 2D is not the same as seen in a 3D layout, clearly there are distinct ways to produce Raycast in both of their settings.

Now, since 2D mode is easily converted through the 3D one let us start the project with a 3D mode on.

Create A Total Of 5 Folders

To begin with this fun project, make a total of five folders under “Assets” folder

·        Models – create additional two folders under “Models” name them “2D Art”  and “3D Art”               

·        Scenes

·        Scripts

Now, it is time to transfer the art portions to the respective folders made. Then you will need to create two scenes and save them individually, these two scenes should be named “2D” and “3D” respectively. Now, click on the “2D” scene and start with raycasting in 2D

Unity Raycast in 2D Mode

The first step is to remove the directional light, click on the 2D tab that is displayed on the screen and select on the camera angle. Change it from “Perspective” to “Orthographic” Now, let’s move on to

● The Tank Spritesheet, here you should find the “texture type” tab through which you shift the settings to “Sprite” which is 2D and UI. Make sure even the “Sprite Mode” is shifted to the “Multiple” Setting. To finalise the change in settings click on the Apply button featured at the bottom.


● Next, to divide the Sprite sheet into singular Sprite objects you should select the Sprite Editor. This tab allows the user to make modifications to the already default sprite sheet. There are certain settings for this to work though, to do so


● Select the “Slice” tab and a display view opens up. This has all the required settings to be made so first off, the Slice Type should be set to “Automatic” and then Click on Slice in the menu. To finalise the entire changed setting click on the Apply button

Some Common Examples

·        For Static Image

In this case, we are taken to the standard UI of Unity3D and notice that the Tanks Sprite Sheet has a high number of single sprites present in the sub Sprite. This is again found to be placed under tanks_spritesheetRetina. Now to begin with the process, you should use the tanks_spritesheetRetina_0 instead.

Then you will need to select and drag the Sprite you wish to view on the screen, the details of the position should be mentioned too so set it under -7.91 for X-axis and 0.75 for Y-axis. Click on “Add Component” and the selected component will be a “Line Renderer” and this is found under “Effects”

Now we can start to make a script named “TwoDRayCasting” which will be attracted to the tanks object. A quick note, the Line Renderer has to have a width of 0.25. Now for the aspect of coding down our next steps visit this link for Best unity game development and coding process.

Unity Raycast in 3D Mode

Finally, performing the part of 3D Raycasting is not an easy task but through following the steps anyone can do it. To set things into perspective, the steps soon mentioned will have elements of building a side scroller platform game. There will be mechanisms on shooting and first person shooting. Now to start, two scripts should be created for the side scroller and the first person shooter mode. This means attaching game objects.

● now, create a side scroll scene named 3DsideScroll. Then set the Main Camera into Perspective mode and the position should be under (0,0.33,-3.38) The other mode of Camera Angle is also possible.

● It’s time to import the prefab previously created in default, the position should be (-3.26,-1.15,0) along with a rotation (0,75,0)

● now include the ThreeDRayCastingSideScrollMode scrip with the character.

● next, add the Line Renderer to the character with the box under it left unchecked. The width of the Lime Renderer should be 0.05 for this practice.

The rest is up for you to practice and have so,e fun with the position ranges.



Enjoy The
Magic Of Words From The Authors

A wonderful serenity has taken possession of my entire soul.
I am alone, and feel the charm of existence in this spot!

Discover TrendyRead

Welcome to TrendyRead, an author and reader focussed destination.
A place where words matter. Discover without further ado our countless stories.

Build great relations

Explore all the content from TrendyRead community network. Forums, Groups, Members, Posts, Social Wall and many more. You can never get tired of it!

Become a member

Get unlimited access to the best articles on TrendyRead and support our  lovely authors with your shares and likes.