In this lecture, we're going to use the add torque method to rotate our player.
And we're going to talk a little bit about linear drag and angular drag.
So let's get started.
We need to jump into our assets directory.
Right click, create ourselves a new folder which we will call scripts, double click into that and
then right click create a C-sharp script, which we will call player controller.
Now let's just have a look at what we need to do here.
Here's Barry clicked at the game object.
Sorry, the parent game object level.
If I click on a then I can rotate variance.
What I want to do, I want to be rotating.
And you can see I mentioned before that it's rotating around the inner pivot point.
We can finesse that later on.
If you want to feel like he's spinning more around his knees or his feet, then we can do that.
But at the moment he's kind of spinning in the middle and that's okay for the moment.
So you can see we want to apply this.
And in the past what we've played around with is just changing the transform rotation.
So just say make the rotation higher or lower on the Z axis or positive or negative, and that will
have it move around.
But that's a little bit clumsy because if we're saying, hey, move the rotation, move the transform
rotation, but he's touching the world, we can have weird artifacts where it doesn't quite work properly.
We've got the physics engine not playing nicely with us, just directly manipulating our game object
like this.
So I'm going to pop that back to zero.
Sorry for spinning you around, Barry.
What we need to do instead is apply a force.
So to use Unity's physics engine to apply a force and the force that is used for rotation is called
torque.
So that's the name of the sort of force that will have our guys spin around.
So let's open up our player controller script.
And in here we're going to add some talk to the character when the player pushes a key on the keyboard.
And the very first thing we need to think about is what is the torque being applied to?
Well, just back into unity again, I know I've nearly jumped into this script, but back into here,
we're applying the talk to the rigid body.
Rigid bodies are the things that make objects move and respond to physics in unity.
So first of all, we need to tap into this rigid body component.
And the way we do that within our start method in here, we're going to use our jet component.
If you're a jet component and we're going to get what sort of component, it'll be the rigid body,
make sure it's 2D and then our parentheses and semicolon.
So we're getting that, but getting it doesn't do much.
We need to store it in a variable so we can use it in another place.
So we will add a little space in here and we will store it in something called r b, so rigid body two
D and then equals.
And this is going to our red squiggly line because it's saying we haven't actually defined this as a
variable yet.
Now what I could do in START is to say, what is this?
This is of type rigid body 2D and I could declare my variable in the start method just here.
And our variable is called rigid body 2d.
However, I'm wanting to use this in update because in update I want to say if you push left, make
the rigid body apply torque in a left direction, if you push the right, apply in the right direction.
So if I start to type R by 2D in here, you can see we've got our red squiggly line.
And why is that?
Well, we're going to talk about this a lot more throughout this course, but this is a first taste
of the fact that certain things can be used in certain areas and not in others.
And it's a way for us to protect our code.
So here I can I can define a variable.
And if it's between these curly braces within the start method, that means I can only ever use this
and call this and modify this within my start method so it's local to within these curly braces.
And so when I try to use it down here within a totally different method, it's saying I don't know what
that is, doesn't exist in the current context.
So what we're doing within START is we are encapsulating this variable, so it only lives within the
START method.
So what do we do in this instance?
Well, you can see these curly prices up here, top and bottom.
This relates to all of the content contained within our class.
So if we want to make a class variable variable or a member variable, we could call it.
And up at the very top of our class we can say rigid body to DX and then we will call it RB to DX and
then semicolon and it's going to want to autocomplete semicolon.
I'm going to get rid of this one.
I've got down in update because we don't need it down there.
And you can see we don't have a squiggly line there and we don't have a squiggly line here.
And then I'm going to remove the type in front of my AB 2d because I don't need to define the type again.
I'm just saying I already know what the variable is.
The variable has been declared up to the top of our class and now I can use this anywhere in our class.
I can use it within update.
I can use it within a new method I create because it's a member variable and it is available to any
of our methods within this class.
So hopefully that explains a little bit of why we put some variables here, why we put some variables
there.
If you only need to use it within a method, there's no need to have it as a member variable.
But if you want to have it accessible in many places, then you do need to have it as a member variable
so that it is available anywhere within our class.
In update, let's create ourselves and if statement if what are we doing in here?
I'm going to use the old input system just because it's really quick and really easy.
I promise you we will use the new input system at some point throughout this course.
But all we need to say in here is if input dot get key and that's the method get key.
So we're just using the input class and we're saying within there get key.
We're trying to find if you've pushed a certain key on the keyboard and in parentheses here we need
to say which key we're interested in.
I'm going to say key code.
We're looking for the left arrow.
I think we'll do here.
First of all, so left arrow.
So this is a way of saying if we push the left key, then what do we want to do in here?
Well, this is where we need to apply our ad talk.
So what do we need to do?
First of all, we need to access the rigid body two D So what should we start with here?
Rigid body two D That means we're accessing Barry's rigid body to D dot and we can use the method in
here ad talk.
You can see right up here near the top, add talk and then you can see in the signature in here it's
looking for parameter.
Remember, parameters go within the parentheses, it's looking for a value that is a float and that
value is going to be the amount of talk we want to apply.
So we can add a value of, say, one in here, for example, and then put our semicolon.
Now it's pretty bad to have a magic number that's buried within our code.
So instead of doing one in here, let's create ourselves another member variable.
Remember, member variable variable can be used anywhere in our code, right up at the top here above
rigid body to DX that will be a serialized field and b of type.
What what sort of type is this variable we're making?
It will be a float.
Very good.
And we'll call this talk amount and then we'll initialize that at, I don't know, initialize it at
one just to give it a value, a starting value else, or else it will initialize at zero.
So instead of one in here, we'll type in talk amount and there we go.
We've created a way for us to apply torque, which is a force that rotates whenever we push the left
over arrow.
So I'm going to save that back over into unity.
There's one step we need to do for this to work.
Do you know what step that is?
We need to add our player controller to Barre Sung and a scroll down.
I'm going to drag my player controller script where we get the blue line just there.
Excellent.
And now when I click on play, if I hit the left arrow, then we should see some sort of torque being
applied.
None yet it might be that.
There we go.
It was just that he was going down the hill and the amount of torque wasn't strong enough to counteract
the physics through gravity.
So I'll put the talk amount up to ten.
We'll see how that goes.
I have a feeling he's just going to go flip de flip.
Yep.
Whoa.
There he.
Okay, well, that could be a game right there, but we've successfully applied torque.
When you push left on the left arrow, there's a lot of settings for us to tweak in here.
And let's put this back to three for now, I think I'm not exactly sure what we want in there, but
we're not yet at the stage where we want to tune it just yet.
But I've got a challenge for you at this stage.
The challenge is to finish our rotation when the player pushes the right arrow, make the character
rotate in the other direction.
So there you go.
A quick little challenge.
Pause the video.
Take that on now.
Okay.
So this one's pretty straightforward.
I'm going to copy my if statement that we've already put in there.
Pop that down below.
I'm going to change from left arrow to right arrow.
And instead of torque amount, this is the one tricky bit in the whole area.
Did you figure this out?
We're going to add negative torque amount.
We're going to have it go in the other direction.
You could have gone and said float talk about is this and you know rotate one direction is this the
other direction is that particularly if you want him to do a front flip quicker than a back flip, but
I want it to be the same.
I don't mind that it's going to say negative torque amount.
What I'm also going to do in here, we've got two if statements.
So if you're doing this, great.
If you're doing that, what happens if you're pushing both of the arrows?
That's going to be a bit of a conflict.
So let's do something that says you can't do that.
And the way we're going to do that is to add an else.
If so, if you're pushing this, do that else.
If so, in other words, if you're not already pushing this first if statement the first left key,
that's what else means ls means otherwise.
Then if you're pushing the right arrow, do this.
That just is a way for us to make sure we're not accidentally doing both arrows at the same time and
getting some weird sort of conflict.
So I'm going to save click on play.
We'll see what happens here.
When I push left, arrow goes rotate, the left right arrow rotates to the right.
Excellent.
Although if I push left arrow and then let go, ooh, he's doing lots and lots of spinning.
So let's look at that briefly what's going on here?
If you look at Barre's rigid body, you'll see we've got a couple of values in here.
Mass linear drag, angular drag and gravity scale.
Now, the angular drag is going to relate to how much friction there is when we spin around and the
linear drag is the same information when we're going forwards and backwards.
So I have a little micro challenge for you here.
Tune the linear drag and angular drag of your character until it feels good, until it's not just spinning
out of control and getting into the death spin, I call it.
So you can see here, if I just hold down lift and get him spinning.
Okay, that's a little bit too quick for a snowboarder.
I think so.
Let's not have the death spin and let's have it that when you let go, he doesn't just keep spinning
and spinning and spinning like that.
So pause the video and play around with the tuning values of the linear drag and the angular drag and
see how you go.
Okay.
Welcome back.
Now, there's a really tricky thing going in here.
Our angular drag and our torque amount or the speed we're trying to make it turn.
Those two relate together.
So I'm going to start with my talk amount at say, eight, and then I'm going to jump up to my angular
drag and make that 15.
Now, how do I know those are the numbers?
Will I just put them in and play around with it and play around with it and just to see how it went?
They're not perfect, but they're are they're better than they were before.
And my linear drag I'm going to put at 0.5 for now, but that one will need to be balanced in conjunction
with our level sprite shape, speed on the effect.
So if the speed is ten, if we put this to say 20 back over onto Barre, if we put the linear drag at
a higher amount, those two are going to relate.
So if you have a much higher linear drag, you might need to have a higher speed on your effective.
But let's see how my tuning values are going now.
I've got those things set up.
I'm going down the hill.
Not too bad.
That speeds a little bit too slow.
But I'm not getting into the death spin.
I'm spinning at a fairly consistent amount, which is what I like.
I just don't want it to speed up or slow down.
I want it to be just a nice, steady, consistent spin.
Okay, so I think I need to make him spin a little bit quicker.
So I'm going to put this up to ten and see if that's just a little bit nicer.
When it comes to spinning and not super out of control looks pretty quick.
Go up the hill.
That's pretty good.
That's roughly what I'm looking for here with my spin.
So there we go.
We've got some general values.
We're in the ballpark.
And you've learned a little bit about the angular drag and linear drag and the torque amount we've set
ourselves up so that we're using the add torque method to spin around our snowboarder.
Great stuff.
I'll see you in the next lecture.