Hello guys.
Welcome to the video.
In this one I want to show you how to make checkpoints okay.
So let's go to the graphics item checkpoint.
Checkpoint.
Let's take this, uh.
Actually, we need to take all of them and do pixel per unit 16.
Okay.
Now we have animation of checkpoint being activated.
Then we have animation for checkpoint, like, you know, being active.
And we have animation of an idle checkpoint.
I'm going to take the sprite of an idle checkpoint.
Let's switch sorting layer to items.
Just like that.
Uh, yeah.
Let's do minus one over here.
And let's name it as checkpoint.
And just checkpoint.
Yeah.
Now I think we can place animator controller on the same game object, so it is easier to control with
the script.
And let's just go to scripts and make new C sharp script.
Checkpoint.
Now I'm going to open it and I'm going to do close all by this.
Now over here, what we need is private animator animation, and I think we need a boolean that would
say if checkpoint can be activated again.
So we actually need to make a decision here.
Uh, let's say you have a level like.
This.
And you have checkpoint here and checkpoint here.
When player goes to the first checkpoint, it is activated.
Then player goes to the second checkpoint and it is activated.
Then let's say player jumps and dies somewhere here and where we're going to respawn the player on the
last checkpoint or on the closest one.
It is purely up to your decision, I think.
Uh, to keep it simple, I will just respawn him at the last checkpoint activated.
So we're going to need private bool can be activated.
And also we need.
Private.
Bool.
Active.
Then we're going to make Ontriggerenter 2D.
And over here we're going to get player player.
Collision getcomponent of a player.
And then we're going to need a method.
Activate checkpoint, which will do one simple thing.
It would say, uh enum.
Set bool.
Active to the Boolean of active.
Actually, we could use trigger as well, I think.
And actually we don't need this second boolean.
I have a better idea.
Yeah.
So this is what it's going to do.
Then over here we're going to do if player not equals to null then activate.
Checkpoint.
And over here at the start we're going to do.
If active, then return so we cannot activate checkpoint again if it is already active.
And also of course I forgot to do it here.
We need to do active equals to true.
I'm sorry.
So yeah how it's going to work.
We have animator and we have Boolean checkpoint active.
Once player enters the trigger, we check if it is active now.
If it's not, then we can proceed with trying to get component of a player and then if player is not
equals to null means player entered the trigger.
And because of that we're going to call the method activate checkpoint which would set active boolean
to true.
Because of that, this would not be able to activate itself again.
And then we would take animator and set.
Value of the parameter to the value of the boolean we have.
So we can play animations.
Yeah.
Let's save this.
Let's go back.
We need to find a checkpoint.
We need to put checkpoint script on it.
And then we need to add a box Collider 2D make it is trigger.
Let's adjust the collider to something like.
So maybe.
Yeah, this should be just fine, I guess.
Then we need animator controller.
Let's go to animations and let's make new one.
Check point.
AC then we're going to take Checkpoint Drug Animator controller over here.
And then we're going to create new clip.
For this.
Make sure you select checkpoint click create.
Over here.
I'm going to do a check.
Check point idol.
And let's go to graphics.
Item checkpoint.
Checkpoint, and let's just drag it here as an idle.
Then let's make.
Checkpoint activated.
And for this one we're going to use this animation over here.
Let's just drag it like so.
Make sample rate 20.
And then we're going to make new clip.
Checkpoint.
Idle active.
And for this one we're going to use this checkpoint with a flag like that.
Sample rate 20.
Now let's go to animator.
Let's select checkpoint.
And over here we want to have checkpoint idle.
Then we need checkpoint activated.
Then checkpoint idle active.
We're going to make one transition here.
If Boolean is true active it should be used.
Trigger parameter.
Because we're not going to transfer back.
So we don't have a reason to check boolean all the time, right?
Yeah.
Let's let's do it.
Let's do trigger activate.
Okay.
So over here we're going to add condition activate.
Let's uncheck exit time.
Transition duration zero.
Then we're going to make another transition to idle active.
And here we're not going to need any condition, but we need exit time one and transition duration zero
just like that.
And then it's going to use this animation and there is no exit from it.
Okay, let's go back and change the parameter to activate.
Okay, I'm going to save it.
And then we're going to try it out.
Let's go to the scene.
To the game.
And I'm going to just try to jump on the checkpoint.
Oh.
Something is wrong.
Let's see.
What is it?
And him.
I did not get animator.
Uh, wake.
Also, you know what this animator is going to be called only once when we activate in checkpoint.
So instead of getting a component, we can do like this get component of animator.
The issue with this is that every time you call an animator, it would try to get component.
And actually when you get in component it takes some, you know, processing power.
And if you just maybe use, uh.
Like for example, if we do random return getcomponent, then every time we use animator in this method,
every time it would get a component every frame, that would be crazy.
That is not okay.
But on a checkpoint when we're doing it only once, I think we can do it this way.
It's fine.
Yeah, and let's save it.
Go to play mode.
Ay yay yay.
We're going to see it now.
I don't know what's up with this, um, error message over here.
I'm getting it only in a new version of unity, and I hope it will be fixed by the time you're doing
this course.
Nice.
Very pretty, I like it.
Now let's actually reassign a position on a game manager when we activate a checkpoint.
So I'm going to go here and I'm going to make.
What we can do here.
I'm going to do public void.
Update respawn position.
And over here, we need to pass a value the same way as we did with a boolean.
We can just make a vector three new position.
And then we would take, uh, sorry, we're not using position.
We're using transform.
So we need to do transform.
Let's do transform.
And let's do a new respawn point.
Then we don't need curly brackets over here because we can do lambda expression and type.
Respawn point is equals to new respawn point just like that.
Now on the checkpoint itself.
When we activate in checkpoint, we're going to call Game Manager.
Game manager dot instance update respawn position.
And as a transform we're going to pass checkpoint itself.
So I'm going to just pass transform.
And save it.
Now we go back and it's supposed to be cool.
Let's go.
Sorry, I got a bit sleepy to do.
Let's go and activate checkpoint.
And we're going to see on the game Manager we've got respawn point checkpoint.
Okay, now let me try to.
Jump into the pit and.
Yes.
Okay.
Now, if you have a couple of them, only the latest one will work.
Let me quickly try that out and we're going to go to the next video.
Um, yeah.
Just like that.
Now it is activated.
I'm going to die.
And.
Here, am I?
Okay.
Very cool.
Okay.
And you cannot activate these checkpoints.
So yeah.
Sorry.
If you want to activate these checkpoints you have to do something about it.
Maybe.
You know, let me just show you the functionality.
Why not?
Let's make a serialized field.
Private bool can be reactivated.
If you have a better name, you can use it and.
We're going to do this and can be reactivated equals to false.
So if this one is false then this if statement still will work.
But if you make it true then it doesn't matter if it is active or not.
You're still going to, uh, activate checkpoint on trigger enter.
Yeah.
And let's go ahead and try this.
Maybe we can make some parameter on a game manager that would tell checkpoints can be reactivated,
but that is not as important right now.
And let's go.
My bed.
So now we can see on the game manager we have checkpoint one is active checkpoint.
Then I'm going to jump to second one.
Now on the game manager.
First checkpoint is a checkpoint.
Now I'm going to go back to the one I had before.
And now in a game manager.
Oh didn't work because it cannot be reactivated.
Let's select them and say can be reactivated.
Okay, game manager, I'm going to see it.
Yeah.
We switched.
So if you want to keep this available, you can do that.
That is up to you.
The last checkpoint you touched would be your checkpoint.
Uh, for me, I'm going to switch it off.
I don't want to have it.
So now let me delete the extra and let's go to assets prefab.
Let's make prefab out of this one.
Also let's go to animations and let's make a folder for checkpoints checkpoint.
And let's drag it here.
Hello, guys.
I made a mistake in a script.
Uh, for some reason, it didn't show me an error, and I did work.
So if you're confused.
Me too.
I'm confused as well.
Why?
It did work.
But anyway, I'm going to show you.
What did I do wrong?
Over here on the animator, we're using trigger.
And in the script I use anim set bool.
And it worked.
So I guess magic.
Nevertheless, I want to be sure it is right.
So I'm going to do set trigger and I'm going to just.
Leave it at that.
Okay?
This is how it's supposed to be.
Uh, thank you so much for paying attention.
And let's go to the next video.