In this lecture we're using foreign object of type to access our service effector to DX and to speed
up our player woo so we can make our jumps a lot more interesting by having a boost in our game.
So let's get started.
Let's start by opening up our player controller script.
Having a look in there, you can see that we've got a mechanism for getting a reference to a component
that's called get component we're using in here.
And this is when there's a component that is on the same game object.
So player controller is on our barrier game object.
You can see down here we've got our player controller.
So we use get component.
If we want to get any of the components we can see in here the rigid body or the transform, the capsule
collider or the other script we've got on there.
We can use get component and unity will know, aha, you're looking from within this same game object.
But what if we have something that is on a different game object?
So for example, here's Barry game object, here's our level sprite shape, different game object.
And on this other game object, our level sprite sheet, we have these surface effects are 2D.
So we want to be able to mess around with this.
We want to increase or decrease the speed to increase or decrease our character.
So the way that we can access that from one script or one game object, we can say find object of type
and find object of type is good if you've only got one of those things in your scene.
So we've only got one level sprite shape, which means we've only got one surface effector to DX.
This particular approach wouldn't work if we had many of these.
So if we're saying find object of type tree and we had a whole bunch of trees, then it would just go
and grab the first one that it would find and it wouldn't be so effective.
But we know in our scene we're only going to have one surface effector.
So let's jump into our player controller script and we'll create ourselves a new variable up the top
here, which will be of type surface effector to DX and we'll call it service effector to DX as a variable
small s, so type largest variable small s.
Now we need to say what's going to be stored in this variable.
And to do that we'll start with surface effector to DX equals and we'll use find object of type.
Now one thing you really need to be careful about is there's also a find objects of type.
That's if we want to define a whole bunch of things or all the particular things of that type, and
it has an S in there so that can trip you up.
So we just want find object of type.
We use our angle braces as we do for our get component.
And within here we're looking for the surface effector to dx parentheses semicolon that will now when
we click on play that will go and look for it'll snuffle around in our scene and say, Hey, I'm looking
for a surface effector to DX.
There it is right in there.
And this stage you might be saying, Well, what's the difference between a component and a type?
Because what we're looking for in here is fine object of type service effector to DX and components
usually are a type as well.
So when we add a component, it is a surface effect to 2D component.
It is a thing that is of type surface effector to DX.
That's how we can go and find it.
So likewise, if we're looking to find something that is one of our scripts, if we jump back to Barry,
if we're looking for a crash detector, we can look for the type of crash detector, because when we
create this as a class, it is also making it of type crash detector.
So with that logic, you could also look for something that is of type circle.
Collider 2D or of type capsule.
Collider 2D type, rigid body 2D.
Those things will work as well if you're looking for find object of type.
But for now we just want this service effector to DX.
Now what are we going to do with it?
Well, we want to be able to use this to speed it up and slow it down.
First of all, I'm just going to tidy up our code a little bit.
We've got this code in here that essentially is saying Rotate the player.
So I'm going to create that as a new method that we will call within.
Update.
So I'm going to highlight all the way from our if statement down to where our curly braces are lined
up.
I'm going to hit control period or control full stop on the keyboard and then extract method.
This will name it as new method.
So double click on new method F two and I'm going to rename this to B Rotate player.
So we don't have all these lines of code just sitting in update that makes update very messy.
We want to have readability at all times, so rotate player, it's popped in the private keyword.
I'm going to just remove the private keyword.
We don't need that at the moment.
We know that it's private.
Next up, I want to create a new method which I will call respond to Boost.
That's a way of saying we're going to boost the player.
Now we've got the red squiggly line because it doesn't exist.
So we could double click on it and then hit control period or control full stop and generate this new
method which we will do.
We don't want to extract, we want to generate a method.
So we click on that and it creates the structure for us with a new method we don't need private.
Once again, I've said that before and we've got respond to Boost.
And then in here it's just a line that says, hey, you haven't put anything in there yet, so it's
going to throw an exception just to remind us that we haven't put any code in, so we'll delete that.
Now you could have just gone and typed void respond to boost.
Curly brace.
You could do that if you want to.
You didn't have to go and do the little technique I showed you with the control full stop or control,
period.
Now I'm going to give you a little bit of framework in here and then challenge you to finish things
off.
I'll just put a comment which is saying, if we push up, then speed up, otherwise stay at normal speed.
That's what we're trying to do in here.
So that's going to require if statement, it's going to require our input.
It's a little bit similar to rotate player, but that's what we're trying to aim for here.
I'm going to give you a little bit of a hint with a couple of variables we create up at the top.
I'll pop this under our serialised field float talk amount.
I'll make a serialize field which will be of type float as well, and we'll name this boost speed and
I'll initialize that at I think 30 F and then we'll make ourselves another serialized field also of
Type Float and we will call this base speed and we'll initialize that at probably 20.
If base speed is well, that's how fast we want to go when we're not boosting and boost.
Speed is while you got it how fast we want to go when we are boosting.
So we've got all the structure in here.
The last little hint I'll give you is we need to access the surface effect two D and change its speed.
So there we go.
There's some clues for you.
So here's your challenge.
Make the player boost.
Finish our code so that you can boost.
In other words, speed up your player by increasing the speed of our effector when the player is pushing
up on the keyboard.
So you'll need an if statement and an else statement, you'll need to get the player input.
So you know when the player is pushing up on the keyboard and you'll need to change the speed of the
effector.
So this is your challenge.
Take that on C back here when you've given it your best shot.
Okay.
So we've got a few clues in here.
Let's start with the sort of shape we've got down below.
If we've got input, dot get key just as we're doing down below and key code dot up arrow think we'll
use.
So just as we've done down below, what do we want to do if that's the case.
Well, curly braces in this instance, we want to be modifying or changing something to do with our
service effector to D.
So I didn't give you all the clues on this, but if you go to your surface effect the two RD and have
a look here, you can see that there is a field which is called speed.
So we want to change the speed.
We want to do exactly as we're doing with our mouse and keyboard here.
But we do this in our code.
So our variable surface effect, the 2d dot and I believe there's a speed excellent.
That's going to equal what will it equal if we're pushing up, it will equal our boost speed, boost
speed semicolon.
I could have just had that equal throw in here so I could have gone 30 F like that or just 30.
But then if we want to change it, we have to come and find the exact spot in our code.
So by having it serialized, we can find that within our inspector, which makes life a lot easier.
So if we're pushing up, speed up, but if we just do that once we push up once we'll, you know, will
stay sped up.
So we need to say else, which is another way of saying otherwise.
And in this instance, we don't need to say else.
If we're just saying if you're doing that, do a thing.
Otherwise the default, the rest of the time, we want you to do something else so we don't need to
say ls if the player isn't pushing up arrow, we're just saying otherwise do this thing, prevent our
curly braces and we will set our surface effects, speed back to our base speed and then semicolon.
I'll just remove my notes got down here make sure that we're calling respond to boost within update.
Yes we are click on save back over into unity.
What I'm going to do just to test that this is working is I'm going to make my speed crazy fast.
I find that's an easier way as opposed to being like, am I sped up so I'm going to make it 100 in my
inspector here.
Let's click on play.
Cruising, cruising, cruising speed.
Whoa!
Woo!
I think we can conclude that that is working now.
We'll pop this back to 30 and we'll see if that's the right value for us to have.
We want to make sure that when we're pushing, we speed up.
When we stop pushing, we slow down again.
Yes, we are definitely doing that.
And does it help going over that jump?
Oh, that was so excellent.
That's exactly what I wanted.
So now you can have the player speed up and do some sweet jumps.
Woo!
And I think for me, I'm going to leave it that the player can just push speed whenever they want to.
They can boost whenever they want to, as opposed to having some sort of time or or some sort of amount
like boost amount.
But this is something as you get more confident, you can come back to this project and you can put
in all sorts of mechanisms.
You can have some visualization on your screen of how much boost you've got left.
And when you boost, then it decreases.
So there's a lot you can do with this.
You can push yourself, you can challenge yourself to take it further.
But for now, I'm pretty happy with this implementation where we've used find objective type to access
the service effector, which is on a different game object from the game object that we're listening
to, the input from our player so that we go, That's all for this lecture.
I will see you in the next one.