Next up, we're going to create ourselves a win condition and a lose condition.
What do we mean by win condition?
Well, how do we win this level?
What's the criteria for winning?
And for me, it's going to be get all the way to the end and you cross the checkpoint without crashing,
then you win.
And for our simple little game we're making, when your win will just restart you back to the start
of the level.
Likewise, I need a lose condition.
So the lose condition will be if I'm cruising along and I bump my head, then I lose.
And as with winning, we're going to restart the scene.
So in this lecture what we'll do is hook up the functionality to detect when we've won or when we've
lost.
And in the next lecture we'll hook up the functionality to reload our scene.
So I'm going to start with a win condition, and I should put my win condition all the way down the
very end, all the way down there.
But you're not for testing purposes.
I'm going to put my win condition right here, right next to the player, just so we don't have to skate
along for so long before we test it out.
I'm going to create two new scripts down here.
So right click Create C-sharp script.
The first one I will call Finish Line and the second one I will call.
So right click Create C-sharp script.
I'll call this crash the tech door and I'm going to work on the finish line first and then I'll challenge
you to work on the crash detector.
How does that sound?
A pretty good plan.
I reckon it's going to right click in the hierarchy and I'm going to create an empty game object.
And the reason I'm creating an empty is I want some visual representation on this, but I don't want
the visual representation to change if I'm manipulating the parent.
So I'm going to have the visual representation underneath the game object.
So we'll call the game object finish line like that.
And then the visual representation.
I'm going to right click on finish line and create a 2D object sprites and a square.
I think square is fine.
Now the square you can see has come in zeroed in at the zero of our finish line.
So it's right in the zero point of finish line child relative to parent and we'll call this post and
I'm going to just manipulate a little bit.
So hit R to scale in and to scale up.
We want a nice big old post looking thing.
I'm going to change the color to red, I think.
So it pops out from the snow.
That seems pretty good.
And now click on finish line, right click and create a 2D object.
This will be a circle.
Now the circle I'm going to rename to be the top, I guess, and we'll just kind of have this as the
top that goes on top of the post.
And once again, I'll, I'll make this red.
Now if you wanted to make it the exact same color red, I can click on the post, click on the color
picker, find the hexadecimal which is F 10,000 and then click on top, click on the color and paste
that in there.
Boink.
There we go.
Now it's exactly the same color.
If I click on the parent of the finish line now, I can move this around and kind of anchor it into
the snow like that.
Now the next step is that I need to put some collusion on this thing so that when the player goes flying
through it, we know that we've actually collided with it.
So I'm going to click on my finish line.
Parent level ad component will make a collider.
What sort do we want a box collider to?
DX You can see it's a little bit smaller here.
The player could accidentally jump over that.
So what I'm going to do under size and offset, I'm going to manipulate this.
So size will make it, let's see, nice and high, probably gigantic on the Y.
So there's no way of me being able to kind of fly over it.
So we've got this nice big old collision that we're going to bump into.
We're going to make it a trigger because if we didn't make it a trigger, we'd go clunk and clunk into
this invisible force field.
So we need to make this a trigger.
So it's got a trigger box and it's got some visual representation to it.
Now, the last thing for the finish line while we're in here in Unity is grab the finish line script
and drag that over onto finish line.
So the player is responsible for what it's doing, but it's the finishing line that's responsible for
saying what happens when something goes through the finish line.
That's why we're putting our code on the finish line.
But sometimes when you're going through this process, you'll think, Hmm, should I put it on this
game object or on that game object?
And there's many ways to go about doing it, but I like to put the logic on the thing that is doing
the controlling or or has the power in this case and the finish line has the power.
Let's open up our finish line script and we've done this before.
We're going to do an on collision enter.
So get rid of my start and update methods and on sorry, not on collision enter on trigger enter.
They're quite similar aren't they.
On trigger enter.
I'm going to remove private and reformat a tiny bit because I like to do that.
Now, what's going on in here?
Well, for now all we going to do is just debug log, which is going to print out something to the console,
parentheses, semicolon quotation marks and I'm going to print you finished as simple as that.
And then semicolon down the end.
Save that up, jump back over into unity.
Click on the console so I can see this message.
Book, click on play.
Here we go.
Slice, I say plump.
You finished?
We've got a couple of you finishes in there.
That's all good.
That's probably representing the fact that I've got a two colliders on here.
So each time these colliders have gone through, it's like you finish and then you finish.
That's cool.
It doesn't matter for us.
Now, which point of this is probably the snowboard is fine going through.
It doesn't really matter if there's two things, because once we go through, then we want a particular
thing to happen.
So we might change that later on, but it's cool for now.
I think that's why we've got our two u finished on there.
So that is success.
From that point of view, what we should probably do is just make sure nothing else can go through this.
We might have some creatures or some other objects, and you want to make sure in general that if it's
only the player that can trigger something, then you protect for that and you make sure only the player
can impact that.
So what we need to do in here is just make sure that we're saying if it's the player that slides through
here, then go about doing this thing.
So how do we know it's the player?
Well, if I click on Barre, we did a little bit of this in the previous section.
We got up to Untagged where it says Tag here.
Untagged, which has yet to play a player, is one of the tags that unity comes with already.
So now Unity knows you are the player, you're tagged as the player.
Jump back over into here.
We just want to make sure that we're only printing this if it's the player.
So if and we can get information from other.
So when we have this trigger event, we get information about the collider, about the thing that collided
with us and it's named as other.
So if other so the thing that collided with this dot tag equals equals and we use the equals equals
operator equals operator because we're saying if it is this particular thing, if there was only one
equals, then we'd be saying, hey, we want to make the thing on the left equal to the thing on the
right.
But we're saying if it equals this particular thing and what is it in our quotation marks, we're looking
for a player with a capital P that's important.
Then we're going to wrap in our curly braces debug log.
You finished?
We'll save that.
We'll make sure that that particular thing works.
Clear my console, click on play.
Here we go.
Slide slides.
Slide.
Any second now.
Slides I slide.
Bump.
We get you finished.
Wonderful.
Okay, so we've completed that stage of knowing when the player slides through.
So.
Okay, so now that we've done that, I want to do the same thing, but for when the play is head.
So this trigger volume just here bumps into the ground.
When that happens, I want to have our level reset, but for now, we're just going to print out something
in the screen that says you bonked your head, so I'm going to give this to you as a challenge that's
to finish the crash detector.
So in our crash detector script, write some code that will print to the screen if we bonk our head
on the ground.
There's your challenge.
Take that on.
See you back here when you're done.
Okay.
So let's find our project crash detector.
Open that up.
And in here, we're going to do similar sort of logic before we're going to have a on trigger enter
to de tidy it up a little bit.
And for this I think I'm going to have the player responsible for this.
So the player needs to know I crashed, I need to do a thing.
So it's all about I crashed into a particular thing.
Whereas the finishing line I was saying, Hey, someone pass through the finish line, do a particular
thing.
So like I said before, there's a little bit of you can do it one way or do it the other, but I feel
good putting the crash detection as a script that lives on the player.
So I'll do that right now.
Click on barrier, just drag crash detector over there.
Just had to scroll down the bottom so I can see it.
There we go.
Just going to roll up my circle collider and my rigid body so I can see these things a bit more clearly.
So now it's the player saying, I bumped into something.
What are we going to bump into?
Well, if I click on the ground, the level sprite shape it's currently untagged, I might actually
tag this as ground.
So you didn't have to do this, have to have to do this.
But I think this is a good layer of thoroughness.
We're going to add tag whatever you want, but I'm going to click on the plus button and coal mine,
Ground Save, click on the ground itself again and then just tag it as ground.
So it's similar to what we were doing before we tag the player as player.
We're tagging the ground as ground.
So when the player bumps into the ground, we can do a particular thing.
So back to a script over here, back to our script over here, our crash detector.
I'm going to say if the other tag equals equals what it equals ground, then we're going to do a thing.
And for now it is going to debug dot log.
Ouch.
Hit my head semicolon.
Feel free to insert your own witty clever comment in there.
You don't have to use my semi witty semi clever comment will save that up and then jump back over into
unity and once again click on the console, clear the message, click on play.
I'm going to see if I can rotate straight away and flip upside down.
No, no.
I was clicked in the wrong window.
You can't control if you clicked in the scene window.
So I'm going to move my window over here game window and do a rotate.
And then here we go.
Bump your head, boink out.
Hit my head.
Bump, bump, bump.
Wonderful.
Okay, so out.
Hit my head.
And when we go through the finishing line, it's saying you got through the finishing line.
So there's the mechanics of our reloading on our win event and our lose event.
In the next lecture, we will set ourselves up where it actually will reload our particular scene.
Okay, so in the next lecture.