Unity : Raycast for check Distance

Matteo Lo Piccolo
4 min readJan 11, 2022

In the previous articles we have seen how use Raycast to check the collisions.
Today we see how use Raycast to check the distance.
In code this not change too much and now we see how it works.

This is my scene :
I create a Floor (with a Cube with this scale (10, 1, 10)
I create a Sphere at this position (0, 4, 0)
and I create a Sphere script.

In Update, we create a simple Raycast check

Inside Physics.Raycast we pass our position (sphere position), the direction (from ourself towards the floor) and we pass the distance, so the Ray goes down from us only 1.0f.

Now, before we move on, we talk one second about a new method called OnDrawGizmos

Reference to documentation here :
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html

This method is a part of Monobehaviour like Start() , Update(), etc.
It’s useful for draw things, as we see in the example in the documentation.
In our case, if we use OnDrawGizmos and we write
Gizmos.color = Color.blue
and then we write another piece of code,
Gizmos.DrawRay(),
we can literally draw our Raycast.

Reference to Gizmos here :
https://docs.unity3d.com/ScriptReference/Gizmos.html

Gizmos.drawRay here :
https://docs.unity3d.com/ScriptReference/Gizmos.DrawRay.html

Now, if we simply go back in Unity WITHOUT press Play

We see that the Ray is draw in our Scene (and in the Game) and starts from the sphere and go towards the ground for 2.0f.

If we change the length in code it changes in Unity

Now, how can we use this informations?
On a practical level, OnDrawGizmos and Gizmo.DrawLine or DrawRay or whatever we need are only for visual aspect.
Are really useful, but they don’t do nothing, we use it only because sometimes we need to see what happen in our scene.
But how can we use Raycast to being useful for calculate the distance?

A simple approach, in this case, can be :
we have this Raycast that start from the sphere on go towards the ground.
And in our game, that sphere have to falling down, but if the distance from the ground is less than 1, the sphere stop falling.

Ok, so for now we have this part, with the reference for the Rigidbody and we start cast a Ray from our position, the direction is Vector3.down and the distance is 1f.

After that we simply insert our logic: we have to zero gravity and make the Rigidbody kinematic so that no force can be applied to it.

If we test it

The sphere stop moving at 1f distance from the floor.

Of course, feel free to do other tests, such as changing the distance or giving the sphere a different behavior once that point is reached.

The same code can be write with RaycastHit

We have to remember to create a tag for the Floor or this code doesn’t work.

If we test it we have same result.

Last tips : we use Gizmos.DrawRay inside OnDrawGizmos, we can achieve same result if we use DebugDrawRay(), and we can delete OnDrawGizmos

Just remember that OnDrawGizmos works even if we are not in Play while Debug.DrawRay works ONLY if we are in Play mode.

After we press Play we see the line of the Raycast

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!