In this lecture we're working with if statements and tags so that if we drive over something that is
tagged as customer, we print to the console package picked up and if we drive over something tagged
as customer, we say package delivered.
So let's get started.
Let's start by doing a little bit of repurposing.
I'm going to click on my object here that was just called Square and I'm going to rename this to B package.
So this is now the package we're picking up and just reshape it a little bit.
So there we go.
It's a bit of a square package.
Doesn't quite look like the thing Amazon would deliver, but there we go.
Well, pop it somewhere in our world so we can drive over it, put over near the player so it's nice
and quick to find.
Excellent.
That's step one.
Now, step two, I'm going to be using my collision script because that's what we've been dealing with
on trigger enter and on collision enter.
But collision doesn't make sense anymore.
So I want to show you how we rename a script because it can be a little bit messy.
First of all, I'm going to click on where it says Collision one more time or you can hit F2.
Let me just check that works f to set a collision.
I'm going to name this delivery because that makes more sense.
We're dealing with the delivery system.
So I'm going to hit enter now.
I'm going to open up delivery.
And you can see here, it's still public class collision.
So it thinks I am a collision class.
I'm going to just highlight the word collision and change that to the word delivery, making sure we
spell it exactly the same as the name of our dot CSS file I'm going to save.
Now back over into Unity.
And the last step is to click on Cruise Image Drive and we can see here that the delivery script has
automatically been updated from collision to delivery.
Cool.
So that should all work exactly the way we want to.
Now, let me tell you a little bit about the logic of what we're going to do.
I'm going to show you what happens when we drive over so when we trigger something to happen by driving
over our package, I'm going to show you that.
And I'm going to print out to the console a message that says something like, you picked up the package,
but we only want to print that to the console if it's the package that we drive over.
Because I'm going to give you a challenge later on in this lecture where I challenge you to make the
customer.
And if we drive over the customer, we want to print a different message.
So we can't just say when you collide with anything, let's just have a look at our delivery script.
It says, When you collide with anything or when you trigger anything, do a particular thing.
But it's like, okay, well, when we when we have on trigger enter with one thing, we want to do one
thing, but if it's on trigger, enter is something else.
We want to do something else.
How do we do that?
The way we do that is with if statements.
Let's talk about if statements just briefly here.
So an if statement lets us check if something is true or not and then do something based upon the result.
And then this is how the syntax looks in C-sharp we start with the if keyword and then in parentheses
we have the thing that we want to know if it's true or not.
So we need to write something in there saying, Is this particular thing true?
And then in our code block we have what it is that we want to have happen if that thing actually is
true.
So for example, for me it might be if I'm hungry right now, then eat junk food while my wife isn't
watching.
That could be my if statement.
You can tell what's on my mind at the moment and if I'm hungry turns out to be false.
Then we don't even consider what's in the statement here.
We don't even think about sneaking some tasty donuts while the wife isn't watching.
It's a way of saying, if I'm hungry, do this.
Otherwise do nothing.
Okay, so let's start this process in here.
Now I'll leave my on collision.
Enter in there just in case we need it in the future, in case we bump into something, we might want
to do something with that.
But we're focusing the on trigger index triggers.
We don't want to bounce into the package and the customer is going to drive over them.
So in here we need to start out if statement and I'm just going to put a comment in here and write some
pseudo code because we don't have everything we need yet to execute on this, but we'll write the general
meaning of what we're doing.
So if the thing we trigger is the package, then what do we want to do then?
Print package picked up to the console.
Now we can give this a little bit more structure by saying we'd have our curly braces and then we would
have our print statement and then we would have our other curly braces.
All of these are within the comments.
You don't need to be typing this.
I just want to type this so you can see exactly what we're doing.
And the bit that we don't have is how do we know if it's a package or not?
So let's have a chat about tags.
Tags allow us to easily check in our code if something belongs to a particular category of thing and
allows us to speed up that process of saying, Are you the package?
Are you the package?
So I'll show you how we go about doing that.
We have our package.
We're clicked on this game object and over in the top right of the inspector, you'll see Tag Untagged.
At the moment it doesn't have a tag.
It's like, I don't know what sort of tag you want me to be.
No worries.
When we click on the untagged area, we've got some default tags that already exist in Unity.
Player is probably the main one that we would use in the future, but for now we're going to click on
the ad tag and then you'll see Under Tags List is empty.
Click the plus button in here.
I'm going to type package as the tag uppercase P and then click on Save.
Now we have package that hasn't assigned it yet.
What you need to do is click on the game object again.
You see it's untagged.
Now I can go in.
Select package, and we've tagged this game object as being of tag package.
It's not a type package, it's just been tagged as package.
Now we can jump back into our code and before I get rid of the comments, there's one other thing I
want to show you in here.
We talked about this one.
We talked about the collision and the trigger that you can see where accessing or something's being
passed into this.
When it's cold, it's being passed in, it's giving us information about the other thing that we bumped
into.
So this is really cool.
This delivery script is on our car, but when we trigger something or when we bump into something with
collision, we know what it is that we bumped into so we can gather information from that.
And in this case, we're going to say the other thing that we collided with, does that have a tag on
it that says package?
So that's what we'll do.
We'll get rid of our comments here.
We don't need those anymore.
Thanks very much.
Comments.
Lots of squiggly lines.
Cool.
If we're going to access the other thing we bumped into by typing other dot and then it's as simple
as saying tag, then we're going to use the double equals sign operator, which is a way of comparing
the left and the right and saying, Are these two things the same?
So we'd say equals equals.
And then we're going to write in here quotation marks, capital P package, if I can spell it correctly.
And this as soon as we use the equals equals operator, it's going to say, is it true or not?
Does the left equal the right?
We get a true or false value back from that.
So this is going to say it's either true, yep, it's a package or the tag is not a package.
And then in here, what do we want to do?
Well, we will type in debug dot log package, picked up semicolon and save.
Now let's jump back over into our game.
Click on the console so that we can see if this is actually going to work or not.
Click on play drive our car over to the package pip and it says Package picked up.
Excellent.
If you drive over it and over it, it's going to go up and up and up and up and up and up.
So that's something we'll deal with in the future where we we get rid of the package after we've picked
it up.
For now, we can print out the message that we've picked up the package.
So I've got a challenge for you.
It's going to be a little bit more detailed, but I think you're ready for it is to set up our delivery
location.
So I'm going to call it the customer.
It's probably just going to be a box or a circle or something.
It doesn't actually need to look like the customer because we're going to drive over it.
But I want you to create a customer that allows us to print delivered package to the console if we drive
over it, similar to the way that we drove over our package.
Now, if that's enough for you to go on, pause the video and jump in and take that on.
Now, otherwise, I've got some hints for you.
So if you don't need the hints, pause the video.
But if you would like the hints, here they are.
Create a game object that has a collider on it.
If you can't see the object, check your sorting layer to make sure it's above the road set is triggered
to true on the collider.
Create a customer tag as we did with the package tag and assign it to the object and then print your
message if we touch the customer object.
So there you go.
There's your challenge plus video.
I'll see you back here when you're done.
Okay.
First step is get out of play mode.
That's a good first step.
And then right click, I'm going to create a 2D object will make a sprite.
What should we make this?
I'll make this a square as well.
I'll just change its color.
So we'll pop the customer over here.
I'm going to increase the size of this a little bit.
Just hit R and then grab the middle of it and scale it up for now.
So it's nice and easy to see.
We can change the graphics on this later on pretty easily.
Change the color to maybe a blue.
Now I'll rename this game object to be customer hit enter.
I also need to create a tag so click on tag, add a new tag, click on plus and we will name this customer.
Click on Save.
Now back to clicking on customer the game object and tag this as customer the tag.
Excellent.
Now we need to go into our code.
I think we've done all of this.
We're going to create another if statement.
So second if statement which will if other tag equals equals customer, then what do we want to do?
Put in our curly braces, debug dot log package delivered semicolon and then save.
Now I usually miss a step in these things, so let's see if I did or didn't miss a step.
I'm going to click on play.
Putt.
Putt, putt.
Drive over here.
Yeah.
Package picked up.
Drive over here.
Okay.
So I have missed the step and I know what it is straight away because I've missed this step many times
in my career.
Do you know what it is?
What has Rick done wrong?
What mistake have I done?
If I click on customer, I don't have a collision on here.
How do I know I've bumped into it?
So I'm going to add a box collider to DX.
And then the very last step is to make sure that I click on is Trigger and then Save.
Now click on play.
That should have got it.
So drive to drive over that one.
Good.
And then over this one.
Excellent package delivered.
Cool.
So now we've got the fundamentals in our code of it knows.
Oh, yeah, you went over a package and oh yeah, you delivered it to the customer, so.
Excellent.
So that's all for this lecture.
I will see you in the next one.