Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
What is Raycasting?
Concept of Raycasting
Properties of Raycasting
Using Unity for Raycasting 
Single Raycast  
Persistent Raycast 
Frequently Asked Questions
Name one application of Raycasting.
Is Raycasting the same as ray tracing?
What field other than video games is Raycasting extensively useful?
Last Updated: Mar 27, 2024


Author Vidhi Singh
0 upvote
Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


We all remember playing video games in our childhood. We all used to get fascinated with the 3D feel these games used to give us. We kind of used to feel to be a part of the game and in the same environment. 

Here, in this article, we will explain a mechanism that helps in rendering the 3D objects and environment in a 2D map, called Raycasting. 

What is Raycasting?

Raycasting is the mapping of a 3D object in a 2D map. It is this technique that helps in creating a 2D perspective of a 3D material. 




Through Reaycasting, a 3D image can be generated even from a small amount of data with the aid of tracing rays from the viewpoint into the viewing volume. 
Raycasting makes the process of 3D modeling and rendering easier and more efficient as a lot of calculations, and visibility logic had to be put into the process.

The whole mechanism has been explained in the next section of this article. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Concept of Raycasting

The basic principle behind raycasting is that the light rays can be cast and traced in bundles depending on specific geometric restrictions.

The process is similar to ray tracing in many aspects, yet it is different from it.  

Let’s see how it actually works.

  • Raycasting is considered very fast as only one calculation has to be done for every vertical line. 
  • Raycasting essentially transforms a line into a line only when rendering 3D objects and scenes; this is what makes it relatively less complex. 
  • Every point of the object is represented in the form : [X, Y, Z, 1] along with a direction vector :  [Dx, Dy, Dz, 0]. The fourth term is zero(0), as it is used for translation and not reflection.
  • Now, geometric rays are traced from the eye of the person( or observer), which is analogous to the sampling ray from the sight of a person to the object. It is somehow based on ray tracing. This same concept is used in optics when explaining how we can see different things. The shading is then computed using the traditional shading model. 
  • This property makes rendering all kinds of non-planar surfaces easier and solids, like a sphere, cone, etc. In short, if a ray can intersect a surface, it can be rendered with raycasting. 
  • These rays from the eye of the person being traced, one per pixel, stop when blocked by an object in the path. When these rays get blocked, the object is what is seen by the observer. 


Here, some assumptions are made. One of them is that an image is a screen, and each square in the screen is a pixel, and the other is that the geometric ray will definitely get blocked by the object.

All the geometry involved in this process is based on light rays and the camera geometry. Let’s take the pinhole camera for understanding this. 

  • The simple pinhole camera model includes an observer point(or focal point) and a screen, basically, an array of pixels. Straight light rays pass through the screen to join the focal point with the scene with one ray for each pixel. 
  • The rays' intensities are responsible for the shade of the picture. 
  • The view is called " parallel " when the focal length is considerable, almost like infinite; the view is called “parallel” because all light rays are parallel and perpendicular to the screen. 
  • Although the perspective view is natural for making pictures, some applications need rays that can be uniformly distributed in space. 





Properties of Raycasting

Properties of Raycasting include:

  1. Raycasting is a highly efficient and fast process.
  2. The speed and simplicity of ray casting are because for computing the colour of the light, other rays that hit the surface are not recursively traced.
  3. This also brings a drawback with it; reflections, refractions, and natural falloff of the shadows of the objects cannot be rendered accurately. But they can easily be faked till an extent with the creative application of creative maps and other methods.  

Using Unity for Raycasting 

We have discussed enough of theory about Raycasting. Let’s get our hands dirty with some practical concepts 

So basically, we perform raycasting, when we want to place a virtual object in a real world scenario.  We “shoot” a ray from the point of the finger tap into the perceived virtual world.
The raycast then informs us if and where this ray intersects with a plane or point cloud, which is called a trackable. 

AR Foundation is unlike a traditional raycast, which only considers objects in its own physics system. AR foundation has its own variant of raycasts. They support two modes:

Single Raycast  

It is also called as one-time raycast. It is precisely of use when we have to react to a specific event of user to see if the user touched on a plane to place a new object.

Let’s see the method that takes an arbitrary ray involving a position and a direction.

public bool Raycast
    Ray r1,
    List<ARRaycastHit> hit_results,
    TrackableType trackableTypes = TrackableType.All


In case, the raycast hits something, hit_results will get populated with a List of ARRaycastHit. 
To see what kind of things has the araycast hit, hitType is used. 

For example, to check if it has hit a plane, following property is used: 

void HandleRayCast(ARRaycastHit hits)
    if ((hits.hitType & TrackableType.Planes) != 0)    //Checking if it is a plane
     	var plane = m_PlaneManager.GetPlane(hit.trackableId);    //Checking up the plane by ID
     	Debug.Log($"/It has hit a plane. Alignment: {plane.alignment}");   //Printing
     	Debug.Log($"It has hit a {hit.hitType}");     //Some other thing is hit

Persistent Raycast 

Persistent Raycast is similar to performing the same raycast in every frame. A Persistent Raycast is trackable through the ARRaycastManager. 
Persistent Raycast should be formed with a screen point like this:

public ARRaycast AddRaycast(Vector2 screen_point, float approx_dist)

Frequently Asked Questions

Name one application of Raycasting.

It is used in real-time 3D video games.

Is Raycasting the same as ray tracing?

No, both are two different things. Although, raycasting uses the concept of ray tracing. 

What field other than video games is Raycasting extensively useful?

It is extensively used to create real-life effects in Augmented Reality.


This article extensively discusses the concept of Raycasting. We also explain the principle behind it and its advantages. 

We hope that this blog has helped you enhance your knowledge regarding Raycasting, and if you would like to learn more and see how to build a game in AR using raycasting, check out our article on Snake Snack Game

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSQLSystem Design, and many more! If you want to test your competency in coding, you may check out the Mock Test Series and participate in the Contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the Problems, Interview Experiences, and Interview Bundle for placement preparations.

Nevertheless, you may consider our Courses to give your career an edge over others!

Do upvote our blog to help other ninjas grow. 

Happy Coding!    

Previous article
Next article
Lighting and Realism
Live masterclass