There are many ways to do a raycast in Unity. For this example, we'll use the overload that takes an Origin, a Direction, and a Max Distance and returns a Hit Info:


The node looks like this:


As you can see, unlike usual nodes, we have two outputs: a boolean, indicating whether the raycast hit something, and a hit info, giving us much more information about the hit.

You can use the inputs you want to get your raycast, but let's focus on how to get the values out of the hit info.

You can use an Expose node to decompose any type. In this case, we want Expose Raycast Hit:


You can then connect that node to the hit info port to expose it:


Here, for example, we have access to the Point that was hit, the Distance the ray traveled before hitting, and the collider / rigidbody / transform of the object that it hit, among many other properties.

You can use these to react to the raycast accordingly. For example, this graph does a raycast in front of the current object when we left click, and if it hits an object, it spawns an explosion particle system at the impact point and destroys the impacted object:



This article was helpful for 5 people. Is this article helpful for you?