What's up Game Developers?
CouchFerret here, and today we'll continue our game with Keyboard and Mouse Inputs to
be able to play the game without a Controller.
We'll restructure the code to handle Controller inputs together with Mouse and Keyboard inputs.
If it sounds fun, then stay with me, and consider subscribing with the bell icon so you won't
miss any future videos of this game.
Cool, let's begin!
Let's start with restructuring our code a bit because it looks kind of messy and it
is hard to extend it.
Let's have a new function called ProcessInputs() which will contain all the Input handling.
Then we need to make the movement and aim variables into global variables, so all functions
can access them, not just the one that declared them.
We need to remove the variable's type when we give it a new value, so it's not making
a new variable, just giving it a new value.
Our goal is to make all input handling in this ProcessInputs() function, so we access
the raw inputs only in one place.
To do this, let's move these two lines into our new function.
We need to call the ProcessInputs() in the Update() function
so our code works the same way as before.
And don't forget to delete the other line in the AimAndShoot() function.
We need to change the shootingDirection variable as well to use the aim variable instead of
accessing the inputs directly.
Now, we are done with the sticks, only the Fire button remains.
Let's create an isAiming global bool to know if we are currently holding down the fire
button or not.
Let's give it a value and use it as well.
The last thing we need is an endOfAiming variable similar to isAming.
This will be only true when we release the Fire button.
Okay, now all input handling is in one function, so we can confidently extend it later.
Let's clean up the Update() function a bit.
Let's move all the animator scripts into a new function called Animate(),
then also move the line that changes the position of our archer into a Move() function.
Cool, the Update() function looks so much simpler.
First, it processes the inputs and stores it into variables like the aim and movement
vector, then it handles the crosshair and shooting, and after that, it animates and
moves the Archer.
To be able to switch between controller and keyboard mouse inputs, we need to create a
useController variable.
Then, we have to create an if statement in the ProcessInputs() function so it changes
how it's handling the inputs based on the useController variable.
Let's move the existing code into it, and the else part of it will contain the keyboard
and mouse handling.
Let me quickly fix the AimAndShoot function, so it doesn't change the aim variable.
I need to move this multiplier so it doesn't change the aim's value and also move the Normalization
into the ProcessInputs() function where it belongs.
Okay, now we can work on the keyboard inputs.
Let's have a line which accesses the default Horizontal and Vertical Inputs that are based
on the arrow and WASD keys.
If we hit play now, we can move around!
Cool!
This was easy, but the mouse movement is a bit trickier.
We can easily press the joystick continuously to the right, but moving the mouse continuously
to right is tiresome.
So I'm going to make a cursor-like crosshair which cannot move too far from the player,
so it won't give an advantage to players who play with the mouse.
To do this, we need to store a current position of the cursor and add the mouse movement into
it in every frame.
Let's create a mouseMovement variable which represents the movement of the mouse within
the current frame.
We can access the mouse's movement by the default Mouse X and Mouse Y inputs.
By default the aim Vector3 variable is initialized with a (0,0,0) value so it starts at the center
of our player, which is perfect.
We need to add the mouseMovement into it so it changes slightly, based on which direction
we move the mouse.
By only normalizing it when its magnitude is greater than 1, we restrict the cursor
so it can't be more than 1 unit away from the Player.
If we had normalized it regardless of its magnitude, then it could only move around
the Player in the circumference of a unit circle, which wouldn't be the worst but wouldn't
be good either.
Now, we only need to give values to the isAiming and endOfAiming variables to have a functional
mouse input handling.
We can achieve this by using the default Fire1 input which is based on the left mouse button.
Let's test it!
It's working!
However, it's a bit too sensitive.
Let's go to the Project Settings/Input and decrease the sensitivity of both the Mouse
X and the Mouse Y to 0.01.
That's better!
Let's build the game so we can try it out in a full screen mode.
We need to go to the File/Build Settings and we have to click Add Open Scene so the current
scene will be played.
Let's hit Build and Run, and save it somewhere.
It looks pretty nice!
However, I've found a very interesting but game breaking bug.
If we move the cursor to the edges of the screen and hold down the left mouse button,
then we can't move the Archer.
I couldn't figure out exactly what is causing this issue, but we can just lock the cursor
to the middle of the screen so it can't reach the edges of the screen.
We can do that by changing the lockState of the Cursor to Locked.
We put this into the Awake() function, so it gets executed only once when the game initializes.
See, it stays in the middle.
We can even hide it as well, so it won't bother us.
We need to set the Cursor's visible parameter to false.
Let's try it out in the Editor!
This is pretty neat!
Because our Cursor is locked, we need to press the Escape key so it reappears and we can
Stop the game.
There's only one thing left that I wanted to fix today.
So the problem is that if we move diagonally with our character, then it moves almost 1.5
times faster than when we move only in one direction.
We need to do a similar thing that we did to the aim vector.
We have to Normalize it when it's bigger than 1 so its maximum value will be always 1.
We can put this piece of code after the if statement because it is needed no matter what
we are using to play the game.
Now the maximum diagonal speed is the same as the maximum horizontal speed.
This also eliminates the differences between different controllers.
For example, an Xbox 360's joystick moves in a circle shape while the Xbox One's moves
in a rounded rectangle.
Without this little fix, we would have given a huge advantage to players
who use Xbox One controllers.
That's it for today folks!
Next time, we will start drawing our first map so our Archer can have a world to live in.
We will probably draw walls and a few small details to make our game a little more interesting.
So be sure to subscribe and leave a thumbs up!
If you have any questions about what you have just seen, feel free to ask and I'll try to
answer all of them.
See you next time!
Không có nhận xét nào:
Đăng nhận xét