OK, so we've got our horizontal rotation in place that's now make it so we can look up and down at
the same time.
So let's go back in here and to go back into my scripts so we have a rotation applied to our main transform.
The next thing we want to do is apply a rotation to our viewpoint.
So as I said, when we create our values up here, we're going to use this rotation store to store whatever
rotation we want to apply to our player.
And let's take a look at why we want to do it like that rather than just doing the same thing here of
just adding on the most input.
So what we want to do is let's just for the moment say we're going to apply the rotation to our viewpoint
because we know our viewpoint.
We want to control looking up and down if you go back in here to our viewpoint itself.
If we rotate it so it looks up and down, you can see we're rotating on the x axis here, which is great.
So let's go back into history if we want to rotate on the x axis.
So let's just say here, viewpoint does rotation equals again, quaternion does oilor for.
Transformed assigner transform viewpoint to rotation, to angles that X, and if we add on the most
input dart, why does our vertical most movement so let's just drop there and then we'll do the same
thing as we did before and on a copy this whole base here and just say rotation or angles that y paste
again for that Z.
OK, so let's see what happens when we do this.
So if I go ahead and play here now, I can still look around left and right and I can look up and down.
At the moment the view is inverted, so we'll fix that in a moment.
But most importantly, if I look all the way down and keep looking down, I see if I look all the way
up and down like this, we can get weird things happening where if we look just enough, there we go.
Now, I flipped the whole camera around.
So we if you kind of rotate in different angles at just the right way, suddenly we're upside down,
which obviously is not great.
We don't want to allow our player to be able to do that.
So let's go back in here.
First thing I'm going to do is just as a quick demonstration.
If we switch this mouse input to B minus instead of plus, if I jump back in here, you'll see this
will actually allow us to invert things.
So if I go in here now, when I move the mess up, the camera moves up.
So depending on which way you prefer that movement, you can do add or minus will add an option to automatically
switch between those in a little bit.
But we're just going to handle actually doing our looking up and down before we worry too much about
that.
So what we can do is we can actually clamp the value of this here between maximum values.
So we could say we can look up sixty or we can look down sixty degrees.
Sixty degrees is a standard amount that you're actually able to look up in most games.
If you play many first person shooters in general, you're actually fine.
If I don't play the game here, generally speaking, you can never look straight up in the air like
that is not really something that you get to be able to do.
You can never really look straight down at the player's feet either.
You'll be able to look slightly ahead of the player.
So you be able to see the player's feet in the bottom of the screen, but you never actually be able
to look straight down.
So actually, sixty degrees is a pretty standard angle now, obviously.
Will you can you can you be able to add up to whatever you want, but we're going to go with sixty degrees
here.
So what we can do is say we can use a function called a mass clamp so we can say math f dot clamp.
And if we push an open bracket here and put this around these two values here and what a clamp does
is just says, hey, this can be at most between these two values.
So we're going to say here the angles that X minus the mouse push this whole thing.
If we put a little comma here, we can say the lowest it can be is minus 60 and the maximum it can be
is 60.
So this is how the math f clamp works.
It'll take whatever its value is and it'll say it can only be between these two values here.
And that is great.
That's absolutely fantastic.
That's what we would want that to be, both because we're dealing with quaternions which are a little
bit weird and transforming and translating them into oilor angles, which again is also weird.
There's some strange stuff that happens here.
So if we go and play here now, what we'll find is that we have a clamp.
We can look down and we can look up.
But when we look up a little bit, you'll see up as soon as we get basically as soon as we get below
zero.
If we look at the the rotation value over here, as I move it up, once it gets towards zero, as soon
as it gets anywhere near zero.
And as soon as he tries to go below zero, it steps to be back at 60.
So what's happening here?
Let's stop this running.
Let's go back in here.
If I remove the clamp again here just to see what's happening.
This is just a demonstration of problem solving within game development.
General.
And it's something you kind of just have to pick up and learn along the way, which is one of those
weird little quirks of unity in general.
So if I look up here now, you might think, oh, OK, so once I get above the line, my number value
goes to minus this.
And I want to say, hey, it should just be clamped to at the most going to minus 60.
But there's a weird quirk of quaternions and how all our angles work, which is if I go back into my
script here, I can say I can use something called a debug log to print simple information out to unity.
So I can say dbo that log here and I'm going to say, hey, I want it to print out for me what the owner
angle X is here.
So if I copy this and say I want you to debug log the other angle X and tell me every frame, what is
the other angle X at the moment.
So if I see this and go back in here.
And if I press play.
When I look downwards, you can see on in the bottom left.
I'm getting information sent to the council and it's saying that the angle is twenty seven point seven,
and if I look in my transform command and inspector, it also says twenty seven point seven.
OK, so that's great.
But if I move up here and go above it, my inspector, it says here, let's say minus ace, just about.
But if you look down the bottom left, it says 350.
So behind the scenes of unity and its in its internal systems, it's saying three hundred and sixty
minus the ace value.
So that's obviously not great, and that's why when we try to directly do that clamp on the on the rotation
angle, that's why we're getting that snapping effect happening, because it doesn't know how to do
that.
It's saying, hey, you're trying to up between minus 60.
And positive, sexy, but there is no.
It goes immediately from any zero value, it jumps back up to three hundred and sixty minus whatever
the value is, which is obviously greater than the 60 we were trying to clamp it in.
And that's why we get that weird kind of snapping and popping effect.
So to fix that, this is why we want to use the vertical rotations that are valuable.
And this is a whole.
Long way to explain what we want to do here, but it's best to see how these things work so you can
understand what's actually going on behind the scenes of unity.
So let's go and delete this debug now because we've understood what's happening.
If we go back here, what we're going to do is say our vertical rotation store.
Will be equal to whatever it currently is, so vertical rotations store, plus the most important dot
y.
Like, so we are semicolon and what we can actually do, just as a simple way to shorten what we have
to write, when you want to set something and you basically just want to add on a new value to what's
always saying, hey, take whatever our rotation store is at the moment and then just take it and add
on another value.
You can actually shorten that really easily by taking all this bit out here and just saying plus equals
so we can just say whatever our store currently is.
Add on the amount of movement we have on the Y axis.
And now with this value, because we're directly storing whatever the mass movement is, we can clamp
this value directly so we can say vertical store.
Equals math, f dot, clamp, clamp, whatever the vertical store value currently is between minus 60
and 60 like so so that this is the value that we're actually directly clamping.
And then, in our view, point of rotation here, we can say set this to be our vertical rotation store
so it doesn't matter whatever it currently is at the moment, we're directly setting us to be this value.
So if we if if this is minus 60 years, it will automatically, behind the scenes, convert that to
be the three hundred and sixty minus 60 by itself.
So we don't have to worry about that part.
So let's save that.
And if we go back in here.
Once I play the game now, we should see it's going to be going back to the inverted here, but that's
OK.
Now I can look up into the sky and it clumps at minus 60.
So you can see in my inspector on the top.
Right, we have that working correctly.
And if I look up, if I look downwards at the moment, we're clamped are looking straight down at the
ground.
So now we can never end up in that weird situation where things get flipped over in our game.
So perfect.
Our clumping is working absolutely correctly.
We now have our movement working the way we want.
But of course it's a little bit inverted at the moment.
So let's take a look at how to fix that.
And we've also got another little problem, which is that as we're moving and looking around here,
we've got our mouse floating off into different areas.
I've got multiple screens on my computer here.
So my mouse is floating off over to the side as I look, which obviously isn't great because you can
end up in a situation.
Obviously, when we start shooting things, we're going to be clicking to shoot.
And we don't want to go over here and click, for example, on another area of our unity, because that
will freeze up our game over here.
So we got to fix both of those things and we've got to do that in the next video.