Hello, guys.
In this video, we're going to make a large claim, and it's a bit complicated.
In my opinion.
It is hardest part of the course, but I'll walk you through it step by step, and you will know this
mechanic top to bottom.
So I'm sure it will be very simple to understand.
Don't worry about that.
So first let me delete all of the prototyping I did, and then I'm going to show it to you how we do
it step by step.
All right.
I think I'm ready.
So let's begin with simple.
Let's just create animation.
First, we're going to select the player animation, create a new clip, and it's going to be player
ledge climb.
All right, we have it now.
Let's go to graphics, fine player.
Let's climb and drag all of the sprites to the left.
Let's try sample rate 15, see how it works.
Um.
No.
Probably.
Let's see how it will be in the game.
If anything, we can fix it.
Now, let's go to animations, find animation.
Let's climb and check loop time over here, and we can go to animator setup animation.
We need the boolean for that so I can climb.
We're going to move it over here and there's going to be a bit different.
We're going to transfer to a ledge, climb from any state.
If can claim true and now can claim somewhere below, let's just drag it here to the top.
Now, if you add conditions, it will be at the top so I can climb through.
Let's go to settings.
Exit time and check duration is zero and make sure to check and transit to self.
It's very important.
Now we can make another transition to idle move and this will be exit time one duration zero and no
condition to exit.
That's it.
Basically, we're done for the animator.
Now we can forget about it for the time being.
And I want to explain a bit how it's going to work.
So we're going to do edge detection, but we're not going to do it from the player script like we did
before.
If you remember, we have this check collision function where we do in all detection we need.
But let's detection.
I want to do from another object.
I want to do it from the child object.
We have a reason to that and you'll know it in a minute.
Let's go all the way up and in the collision info I want to create public boolean.
Bull.
Ledge detect it, but I don't want to see it in the inspector.
I just need it public so I can access it from another script.
But I want to keep it hidden so it will be hiding the inspector over here.
All right, cool.
We can move it up here.
And now let's create ledge detection first.
I guess I changed my world check size.
Check If it's the same to yours.
-0.3 Now, you can fix it if you want.
Now we're going to create empty object.
I'm going to call it large check.
We're going to add component new script, ledge detection, and we can go inside of this script.
All right.
So what we need for let's check, we need to know position and for the position we're going to use.
Exactly.
The child object itself.
This one.
Right.
This is going to be our position for.
Let's check.
Then we need read use for check.
So I serialize field private float.
We can call it just radius and basically that's it.
So first I want to draw gizmos so we can visualize it on draw gizmos Gizmos to draw wire sphere from
transform position in a radius.
Yes.
Let's go and see this.
We're going to increase radios to something like point two, and I want to place it somewhere above.
Maybe right here, just like in front of the head of the player where?
Let's check.
Possibly it could be all right.
So I think this is fine.
Somewhere in front of the head.
Don't worry about precise placement.
It's not so important.
So now we have let's check position.
We know area we detected.
Now it's time to create.
Let's check itself and I don't want to create boolean here for edge detected.
I want to use boolean.
We just create it on the player which we made public.
So let's go back to edge detection, get access to the player.
Serialize field, Private player player.
Sorry.
And then in the update we're going to do player dot slash detected equals to physics to DX, and here
it's all the same.
You see, basically we do in what we did here check collision.
Very same stuff just from another script and our lab circle.
Right.
Over lap circle.
From transfer position.
In the radius.
Och now I want to go to player and just debug ledge, detect it, debug that log ledge, detect it.
We need to say what is clear so we can just drag it like this and we're going to go to play mode.
Oh, no, I forgot something important.
Guys.
We need a layer for this.
Right.
I hope you did notice before me.
Serialize field pry.
What layer mask?
What is ground?
Okay.
And we're going to do check radius.
And what is ground is a layer mask.
Now, let's go back and check this.
You're going to go to play mode.
We need to choose what is ground and better do it out of play mode.
So now we have console that says false.
If I drag my ground like this to the circle now it says true only.
And by the way, if your console looks like this, you can just click button collapse and it will collapse
the messages.
So now it is true.
If I move it out, let me make it smaller.
Right.
So now let's just detect it.
True.
If I move it out, it is false.
But what if we detect ledge like that when we face a long wall?
Watch this.
This is pretty long.
All right.
And we place it here.
It says true, but it is not the ledge.
It is just a wall.
So we have a way to control that.
How are we going to do that?
We're going to disable Let's check if there is a wall in front of us.
And if there is no wall in front or above and only below, then we're going to know it is a ledge.
How are we going to do that?
I'm going to show you right now.
And let's select, let's check, choose what is ground, because I didn't do it before.
And we're going to add component box Collider that we're going to make is trigger and we're going to
use on trigger enter.
If you never faced an trigger enter before, don't worry.
Later in the course, we're going to discuss it deeply.
I would say for now we're just going to use it.
You just follow me.
And it's not complicated, honestly.
So this is a collider we have here, and we can turn this collider into a trigger.
And when something enters the collider, we can make something happen.
So that is basically called on trigger enter.
Now we have this trigger, right?
I'm going to make it like this and I'm going to go to ledge detection back again.
Create a boolean private bool can detect.
Now let's go over here and create a function which is called on trigger Enter to DX over here.
Now, what we do here, we say if collision dot game object, dot layer equals equals to layer mask
dot.
Name to layer so we can get a layer by name and in the parentheses and quotation marks we should say
ground and please be sure it is named same as an layer in unity.
You can check it.
Over here.
If you go to layers over here, ground, right.
So name should be saved.
And let's go back and we do this on trigger enter.
If layer is ground, then can detect equals to false.
Now we're going to create another on trigger but exit on trigger exit to the.
And we're going to copy these two lines.
Paste it in here, but make it true.
Then we're going to update.
And we say if can detect, then we do this detection.
Pretty simple.
Now, let's try it again.
I will take my inspector and I'll turn it into debug mode.
So we can see, can detect boolean.
Now, look, if I take my ground.
And I'll make it something like this.
Move it in front of the character.
Now let me look the ledger check so we can see it.
Click.
Look over here.
Now check this out.
If I enter, the trigger from this side can detect turns to false.
You watch this one, right?
If I move it out, can detect equals to true.
If I make it long and place it like this again, can detect false.
But if it is a wall and we say.
Move it from below.
It does not switch off can detect boolean.
And actually you can see here that our alleged detection became true.
It is happening because of the collider.
We said before, if I go back to normal mode and click edit on my collider, you see how it is set.
So if we enter this trigger from the right top or left side and actually we do need left side, only
right side.
Yeah.
Anyway, we said can detect to false and we are no longer able to detect.
But if we move in platform from the bottom, where is it.
Yeah.
You can see that this ground platform entered the alleged detection before it entered the trigger.
So it is able to detect.
I hope that makes sense.
If it's still a bit confusing to you, please don't worry with the time, it will be super simple to
understand.
All right.
So as of now, let's just make sure our detection is working.
I'm going to clear it.
And if I place my role like this false if I go from the top false.
But if I go from the bottom.
True.
The true goes right.
If I place my role like this and I move my character, let's say, to the wall and then he is jumping,
it is false.
False, false, false, false, false, false.
And then boom, true.
Let's detect it.
Perfect.
Next we go into freeze the player at the position of let's check.
Let's delete this debug.
Go to the top and we're going to create another header.
Header Ledge info.
Now foliage in four info we need offset and I'm going to explain why in a minute.
For now, just repeat after me.
Try what vector to offset.
One and then serialize field private vector to offset to OC.
Now we need.
Another vector too, and it can be just private without serialize field, private vector to climb,
begun position and then private vector to climb over position.
Cool.
Now we need couple of booleans.
First one will be private bool can grab, which means can grab the ledge.
Maybe we should write it can grab ledge and then another boolean private bool can climb.
All right, we have all set.
Now let's go and create a function over here that's going to be named as private void.
Check for ledge.
Inside.
What we're going to do is to check if ledge detect it.
And you remember we said this pledge detected with alleged detection from the child over here.
So now if ledge detect it and.
Can grab lunch.
Then we're going to set can grab lunch to force.
So it happens only once.
False.
Then we need to assign position before climb and after climb.
Maybe.
Let's step aside and I'll explain to you why we need position before and after.
All right, Just for a second.
So check this out.
We have Let me find it platform.
So at the moment we detect in the ledge we're going to freeze position of the player at the place where
ledge was detected right here.
Then we're going to play an animation of a ledge climb from the first frame.
Just like this.
And at the first frame of this animation, we're going to assign position that we are about to create
position before a client.
And it should be somewhere here.
So it looks properly, right?
So this is our position before client.
Then we're going to play animation.
And when animation is over, we're going to move position of the character to position after client
and we're going to play idle animation and it's going to be just perfect.
So let's go back first.
Let's get position of the ledge check.
So we're going to do vector to ledge position equals to get component in children ledge detection transform
dot position.
Now we're going to do this climb began position equals to ledge position plus of set one, then climb
over.
Position equals to ledge position plus offset.
Now we're going to set can claim to true and we're going to create another if statement that says if
can claim true, then transform position equals to climb begun position.
So what are we doing here?
Basically, if let's detect it and we can grab the ledge, then we grab in the ledge.
We're saying we can no longer grab the ledge, we get in position of the ledge itself.
Then we set in position before climb began and we set in position after climb is over with the help
of the ledge position and the offset.
Then we're saying that we have position.
We can no longer grab the edge, but we can climb.
So we set it to true.
And then if we can climb, if this boolean is true, then we hold in position of the player at the climb
began position.
Now I only want to check if can grab ledge is true because we never set it to true at first place.
Okay, Over here.
We need to make it true by default.
If you don't write anything here, then default value will be false.
If you want to make it true by default, we need to do it.
Okay, cool.
Now let's go back and see if this working.
And I'm pretty sure it is.
I'm going to select my level part and just add another platform.
Okay.
Let's go to play mode.
Now, if I'll jump, we can see I stuck in the position of the ledge.
And this is perfect.
This is exactly what we need.
Now, let's do this.
Let's go to graphics.
Fine player.
Climb.
We're going to take first pride and drag it here.
Then we're going to go to Idol, Take first pride, drag it here and select both of them.
We're going to change certain layer so we can see it.
And now we need to define offset of the player how we can do it.
We can just take this two sprites, make them child of the player.
Then let's take the first one, drag it so we can see it.
And I cannot actually see.
So maybe I will switch off sprite of the player.
Okay.
He should be placed somewhere here.
This looks okay.
Now let's go to Notepad and we're going to type before climb X is minus point five.
We can see that we're here in the corner, minus point five, F and Y is -1.2.
So this is how he should be.
Before we climb.
Then let's take a second Sprite and define how he should be after climb.
We're going to place him somewhere here in a proper position.
Then we can see position here.
Point 57 and 1.3.
Let's go to Notepad.
After climb.
X 1.7 fy1 .33 f oc Cool.
Now let's uncheck play mode.
Go to player and write these two offsets minus.
Minus five.
Here.
-1.23.
Here, point 57 and here, 1.33.
And again, your values can be different from mine.
All right.
If you place the alleged check, just not where I did.
If your position is not the same, then your offset might be a bit different.
That's why we using this picture to define offset.
All right.
That's why we place it like that so we can see where he is going to be.
So you do not look at my numbers?
Of course you can check my numbers, but you do not check my numbers.
You go to play mode just like I did.
And you grab the ledge first, just like I did.
Sorry.
Already doing it with the offset?
Yeah.
No, Let me put zero zero here.
So you grab the ledge just like I did.
Then you go to graphics ledge, climb, find this sprite, make it certain layer player and place it
somewhere so you can see it.
Yeah.
Make sure it's a child of the player so we know the offset actually plays it how you want it to be.
Just like this.
Then check the numbers.
You see now, these numbers are different.
Not the same I wrote before, so you need to check it by yourself and then you write it here.
OC Okay, so what is it, minus four or five?
Eight?
Yeah.
It's the Y and this is minus four, five, eight.
Okay.
Now, I don't need this notepad anymore.
Close it.
Let's go back to code and set animator.
We need to find animation controllers.
And here we said Anim, said bull.
Can claim.
Two can claim.
Okay.
Save it.
Let's go.
We're about to be done, actually.
Och, this is good.
What happens here is transition between two animations over and over again.
If you go to animator, you can see it.
So to stop this, we just need to add one condition.
Let's go to this condition and add plus can climb should be false.
If you want the transfer to jump fall from any state can climb should be false.
Now let's go to play mode.
Cool, right?
Mine looks nice.
I hope yours as well.
Good.
Now, what we do next is we need to stop this animation, and we need to actually change position of
the player.
How to do that?
Very simple.
Let's go back to code.
Maybe right above the Czech village, we're going to create private void.
I know.
You know what?
Let's do it below.
I want to do it below.
Just here.
Private void.
Let.
And.
Yeah.
Let's climb over.
And inside we're going to do.
Can claim false because claim is over, then we're going to do transform position equals to claim over
position and.
Can grub ledge equals true.
Let's go back to Unity now.
Begin to select player.
Find animation of the ledge climb.
Go to the last frame or maybe even one after.
Click add event and find ledge.
Climb over.
This might cause a little bug.
And I'm going to show you how to fix it, if it will.
Yeah.
So now it works perfectly.
And now there was a bug you see, in that it was triggered twice.
So what happens here?
We set in our can grab latch to true at the moment when player is still is attached to the ledge and
he detected ledge again and he's doing all the process again.
So what we need here is to make a short delay.
How we can do that We can create a function private void, allow ledge grab.
It's going to return only one line of code so we don't necessarily have to type curly brackets.
We can just put return sign, which is combination of equals and bigger.
So this means return and it will say return can grab ledge equals true.
Now we're going to go back to a ledge, climb over and instead of this line of code, we're going to
type.
Invoke and invoke means you can call function with the delay.
Just how it says here invokes method name in time seconds.
So in the parentheses we're going to put quotation marks.
We need a name of the function and it should be exact name.
So let's copy it here, paste it in here, and then put comma point one F Now it should be fine.
Let's go back.
And.
Works perfectly.
Now there might be some issue just like what I showed you.
But I know this because I faced this before.
Look, if I'm jumping from here, he's not detecting ledge, but he's supposed to right from here.
He does detect in a ledge, but from above he does not.
So what happens here?
Sometimes there is no enough time to make detection.
Right.
Let me show it to you.
All right, So.
So he's there and he's supposed to detect the ledge but rigid by they move so fast that there is no
time to detect the ledge.
What we can do here is we can take our ledge detection.
And rigid body to it rooted by the two D.
Then I'm going to open this rigid body and change body type to kinematic.
Collision detection should be continuous.
We can close this now.
So this what will help us to detect collision even if we move in fast?
Yeah.
Continuous detection.
Then we can cover it.
And just in case, let's increase the box Collider of our trigger to the right side to something like
this.
You don't have to do precise copy.
Just make it a bit bigger and longer.
All right, Now we can check it again.
Let me try that.
Right now, it's okay detecting from any spot, but we can see that sometimes offset is really off and
there is a bit of space between the player and the platform.
We can fix that just by finding our check and.
Decrease in the radius.
So maybe, I don't know, like 1.5 to make it small.
And now we need to adjust offset just by bit.
I don't know.
I'll try here.
25.
Not sure.
Let me change to focused.
Yeah.
It looks like it's okay.
Yeah, it's fine.
If you still have a bit of offset, try to just adjust these values.
And remember, first offset is for position before the climb.
Second, offset for position after climb.
Let's go and put some comments there so you won't forget it.
Offset for position.
Be for a climb.
I'm going to copy this.
Paste it in here.
Offset for position.
After a climb.
Thank you for attention.
I know it was a long video.
I know it might be difficult.
I remember myself when I did something like this, and it felt complicated.
But I'm telling you, in no time you'll be creating your own video games.
I'll see in the next video by.