How to guys.
All right.
So in this lecture what we're gonna do is focus on building or hooking up the engine for the helicopter
so we're gonna do is put the script onto the helicopter and we're gonna put in the meat of the code
that is actually processing the power that the engine produces.
All right so let's jump over into unity and get started.
OK.
So first thing that I want to do is I want to come over into the 22 group here and make a new empty
game object underneath it and just call this engines.
All right.
And what I'm going to do is put another empty game object underneath that and we'll just call this mean.
And now I'm doing this because I'm gonna basically put the helicopter's script the engine script onto
this particular game object over here and this script will basically be in charge of running the engine.
All right.
So what we need to do is we need to basically update this engine so that we were getting input from
the helicopter and we're getting that input from this helicopter controller over here.
So what we're gonna do is we got to pop up and the heli controller and we are going to want to put in
a few things so first things first.
I'm gonna put my head or back in here and we're gonna do helicopter properties all right inside of this
helicopter properties we're under neath this particular header.
I have this private input controller inputs but what I want to do is I want to create a new public list.
All right.
So a list is a type that holds a lot of these same type of class.
All right.
So in this case we want to hold a whole bunch of heli engines.
All right.
And we'll just call those engines.
Just initialize that to a new list.
All right.
So we can assign as many engines as we want to our helicopter controller and update them altogether.
OK.
So that's what we're doing at the hellions.
And so if we go into unity over here and we take a look at our controller script we should get another
property in here called engines.
And in this case it's a list of engines.
So we can add as many engines as we want so if you want to turn this into a drone let's say you have
four engines and I'm talking about a little RC drones sitting at four engines that you want to update
you could do that this way.
So it keeps it nice and flexible.
So in this case I just have the one engine and so what I need to do is I just need to drag and drop
the game object that has the engine script on it onto the label of the list and you can see it adds
to the list here.
All right.
So I'm going to hit apply to say save all that to the prefab.
So that's part of that prefab.
So anytime we drag this open so we can select the route prefab here and say select and it goes and finds
it in the folder here.
So now if I were to drag this out I have a helicopter with everything already setup appropriately.
Ok so next thing that we need to do now that we've got that is we need to update all the engines that
we have in our list.
OK.
So we had already started adding the code for that down here.
So whenever we call this handle physics function we're going to update the engines first.
So we're gonna handle all the engines.
So I'm going to come in here and I'm going to say for int i is equal to zero.
All right.
So there's a couple of ways you could do this.
I'm going to use the just the standard for loop.
You could also use it for you to leave but also here in a second but I want to show this first.
So I want to say in I.
And then i is less than the engine count.
All right.
We're gonna then iterate.
So we're going to add one two I every single time a loop through this.
OK.
So every single frame we're going to move through all the engines we're gonna update them.
So all we need to do is we need to say engines that are engines eyes we need that iterator dot update
engine.
And what we can do if you remember inside of our engine script here it takes the argument.
All right.
We need that throttle value and we're checking right here to see if we have input if we do that we're
going to update the engine.
So we're good there.
Which means now we can take inputs.
All right.
So using input throttle but you'll notice that throttle input isn't coming up in our list and that is
because input is actually the input controller.
So if I were to highlight this and then hit F 12 or my keyboard All right you can see that while we're
updating all of the the inputs OK so we're getting all the inputs and stuff like that and we're setting
it.
But what we're not doing is we're not getting any of the actual input from the current active input.
All right.
So what we need to do is we need to update this particular script right here to start getting the throttle
input from our current active input and to do that what we're gonna do is we're going to put this into
an update function as a void Update.
All right.
And what I want to do is I want to I want to say that we update a bunch of variables in here.
So we're going to open up the keyboard how the input script as well so f twelve again.
And so what I need are variables for throttle for collective cyclic pedal and that's it.
So if we go back to our controller our input controller over here we're gonna have to make a couple
more properties.
OK.
So let's create some private floats and we'll do throttle input here.
All right.
And then we'll do a public float throttle input.
Now I'm just keeping the names the same just to to stay consistent.
So we're gonna return our throttle input so already.
And let's just copy this a couple more times here just because we've seen this before.
So we don't necessarily need to watch me do this over and over again.
All right.
So I want to do this couple more times here and so we want to do the collective.
So let's just copy our names over here.
So I want to get it from the inputs over here so the collective input so I go we'll call this collective
input and then we'll just capitalize the C and we want to update the cyclic All righty
There we go.
And again we'll just update that first letter there to be capital.
And finally we want the pedal inputs.
So these guys right here.
All right.
So now what we can do is we can we can update based off of whichever one is selected so we're gonna
say f or we could actually do a switch to a switch because we haven't used that yet.
So we're gonna say switch on type all right.
So we have a type variable up here.
So input type.
So what I call it.
Okay.
So say input type.
All right.
And what we do is switch statements is we first check to see the variable of what is set to.
And then we can use all these cases.
So we set cases for these.
So is the input type dot keyboard.
Okay.
And basically what I want to do is then set all the inputs saying and say throttle input is equal to
key input that throttle input like so.
And we do do the collective.
So say collective input is the key input that collective All right.
So on and so forth.
So we then we have the cyclic input with the key input that cyclic input and then pedal input.
So now we have pedal input and we go.
Perfect.
So now we are all set up but we also need to make sure we add the break to the end of this.
That way the script knows exactly what to do.
And for some reason this guy is airing out
because cycling is effective too.
My apologies.
So we need to change the type here.
It's a vector too because it's got the forward and strafing motions.
So we have x and y for that particular variable.
All right.
So then we're just gonna copy this year and paste this down and we're gonna say if we are equal to our
x box then we are just gonna copy this variable here.
There's no need to type it.
Well we could just do this like so.
So when we get to the mobile controls it's really easy to update you know the input type.
But was it switch statements or we want to do is we want to put in a default break just in case we do
have a case as not met.
And just in case the input type is set to negative 1 for some odd reason.
All right.
One other thing we should also do before we leave the input controller here is just check to make sure
we have both those inputs.
It's just good practice that we got because if we don't have both these and we shouldn't.
Right now you know you could get a more fancy and do one check for each of these but I'm just gonna
keep like this for now and maybe refactor it a little bit later because this is going to work just fine.
All right so let's go back to the helicopter controllers script down where we're handling engines and
now we can do is get the throttle inputs.
All right.
And now it doesn't matter if we have the keyboard turned down or the X Box controller turned down we're
gonna just get throttle input for both.
All right.
And now what should happen is every frame we're going to update the attached engine which means we're
going to run this particular updated engine function and we're going to get our throttle input so we'll
debug log the current throttle value here.
All right.
Just to make sure that this is in fact all working so we'll go back to unity here.
And I'm just going to use the keyboard in this case and that the input controller to keyboard to my
console here and let's go try this out now making sure that we have a main engine assigned and there
we go.
So I'm using the plus or minus keys on the keyboard Excellent all right.
So we are now updating our engine perfect.
OK.
So that's the first step in getting all this to work.
Let's just comment this out with the control and then see.
All right.
So now we can close down our input controller and in the next lecture what we're gonna do is we're going
to update or add in the code here to actually calculate our current horsepower and our current APM.
OK.
Thanks so much.