Hey, welcome back, My favorite game developers in this video,
our gun is no longer a static object.
If you look closely,
you can see that when we move down,
the gun moves down.
When we look up,
the gun locks up.
Obviously, there are still a couple of things that need to be
fixed because holding the gun like that I feel is pretty uncomfortable,
but no worries about that.
This is the first step in naming our gun properly.
So let's not waste any more time and let's get started.
There's a lot of math in this.
So put your thinking caps on.
Now that we are moving and we're bumping into things,
it's time to start aiming our weapon.
To do that, we want to rotate the weapon around a certain position.
We don't want to actually be rotating the exact weapon because if we look in here,
if we go to the scene,
you can see right now,
if we try to rotate the gun as it is,
it doesn't look as good.
We need a point from which the gun will be rotating.
So what we'll do is we're going to right-click on the dome player.
We're going to create an empty game object.
We are going to call it the weapons arm.
And we're going to move it to a certain point.
And this is the point where the gun or from which the gun will be rotating.
So I'm going to put it around almost the hand or the arm of the player.
And I am going to make the shotgun,
a child of that weapons are.
So now when we rotate the weapons arm,
you'll notice that the shotgun stays in its place almost around the arm right here,
and it looks much more natural as we rotate the shotgun or the weapons are.
So now it's time for a bit of cold.
But before we do that,
how are we actually going to be rotating this arm?
It's going to be dependent on where our mouth is.
So let's say we are up here.
We want the shot gun to point towards the mouse.
How are we going to calculate that?
Well, we will need a couple of points and let me explain it using math, yay math.
So let's get started.
First thing first we need the player position on the screen.
Well, I use the term player position,
but it's actually the point on which we are rotating the shotgun around.
But for now, let's just keep it simple.
Call this the player position,
and we have the mouse position on the screen.
So what we want is we want to get the vector on these two points.
This is the direction which are gone will be pointing.
But how are we actually going to calculate the angle we want the gun to be pointing in?
Well, we know the exposition and we know the y position.
Well, I meant X and Y.
So what we can do is we can use something called the arc tangent.
And this is something available for us in math.
F of unit is library.
So we can use that to calculate the arctan.
And if you don't know what arctan or you don't even know what Dan is
tangent I believe it's called it's the front side,
which is the y divided by V x.
And when we do that,
we get a certain number,
which is the tangent.
So to get the angle,
we use something called the arc tangent,
which is kind of a reverse.
But all you need to know is that we get the difference in the height,
we get the difference in the distance.
And using that, we calculate the angle in which argon should be pointing out.
This is basic elementary math.
I'm not sure if you're familiar with that.
If you want to delve deeper into it, be my guest,
I believe this will be enough explanation.
So let's translate these mathematical equations into mathematical codes.
So I'm going to go back and our script in here in the player controller.
And the first thing we need is the two reference for the camera and for the weapons are.
So I'm going to start off by creating a serialized field,
which will be a transform.
And it's going to represent the weapons are great.
The second variable we're going to create as a private camera,
and it's going to be the main camera.
And the cool thing about this is that we can immediately go into
Stuart and say that the main camera is equal to the camera.
Dot main.
And using this camera, that main,
we immediately can get a reference to the main camera which we have.
And here, great.
Now we need to know where our mouth is in the world.
So we are going to scroll down and here,
and I'm just going to remove these comments because we no longer need them.
If you feel you need them,
keep them as a reminder for later on how we changed and moved through our code.
So now I'm going to get the position of the mouse.
I'm going to use a vector three.
And it's going to be called the mouse position.
And it's going to be equal to the input dot mouse position.
And you can see right now that immediately the Visual Studio Community
gives us the current mouse position in pixel coordinates.
So great, that's what we need.
And obviously, I already know this stuff.
If you want to research this,
you would have needed to go into Google,
how to go get most position.
You have gotten the documentations for unity.
You would have found this input dot mouse position.
You would have used it.
This is just a shortcut.
Let's continue on. Next thing we need is the screen point.
So screen point, and it's going to be equal to the main camera,
the world camera, world to screen point.
And you can see the definition and here is
the transform position from world space and to screen space.
There we go. This is what we need.
Transform local scale, I mean local position.
And this will give us the local position of the transform,
which is the player right now.
And now it's time to calculate the angle.
Let's first of all, save all of that.
Let's go ahead and assign the arm for the player.
So I'm going to drag the weapons arm right now.
We're going to save that.
And now the next step is to actually calculate the angle.
So I'm going to first of all,
show you this again so you can go back and look at it if you need to.
And then I'm going to issue you a challenge.
And your challenge is going to be to calculate the angle.
So first thing you do is you need to subtract
the mouse x position from the screen point position of the x.
Obviously, you will need to do the same for the y.
And then you'll need to calculate
the math F dot arctangent to find that in the Unity documentation.
Go ahead, pause the video right now.
Go do your best.
Try it.
You won't lose anything by doing your best.
Pause the video right now and go do the challenge.
Hey, welcome back.
So as you can see,
I've just looked up math,
arc tangent in Unity.
Now I am right here.
What do I do? Well, I need to see how this works.
What does do? It returns a float.
This is our how I use matlab.org Tange.
Okay, great.
Then let's go back in here.
So first thing we need to do is we need to calculate the difference.
I'm going to put both of these into a vector two.
So I'm going to use a vector to,
I'll call this the offset.
So it's the offset between the two points.
It will be equal to a new vector two.
And in here I'm going to first of all get the mouse position dot x and subtract from it,
the screen point directs.
And I'm going to do the same on the why.
Now I'm going to create a float because as we saw this method,
the method dot arctan returns a float,
is going to be equal to math F dot arc tan or no, Eta.
It's called a tan two.
And I'm going to use the offset dot y divided by the offset DRX.
Great, so I'm going to save that.
But whenever we want to assign the actual rotation to the weapons are,
we cannot simply do it using a float.
What we need to do is we need to use an extra method,
which is the method that rad two degrees.
And you can see this converts radians to degrees constant.
So we will need to do that.
And the final thing we need to do is we will need to access the weapons on
dot rotation and now get ready for a bit of a scary world.
We are going to use quaternions.
So quaternion dot Bueller.
And we're going to set it as 00 on the x-axis.
We don't rotate on the y-axis, we don't rotate.
We only rotate around the z-axis using the angle.
Now, before you run away,
what is Turnitin dot Hulu?
If you look on the earlier right here,
you can see that it returns a rotation that rotate Z degrees around z axis,
x around x axis,
and so on and so forth.
So quaternion dot.
If you are interested,
you can delve deep into this.
Me personally, I've been rotating objects in Unity for
several years and I've never quite understood what they
mean by quaternion is just a way to convert
from the XYZ into an actual rotation and space.
So feel free to delve deep,
go look it up, make sure you understand everything.
If not, if you're not bothered by not fully
understanding what is happening with quaternion u dot.
Trust me, you can go through your game development journey without worrying about it.
So this is simply a way to convert x,
y, and z into an angle into a rotation.
We'll save that.
We'll go back into Unity.
And we are going to run the game.
And let's see what is happening with the gun right now,
we move up, the gun moves up.
We move down. The gun moves down.
If we go to the left, yep.
Our gun looks a bit weird when we look in the other direction,
but when it comes to the proper direction, there you go.
We move and even adjusts as we walk.
So you can see when we move up,
the gun moves down.
Always looking at exactly where our mouse position is.
So great now we have a gun that is actually firing,
which is amazing in my opinion, not actually firing,
which will be firing in the right direction later on as we move.
But obviously there is something wrong when
we try to move the mouse in the other direction,
it does move properly.
That's what we are going to be fixing in the next video while learning
something very nice encoding which is called the if statements and if conditions.
So I'll see you there.
But before we go,
always make sure to stage all your files.
Mega commits.
I'm going to call this made my gun point in
the right direction or not envy right angle.
Because in the next video,
we'll make it point in the right direction,
will make the whole player point and the right direction.
So, see you then.