Unity : use Raycast for move the Player

Matteo Lo Piccolo
4 min readJan 12, 2022

Today we see another useful method when to use Raycast.

We create a simple scene with two Cubes, one withscale 10, 1, 10 that we use as the Floor.

Then we create two scripts, one for “Player” and one for “Pointer”.

We attach Player to the little Cube and Pointer to the Camera.

Now, inside the Pointer class, we manage the Input.
So we take the reference to the Player and we use “Find” in Start and make a check for null reference.

In Update we use the Mouse Input.
Now, I use the old Input System in this case, but feel free to use the new Input System, nothing change.

The first part is

If you use the new Input System, the line is

Don’t worry about my error, it’s because I don’t have the Input System Package install in this project

Now we create a Raycast and a RaycastHit

With new Input System, the line is

And at the end we make a check if we hit something

We stop here for now, and let’s see what happen inside Player script.
Inside here, we have the “logic” to check and calculate the distances and at the end we have a method for Update our position.

First we need a variable to store our destination.
Then we calculate the distance between our position and the destination.

Now, this is juat a test, so I use a simple If Statement to check :
if the distance is greater than 1.0f, move the Player

What happen inside here is simply this :
we create a variable called distance to calculate the distance form the Cube and the point where we hit the Floor with the Raycast, then we check if we click at some distance greater then 1.0f from our position : if yes, move the Cube.

NOTE : why we use direction.Normalize()?
Because after we calculate the distance, the Vector that return can be much bigger then 1, and when we multiply the direction * an hypotetical speed, in this case 2.0f, we want the speed be 2.
If we not normalize the movement it is much faster than what we want.

The last piece of this test is to have a method, UpdateDestination()

Here we simply set targetDestination to the parameter pos, and we “call” this method inside our Pointer class

And we pass as parameter the hot.point, which is the point when Raycast hit the surface.

Play it

as we can see it works, but we have a little problem with the y axis.
To fix it, we simply need to block the y in the UpdateDestination()

Play it again

And we have everything set up.

Just for fun, we can comment out the Normalize() and see what happen

As we can see, the movement is so much faster if we not use Normalize() for the vector.

With this article we finish the part on Raycast and Raycasting.
As we can see is really useful in many situations, so feel free to use it and test it whatever you want, I’m sure is a good implementation for your games!

See you in the next articles!

--

--

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!