Hello guys.
Welcome to the video.
So in this one I want to make respond for the player.
So when he dies he can get on the starting position or something like that.
There are a couple of things we need to do to make it work, but they all are very simple.
Okay.
First, let's make a respawn point so we can use it.
I'm going to go to graphics.
Uh items checkpoints start over here.
We need to be sure they have pixel per unit 16.
So let's, uh, select them.
And make sure you have pixel per unit 16 over here.
Now let's take sprite of the idle one.
Just like that.
I think we can take it and choose layers layer for this one.
Now.
Wait.
Maybe it should be like, maybe enemy should pass by this one and we should see the enemies.
So I think let's put it on the ground.
Let's do it.
Order in layer one.
Okay.
Yeah.
Or is it item.
Yeah.
It's an I think it's an item.
Okay.
Let's keep it as an item.
And then once we have enemies we're going to see what is better working for this one.
And just to be sure it is behind other fruits, I'm going to do minus one here.
Okay.
So now we have a starting point that is on the layer of items I mean certain layer.
And now we need to rename this to something like Start Point.
This is the place where we're going to start the game.
And for this one, we don't need anything else for now, so this is just fine.
Let's go to the game manager.
We need to open the script.
Or actually, let me show you something.
Over here.
We have reference to the player, but when player dies.
This reference is missing, so we need to refill this reference as well.
Let's go back to script.
And over here I'm going to do header.
Player.
Then we need serialize field.
Private GameObject.
I'm going to name it player.
Prefab.
So we have prefab of a player and we can respawn him.
Then we need to realize.
Field.
Private.
Transform.
Respawn point, so we know position where a player should be after he responds.
Now let's go ahead over here and make a public void.
Respawn player and inside we need to do GameObject.
New player equals to instantiate player prefab, add respawn point, dot position.
And we don't need to type dot transform here because respawn point is a transform itself.
And then we're not going to change rotation.
So it's going to be quaternion dot identity.
Very nice.
Now, as you remember, I said that we need to refill this parameter over here because we're going to
use it later on in different places.
So one of the ways to do that would be to change this game object to just player over here.
Oh.
I'm sorry.
Just like that.
So now this reference is this reference.
And we're going to instantiate the object.
But this one is a game object.
But nevertheless we can type dot get component.
Of a player and it would create an object, and also it would get component and assign it to this variable
over here.
But I don't like it.
I don't like to do it this way because it's kind of, I would say cluttered.
I want it to be more readable.
That's why I'm going to do this.
I'm going to make it as a GameObject new player.
And then I'm going to do player equals to new player.
Get component of a player and that's it.
This is very nice and very clean.
Now let's save this and let's go to Dead Zone.
And over here, after we die, we're going to do game manager dot instance respond player.
Just that.
Now let's go ahead and see how does this work.
Oh, sorry, I forgot to do something.
We need to take this start point, move it to respawn point over here.
Also, maybe let's take Game Manager and move it all the way above.
Then we need to go to assets prefab and we need to make prefab of a player.
We can just drag it like so.
Then on a game manager, we're going to take the prefab and give it over here.
Very cool.
Now let's go to the game mode and let's just jump into a dead zone.
And we have a new player every time we die.
Very cool.
Now I want to make it a bit prettier because I don't like how it looks right now.
One of the ways to do that would be to add a delay for eastbound.
So let's make a variable serialized field.
Private float.
Respond.
Delay.
Then we're going to go over here and make a private I enumerator respawn CoroCoro coroutine.
And over here we're going to do yield return new wait for a seconds of respawn delay.
And then we're going to take these two lines of code.
Cut them out.
And place them in here.
And this method would only start a coroutine.
Startcoroutine.
Respawn.
Coroutine.
So now we respawning player with a coroutine.
But this coroutine is private, so we cannot call it outside of a script.
That's why we have this public method that is called respawn player.
And this method will start a coroutine.
Now let's go ahead and check the delay.
I'm going to give it a value of 1.5 seconds.
That's, uh, good enough, I think.
And you.
You okay?
Very cool.
Now let's make visuals for the respawn.
This time we do need to instantiate any visual effect because I have a better way.
We're going to go to assets.
Uh, sorry.
We're going to go to Players Animator and we're going to create new clip.
Then we're going to go to Animations Folder Player.
And over here we're going to do player respawn.
Nice.
Now let's save it.
We're going to go to graphics main character, take this appearance sprites and place them over here.
Sample rate will be 20.
Yeah.
Very cool.
And on the animator, we're going to take this newly created animation and we're going to click right
button and choose Set Default.
Now this is a default animation that is played when object is created or, you know, just starting
the life cycle.
And from here we're going to make transition to idle move.
With an exit time one.
Transition duration zero.
Okay.
Now let's go back to Game Window.
And we can see this effect at the start.
And even when I die.
It is there.
The only issue is that I don't really like how he's affected by gravity and how he can move while he's
respawning, just in that animation.
So I want to fix it.
You're going to go to player.
Over here, and let's make some sort of a method that says, actually, I think we can just make a method
that would say something like respawn is finished or not.
Just simple as that.
What do we need to do?
Let's do it.
I'm going to go ahead somewhere here.
And I want to make a boolean that would say if I can move, we already have is knocked, but I don't
want to use is knocked for the respawn because it just has a different name.
So it can be very confusing why it is there.
I'm just going to go ahead over here and make a private bool.
Can be controlled.
Maybe even I'll place it above.
Just like so.
Yeah.
And by default it's going to be false.
Let's save this then.
Let's go ahead and below the update I'm going to do public void respawn finished.
And inside of this method we want to check if it is finished or not.
And actually instead of checking we can pass the value into the parentheses.
Let's just type away here bool finished.
And now whenever we call this method we can just pass boolean through.
The parentheses could be something like this.
Respawn finished true or respond finished false.
And this value would be passed over here.
And now we can use this value over here as a local variable.
Very cool.
I want to make a float gravity scale and I would make it equals to arb gravity scale.
We're just going to save the value.
Then over here I'm going to do.
If finished then ARB gravity scale equals to gravity scale and can be controlled equals to true.
Else.
We're going to do.
Our big gravity scale equals to zero can be controlled, equals to false.
Just like that.
And then over here before is knocked.
We're going to do if can be controlled equals to false then return.
Simple as that.
Also it would be a good idea to disable Collider I guess.
So we don't get knocked while we respawn in just in case.
So.
Yeah.
Let's go ahead over here and get private Capsule Collider 2D.
I'm going to call it CD.
Then in the awake we're going to do CD equals to get component of a capsule collider 2D.
Then.
I'm sorry.
That was too quick.
Yeah.
Just like that.
Then inside of this method I would do.
I CD enabled, we can disable a component.
Equals to true.
And over here it would be CD enabled equals to false.
Very nice.
Now we have a goal.
We need to call this method at the beginning of the respawn animation and set it to false.
And then oh by the way, this would not work.
This would not work.
Um.
Yeah.
We need to do something else here.
Let's go and make private float.
Default gravity scale.
I'll just place it into a movement.
Yeah.
We cannot make local variable.
I'm going to quickly explain why.
Over here.
First we would call it with a value of false over here.
Yeah.
And.
If we do that, we're going to say our gravity scale equals to current gravity scale.
And next time we call this method with a value of.
True.
This value would be reassigned again, because this is called in the beginning of the method, and because
we have our great scale zero because we set it to zero when it was false, this would assign it to zero,
and then this value would be used over here as a zero.
Yes.
So we cannot do a local variable.
We need to.
Do.
Default gravity scale over here.
Very nice.
Now we can go to the start method.
We don't have a start method.
We're going to make it.
And in the start I'm going to do default gravity scale equals to ARB gravity scale.
And then I would do respawn finished false.
So this would just give it default values.
So this will just uh sorry.
Let's just.
Do it like so.
Yeah.
So because I'm calling this method with a false false value will be passed over here because of that,
this if statement will not work and else statement will work.
And over here we're going to set gravity scale to zero can be controlled false.
Collider enabled false.
So because of that, player is not going to fall.
He's just going to stand in the air and he will be invincible for some time.
Right.
Let's go and try this.
I'm going to go to play mode and.
I cannot control it.
Very cool.
And also Collider is disabled.
Yes.
Now we have a goal to call this method over here with the value true whenever we are ready and we can
do it with the animation trigger, right?
We could go to.
Animator animation.
Find this player.
Respawn and.
In the end.
Over here, we can add an event.
The only problem is we cannot add event to this game object from a player.
Events are available only when script and animator are on the same game object, just like we have with
a visual effect.
Over here.
We have animator and script together, and that's why this animator on this object can call method from
this script.
But over here, this one has no script and this one has no animator.
So we cannot just directly call method on this animator from this script.
But.
We can go to scripts.
And make new C sharp script player.
Animation events.
So over here I have player animation events.
Now I'm going to type private player.
Player.
In their wake.
I'm going to get player equals to get component in parent of a player.
And this will get a variable player in the parent.
We can go back.
And just go to play mode and see it in a debug window.
Animator.
Sorry, we need to put the script on.
Let's put the script.
Then let's go to play mode.
Debug.
And over here I have player reference.
Very cool.
Now we can go back to this one.
Uh, player animation events.
And I'm going to make an event public void finish respawn which would do just player finish player respawn.
Finished.
True.
So this script will call method from a player which would finish respawn.
And we're going to call this method on the event that will do the job for us.
Okay, let's go back to unity.
On the animator.
Let's switch to normal mode.
And then we're going to find player respawn.
And at this event over here we're going to do player animation events method.
Finish.
Respond.
Now let's select the player and apply overrides.
And let's go to play mode.
I'm going to jump.
Very cool.
I cannot be controlled while I am respawning, but I can be controlled after and I'm falling only when
I'm ready.
I think this is a cool effect.
I like it a lot.
Well, this is good for now and I'll see you in the next video.