Hello, boys and girls.
And in this video, we're going to check collision and surroundings around the player.
So at the moment, you can see if I start my game, I can jump and I can jump more and more and more
and more and more.
And then basically I can fly.
But we want to be able to jump only if player is on the ground, how we do that.
We just need to make sure player is on the ground.
And for that, we're going to go to the player script and create something here.
We're going to create a ground check.
Let's go somewhere below and type private.
Ball is grounded like that.
Now we have that.
Let's go to update and type is grounded equals two x to DD dot recast.
And now I have my internal suggestion here, but it's not very correct if I put my mouse on the recast
or I can see what I need.
So first I need origin from where I'm shooting my recast and for that I'm going to type transform the
position.
Transform position is the position of the player exact position he has.
So it will be basically shot from the middle of the player.
Then we need direction and because we want to check what's below the feet of the player, we're going
to do vector to dot down.
Then we need distance and we did not create that.
So let's go over here and create public float ground, check distance.
Very cool.
Now we put it here.
Ground check, distance.
And we can see we need a layer mask because if we don't use layer mask recast, going to detect everything
he finds, including the player.
So let's go over here and create public layer mask.
What is ground?
Now we type here, what is ground and now we have boolean.
That is true only if recast from position of the player that goes down in a distance of ground.
Check in the layer mask.
What is ground detects something in there.
Then we go over here to jump button and we can type and is grounded.
Now let's go back to Unity.
First, we need to go to layer and layer and add layer ground.
Now let's select the platform and choose layer round.
Cool.
Now we can go to player itself.
Find what is ground and click ground ground shake distance.
Let me try to put two.
So it works.
I can jump only if I'm on the ground, but it's not very accurate because I cannot see how long is ground
distance.
I need to see it.
For that we're going to create another function back in the code.
Let's just type on draw gizmos and inside we're going to do gizmos dot draw line and we say from where
to where from transform position to and is going to be a bit tricky new vector two on the x it should
be the same.
So transform position and on the Y it should be transformed position minus ground distance weight sorry,
transform position dot x and here transform position dot y.
Now let's go back to Unity and over here we can see our line gizmos.
Now, if I change my ground check distance, it will change its length so I can fix it as I like.
And I want to make something like.
This.
This is.
Okay, so maybe 1.4.
I'm sure this will work properly.
It does.
Now, I don't like my jump.
It's kind of floaty and we're going to fix it quickly.
We probably going to balance this later again.
But for now, let's just change gravity scale to four and see this.
Now his jump is better.
Maybe gravity skill to three and jump force to 12.
Yeah, This feels like how it feels.
And we cannot do endless jump anymore.
Perfect.
Now we done here.
Let's do a bit of cleanup after work.
It's going to be very simple.
First, I want to create header and let's put square brackets and type header.
Then in the parentheses we need to give a name for the header and it's going to be something like collision
info.
This will make it look better.
In the inspector.
If we go back to Unity, we can see it.
Can we?
We cannot.
We cannot see it because first variable after header is private and to make it feasible, it should
be public or serialized.
So let's just move this below and save it.
Good.
Now we can see collision info and I probably misspelled that, but it's arranged a bit better.
We don't need to see this boolean anymore so we can go and type private and the run began.
Move it below.
I can move this rigid body above and I'll make Heather move in for.
Right.
It looks much cleaner, much more understandable.
Let's go back to code and change something else here.
Doesn't make a big difference, but will help us in the future.
Let's select this line of code.
Click out.
Enter Extract Method.
Check lesion, and I want to be sure I did not misspell that at both places.
So let me do this lesion.
I'll just copy it from here.
Yeah, like that.
So this one is correct, I suppose.
Yeah.
And I copy this and paste it in here.
Okay.
Now we have update that is clean.
Just a couple of functions doing stuff, and we have it clean here.
Cool.
We can move to next video.