Hello again.
In this lecture, we're doing something crazy.
We're sterilizing our variables so that we can move our steer speed left and right within the inspector.
So I can see how our game is going to look when we properly implement our steering.
So let's get started.
Making your variables available in the inspector will save you a lot of time and also help a lot if
you're working with someone on your team who maybe is wearing the design hat and doesn't want to dig
around in the code to see what's going on.
So what we're going to do is use something called serialised field.
So if you jump back into driver X and I find move speed, give myself a space before that, use the
open square braces and you can see that Visual Studio code is automatically added the closed saying,
I think you want to put something within here, I'm going to type in serialize field and you can see
I just type in C, R and then it knows what the remainder is.
You can click on that or hit tab or hit spacebar and that will complete serialize field and serialize
field is an example of an attribute.
This square braces, when we put it before a variable or a class or a method, shows that we're adding
some sort of attribute to the thing that is following it.
So Serialize Field is adding the attribute that allows us to serialize, in other words, right to disk
this particular variable.
And when we do that, we can access it in the inspector.
So I'm going to save jump back into unity and you should see give it a moment to think about things.
Make sure you clicked on your vehicle or your player you can see on the driver script.
Now we have move speed and the number in here will be whatever we initialized this at, which is cool.
Now I'm going to change the move speed to say 0.03 and then click on play and that should change my
speed.
So I'm moving quicker in this loop and then if I change it while I'm still in play mode to increase
it while it goes way up, the screen goes really far so you can change it while it's in motion, which
is pretty cool.
I'm going to leave it at 0.03.
Now, what I want to highlight here as well is as soon as you change this within the inspector, that
will be the new value.
Despite the fact when I go back here into my code, it's been initialized at 0.01.
This is being overwritten by the value that I'm saving to my disk, which says 0.03.
So sometimes that can get you a little bit of confusion where you're looking in your code and you're
saying, hang on a minute, 0.01, why is it going so fast?
As soon as you modify the value in the inspector, that becomes the overriding value and that's the
one it's going to be using.
Okay, so we've done that for move speed.
Let's do that for steer speed.
And this is time for a challenge for you serialize your steer speed using serialized field make steer
speed available in the inspector.
And then I've got another kind of quirky challenge for you here.
Drive your car using the inspector.
See if you can keep it on the screen for 10 seconds by modifying the value of the steer speed while
you are playing the game.
There you go.
Take on that challenge.
I'll see you back here when you're done.
Okay.
First step, we're going to put a serialized field in front of our steer speed.
So serialize field hit tab to autocomplete complete click on save.
Pretty straightforward to add that bit of code, jump back over into unity.
It'll think for a second we should now have a second value, which is my steer speed.
I'm going to pop my move speed down a little bit.
I'm going to cheat a bit here.
I'm going to move speed .01.
So we're going a little bit slower now.
Click on play.
I'm going to get ready.
Oversteer speed, it's doing some left so I'm going to turn it to increase it.
Now I need to decrease it so it doesn't turn as much and then back the other way.
That will turn it the other way.
Okay, so we've got the fundamentals of steering our vehicle is a little bit clunky and not exactly
how we'd want our player to play the game, but it is working and looks pretty cool.
It's a car driving game and we can do this by.
We're playing around with moving our steer speed in the inspector and obviously the next step is for
us to hook this up so the player can use keys on their keyboard because obviously the player is not
going to have access to the unity inspector when they're playing around.
But this is the fundamental we've got move speed to go faster or slower.
We've got steer speed to move left or right.
So there we go.
I'll see you in the next lecture.