Hi everyone, I'm Jeremy Gibson Bond,
and welcome back to the Unity Certified Programmer exam review materials.
In this video, we're looking at the first part of the solution to the Points,
Jumps, and UI challenge that you had.
In this video, we're going to be looking at
the PlayerShip itself and all the changes involved
in it including the Jumps and jumping to a location that is relatively safe which is fun.
First, I'm going to look at my PlayerShip.
In the PlayerShip, you can see I've added Respawn Delay and Starting Jumps.
I talked about thinking ahead about
where the information should be so that other people can modify it and they
might know where to look for it and because both the amount of time it takes to
Respawn and the number of Jumps you start with are core to the PlayerShip.
I thought it would be best to put them on the PlayerShip and each one of these has
a tool tip that shows a little bit more information about what it does.
Great. So, let's go into the script now and take a look at what's different there.
So here in the script,
I've added a few static fields,
the number of Jumps remaining.
Last_Collision and Collision_Delay, we're going to talk about these in a little bit,
but these have to do with avoiding multiple hits in the same frame on the ship.
Then you've got the respawnDelay and the startingJumps that you just
saw and on Awake we set
the Static Field Jumps equal to the startingJumps and
then the Static Field Jumps actually stores all the core information from then on,
startingJumps is just used up then.
Okay, updates all the same,
fires the same and now we have a new OnCollisionEnter.
So, this happens anytime that the PlayerShip collides with
any thing but the only thing the PlayerShip should be able to collide with are Asteroids.
So, we get the Asteroid component of the other thing
that we collided with and if that is null then,
somehow we collided with something that's not Asteroid so we return.
This is where we say okay if the current time is less than
the Last Collision plus the Collision Delay then go ahead and return and what
this stops from happening is sometimes
a PlayerShip might actually collide with both an Asteroid and a
smaller Asteroid or two different Asteroids at
the same time and we really want only one of those to be the real collision.
So, the Collision Delay of one second means we have a Collision and we set
the Last Collision to be the current time and then any other collision
that happens within a second we're just going to ignore, we're going to return.
Jumps gets decreased and if Jumps is less than zero
we call GameOver on the Asterax script
and we set active of the PlayerShip game
object false which causes it to disappear and then Respawn is called.
Respawn actually causes us to respawn in a new location.
Scrolling down here if this Debug PlayerShip respond
notifications is turned on which is
another one of those how to find things that I did at the top of the script.
Then we spit that out to the console and then we Start the Co-routine which is going to
find a good RespawnPoint for us and I turn off the OffScreenWrapper.
So, it is disabled and then I move the ship way off screen this 10,000,10,000.
So, that way it doesn't try
and wrap itself around the screen or anything weird like that it
just sits off screen until we bring it back on and at
that point we will re-enable the OffScreenWrapper script.
Before jumping into the Coroutine itself,
I want to talk about this RespawnCallback.
Now you can see the RespawnCallback is passed in as the second parameter on
the FindRespawnPointCoroutine and then that actual Callback Method starts on line 135.
A callback is a name for a method that is
called by something else to let us know when it is done.
So, here that gets called with the new position that the ship should jump to.
If we are debugging and passing out these notifications to the console that gets passed
out and then we set the Transform Position of the ship to
the New Position turn the OffScreenWrapper back on and we're done,
the game starts back up again.
So, now let's go down and look at this Coroutine.
There is a lot of documentation of how the Coroutine works here and if you mouse over it,
it will pop up in this really long thing.
I'm going to give you a short version and you can look at
the long version in your own copy of the code.
So, this is the actual Coroutine that we call and you
pass in the previous position where the ship was
and a CallbackDelegate that gets called back when
we're done and that is the Callback Method that we passed in.
You can see that my Respawn Divisions is set to
eight and Respawn Avoid Edges is set to two.
What this does it divides the field into eight segments horizontally
and eight segments vertically for a total of 64 and they're numbered like zero,
one, two, three, four, five, six, seven.
Respawn Avoid Edges two says don't use zero, one or six,
seven so you're left with two, three, four,
and five in the middle with a nice four-by-four grid.
The first thing that this does is,
if there were a particle effect it would
Instantiate it on line 292 we don't have one yet,
so I have that commented out.
That's going to come in the second course.
Then here it sets up all of the points and this is going to
be a grid of points that stores,
how close the closest Asteroid is.
So, it's trying to find the point that is furthest from
any single Asteroid and is not the same point that you just jumped from.
We don't want you to disappear and reappear in basically the same place.
So, we want to make sure that you jump to another part of the screen.
So, that gets all set up and then there is
a yield where we wait for 80 percent of the Respawn Delay Time.
The Respawn Delay Time is currently two seconds so we're waiting for one
1.6 seconds and then we
check and actually see how close
the closest Asteroid is because if we checked ahead of
time then in 1.6 seconds they would've moved quite a bit.
Here we're leaving only 0.4 seconds which is not too far for them to move.
So, we figure that out and frankly we're not getting the closest distance,
we're getting the closest distance squared because that
avoids us having to do a square root and square roots can be costly.
Again, you should look at this code on your own.
We find the nearest thing and then we iterate
over all of the respawn points to find the one with
the furthest away closest
Asteroid and then we would spawn the particle effect for reappearing.
Again, there's no particle effect so we don't do that,
but we wait the remaining 0.4 seconds and then finally we hit the Callback which
calls back to that method in the PlayerShip script.
So, if we click here we can say that would call the Respawn Callback method.
Now, you'll note that FindRespawnPointCoroutine is actually on
Asterax and that's because I thought that maybe at some point in the future,
I might want to reuse it and also it's Asterax
that knows where all of the Asteroids are because
Asterax is maintaining a list of Asteroids.
So, it was just easier to put this on
the Asterax script and call it from the PlayerShip script.
So, that's it for the respawning and the other thing that we showed was OnCollisionEnter.
If there are no Jumps left then we are going to call
GameOver and GameOver is also on Asterax
and you'll see this a lot I have this private singleton
_S in Asterax and I want to call the EndGame method that's on
that singleton that is in a method of that class but I want to make it
statically accessible to call it without exposing the actual class, the singleton.
So, I create a static public void GameOver which then
calls _S.EndGame and that actually ends the game.
It sets the game state, the GameOver state and then
invokes reloading the scene in four seconds.
Which is delayed before reloading scene is four seconds and when we
ReloadScene we're just going to use the Unity Scene Manager to reload the scene.
So, that's it for this video.
In the next one,
we'll actually look at how the UI works and how the GameOver panel works.
Thanks a lot and I'll see you in that video.