In this lecture, we're going to wrap up the main functionality for this section of the course by adding
in a boost.
When I drive over this yellow thing, I go a little bit quicker and then when I bump into anything else,
I go slower than I was going before.
So let's implement our speed up and our slow down mechanism.
I'm going to give you a couple of starting pieces for this and then give you a pretty meaty challenge
to sink your teeth into.
So first thing I do in the hierarchy is right click to the object and I'll create a sprite, which will
be a Oh, make this a circle, I think just to make it look a little bit different and I'll call this
a speed up.
Now, where's the speed up in my scene?
Double click.
It'll take me right to it.
It's underneath a tree because that where was that was the middle of the camera when I added this into
the scene.
I want to hit w move this over to the left.
I think it's under my road, so I need to put it up, say five in the order.
Change the color.
I think yellowy orange kind of thing makes sense.
Maybe a yellow.
And so I don't have to drive too far.
I'll be lazy.
I'll put it right over near where my car is at the moment.
So there we go.
Probably make it just a tiny bit bigger, hitting R and scaling it up a little bit.
There we go.
That's a speed up thingy that when I drive over it, I wished to speed up.
Now I'm going to open up my driver script.
Everything we do can be done in driver in this case, and I'm going to give myself two more serialized
fields up at the top here.
So Serialize Field will get a float and I'll call this slow speed.
It's going to represent how slow we go when we bump into something.
I'll initialize that at 0.1 F just the same as move speed.
At the moment, I don't know what it actually is in the game.
If I click on Cruising MC Drive, we've probably modified this in the inspector, so it's currently
hidden.
We open that up and currently the move speed is 20.
Okay, so my initialization is way off in there.
Will make this 24 slow speed and I'll just while I'm here, this won't change anything.
But just in case we do some updates for our code, I'm just going to make that 20.
And you know what?
Slow speed will make 15 a little bit slower than the regular old move speed.
Now I'm going to add a next serialised field and this will be of type float and we'll call this boost
speed.
How much do we want?
Boost B will say 30 F as a starting point.
So we've got this concept now, these variables.
One is our general old.
Here's how fast we're going to be moving and we're using that throughout our code just down in here.
And the other is how slow we're going to go or how fast we're going to go and save that up.
And with that done, I'm going to give you a big challenge.
That big a challenge is going to be to create a boost and bump system.
So boosts are triggers.
We've just put one of these into our scene.
If you look at this thing here, this is what I mean by this boost thing.
It's going to be a trigger.
So some things you need to do to that.
But I want that to speed up the player.
When we drive over it, we're going to have bumps and bumps are going to be colliders, which slow down
the player.
I'm pretty much going to make everything else in my world, all these other things, houses and trees
and stuff.
If I bump into them, I'm going to slow my vehicle down.
That'll be my approach.
With that, you could make separate things that you bump into, which slow the player down if you wanted
to like witches, hats or cones that you place around.
And we can do all of this in our driver script.
So there's other ways you could do this in the future, but for now, we're just going to do it all
in.
Driver You might want to use a tag.
And the other little hint I'll give you here is remember our on events so on trigger enter for example,
we might want to be playing around with those sorts of things and essentially what we're doing is changing
move speed to be either slow speed or boost speed depending upon what happens, depending upon whether
we trigger something or bump into something.
We want to be changing what the value of move speed is.
So there we go.
It's a little bit of a trickier challenge, so be sure to give it a shot.
This is how you learn by sitting there and staring at it and trying to figure it out.
But if you get really stuck, then you're more than welcome to watch my approach, which I'll go through
after you come back from giving it your best shot.
So pause a video and I'll see you back here when you've given a good old try.
Rightio.
So I need to in my driver script, I need to get rid of Start because we're not actually using that
at all are we.
Start don't need you.
What we do need to have in here is some on events so on trigger into remember the two.
RD So you're not crazy like Rick for getting the two?
RD I can just hit tab that will auto complete remove the private because I tend not to like it and then
give myself a little bit of extra formatting in here.
What are we going to be doing without triggering?
Well, it's really similar to the delivery.
So if we jump over to our delivery script and scroll down, you can see that we've got if the tag relates
to something, then we do some things.
So let's put an if statement in our driver dot.
So if tag equals equals and I haven't created this yet, but we'll put in parentheses and then boost.
So we need to create that in a moment.
So if it equals boost, then curly braces.
Let's just for now debug dot log and we'll say are you are boosting man okay just to make sure that
it works before we do any more of our logic, save that up, hop back over into unity.
And I need to make sure that when I run over this guy here, it's got all the things it needs on it.
So what does it need?
Well, it needs to have a collider for a start.
So we'll choose a circle collider to RD.
We need to make that a trigger very important.
We don't need to put on a rigid body because we don't want to be bouncing it around.
It's just going to stay there where it is and we also need it to be tagged.
So I'm going to add a new tag, which I will click the little plus button and call this boost with a
capital B, click on Save.
Now I need to click back on my speed up again and I will change the tag to B Boost.
Now we'll see if that works.
When I click on play, I can drive, drive, drive and we should print out the console something down
here, which we didn't.
So once again, Rick's made a mistake.
Let's see what we've done here on trigger into two 2D.
Yes.
Okay.
So the mistake I made sorry I was going through this a little bit too quickly and not paying attention.
It's not tag equals boost, is it?
If we go back to delivery wins, says other tag, why is it other?
Well, when we collide with something, we're getting this information passed in.
And don't worry if you don't understand passing in and parameters and stuff yet, we'll do that a lot
more throughout this course.
But for now we just need to know we're getting information about others, so it needs to be other dot
tag.
Okay.
And you know what?
A lot of my mistakes, I make a few, I edit them out.
But every now and again I like to keep them in.
Just so you can see the thought process I'm going through if errors come up because I guarantee there's
going to be issues and errors.
Even if you're following along with what I'm doing, you're going to type something a little bit wrong
or miss it.
So it's good to know that process.
Also good to know that folks like myself do that as well.
Okay, so when I drive over this oak, you are boosting, man.
Perfect.
Okay.
What does it mean to boost?
Well, sort of debugging that.
We don't need that anymore.
As cool as it was, we want to be changing our speed.
So currently we have move speed equals something and we're using move speed in here.
So we run over the boost.
When I say move, speed equals boost speed.
Okay.
So basically whatever is stored in boost speed is going to be shoved in to move speed.
So if we're currently storing 30, that will be what is shoved into or assigned into move speed.
So let's save back to unity.
We'll see if that actually worked.
Click on play.
Sometimes it's good to test these things with a really huge value because let's see, do I feel like
I'm going faster?
Yes, I felt like I was going a little bit faster.
And it's going to stay that speed now because I haven't done anything to stop it going that quick.
But that certainly feels like it's going 30.
How can we tell if I click on Cruise in my drive?
There's probably Move Speed somewhere.
Yeah, move.
Speed is now at 30, the same as boost speed.
If I click play then move.
Speed will return back to the 20 that it was excellent.
So we have created ourselves to speed up, but we want to make sure that we stop speeding up if we bump
into something like a crazy person.
So that's the other thing I'm going to do in here is going to be on collision, enter to DX, get rid
of private and just do a little bit of reformatting here.
My particular style, you don't need to follow it.
Okay, hit tab and then I'm going to what what I want to do in here.
Well, if I bump into something, it's just a simple case of saying move speed equals slow speed.
So we're going to assign the value of slow speed to become the value of move speed.
Save that.
And this is if we bump into anything at all.
So if you want to have it a little bit more specific, then you would need to use some tags or need
to use some other ways of doing that.
But I'm just going to say, if you're crazy enough to bump into anything, then you deserve to go a
little bit slower, click on play and make my windows a little bit bigger, I think.
Okay, did the dirt get a little bit of a run up here, bump into that loop and then bump into that?
You can see there's definitely a difference.
Now I'm going slow and bumping into everything.
Now I'm going fast.
Now going slow.
Oh yeah, cool.
So it's definitely working.
We've got a way that we can go faster when we hit a speedy thing and we've got a way that we can go
slower when we bump into any sort of collision and slow our vehicle.
Down again.
And also, you can see I'm green at the moment, so our package stuff still working.
And you can also see that I'm pretty bad at driving in my own game.
Okay, so that's the core functionality I wanted to implement in my game.
We can drive around a car, we can pick up packages, we can deliver the packages, we can change the
colour to give visual feedback, the packages disappear.
We can bump into things without collision.
We can trigger things to go quicker, we can bump into things to go slower.
And all in all, we've got a fun little game that you could build upon and take in a whole bunch of
different directions.
So that's all of the main content I wanted to cover in this section.
Great work.
I look forward to seeing what you've created.
Please do share with us what you've got and I'll see you in the next lecture.