Welcome back.
In this video, you will learn how to use Rakes, which is a powerful tool which you can use on in many
different locations.
You can, for example, check if there is something in front of you behind you, on top of you, or
at the point where your mouse is and all of those kind of things.
And we will use that in two different games.
So let's go ahead and do it now so that you know how to use it later on.
What we need for that is our cube.
So let's bring our cube back into the game and at the same time, let's create a plane.
So 3D object and plane.
So now things look a bit different here.
As you can see, our angle is very weird.
So let's have a look at this.
Plane is within the cube, so let's change the rotation of the plane.
All right.
Now we're good to go.
Okay.
So our plane should be lower than our cube.
And at the same time, I'm going to give it a material.
So let's create a material real quick so that we can see it better under ground.
And let's change that to red.
So let's make the underground red.
So now we can see it in our game.
So the idea is to check if our cube currently has something below it.
So is there if there is anything that it collides with underneath it.
And in order to do that, we can give that cube a new script.
So I'm going to add a new script to it and check for objects.
I'm going to call it check for objects.
And what we need here is within our update method, we will need a red cast hit.
So it's going to request towards a specific direction and we check if there is a hit and if physics.
Thought recast and here are 16 different overloads and we'll use one of them, the one with transformed
position.
So that's the origin of our request.
Then we need the direction of the recast.
So in my case I want to look down.
So vector three up.
So minus vector three up means go down.
And then we want to have the point which we hit.
So output hit and finally we want to know how far we want to go.
So for example, we only want to check 100 units.
We don't want to go any further than that.
And if we hit something, then we can, for example, print, we hit something and we could even go
as far as telling the different distance to the object at plus hit dot distance.
Or maybe even hit that transform thought position so we can even say where we hit something.
All right, so let's save that script and go back to Unity.
So now our cube has this script.
So now let's go ahead and run it.
Let's have a look at the console, what it says.
And it says, We hit something at zero zero.
And now I'm going to move it a little bit and I'm going to clear everything.
Let's see, nothing is hit and we move just towards our on the ground and we see that the method is
called and it's called in the update method.
So it's called very often.
So our print method is called like very often in every single frame.
So now we know precisely when we hit something.
What we even could do is go as far as telling if we don't hit anything so else print nothing underneath
us.
So let's save that and try it again.
And now let's move our element back and forth, nothing underneath us.
And we hit something and they move back and forth.
All right.
So I'm still getting this error because I have something in the build that I'm not using anymore.
So that shouldn't appear for you.
Okay, so now you know how to use Red Cast Hit to check if something is underneath you or not.
Now let's have a look at how we can do the same thing for moving over with our mouse and seeing if we
hit something with our mouse.
So let's stop the game.
Let's go back to Visual Studio.
And now, instead of using recast hit, we will need to use Ray.
So let's comment that out and we'll use Ray now.
So Ray Ray is equal to camera dot main, that screen point to Ray.
And in here we use input before we always use get mouse down and so on.
But this time we use the mouse position, which is a vector three, which checks where is our mouse
and then calculates at which point in our game is it at.
And now we can check if physics dot recast with our ray and for example, the distance of 100 if there
is something and if there is something.
Print.
Hit and hit.
Uh, maybe something.
Hit something.
So let's have a look.
What happens now?
Let's go back into unity, and let's play around with our mouths and see if we hit something.
And we hit something, and I hit the bottom.
And so now I can see every time that I hit something or update method goes up for every single frame
that I hit something.
Now, if I want to know what I've hit, I can use rate cast hit and I'm just going to call it hit.
And in here I will need to use a different overload, the out hit overload.
So now I'll know what I will.
What I have hit and hit something I replace that with hit dot collider, which means the the collider
that I have hit.
And from that collider, I want to know the game object itself and maybe even the name of the game object.
So that will give me the name.
So let's say I hit.
The Cube, then it should say, Hit Cube.
If I hit our underground, our plane, then it should say plane.
So let's check that out.
Let's go somewhere with our mouse and let's walk over it.
Hit cube and hit plane.
As you can see now, we even know which object we have hit.
And that one will allow us to, for example, build buildings because we know where we currently are
with the mouse and we know where we are positioning a building.
That's something that we will do in the Fun World Clone chapter.
So you might want to come back to this video if you want to build the Farmville clone by yourself.
All right.
So see you in the next video where we will finally start off with the game itself.