Game Advanced Version Prototype version 1
Hello everyone
I'm Kim Hyunjin of FPS Game Development
FPS is a genre that plays the shooting game
from the perspective of a player character in a 3D space
Through FPS game development
We will actively utilize the 3D function
Implementing position movement, directional rotation, jump etc
In order to control character modeling
We will also take a look at animation application method
In this lesson, we will learn to calculate rotation value depending on the mouse movement
and rotate the game object
We will implement the function to follow around a particular object
And take a look at the difference between world space and local space
I will implement the movement of the character
based on the direction that the camera is facing
Project creation and setting, character rotation
Let's try the project creation and setting
Turn on the unity herb
Click on the new project
I will choose the template that says Universal 3D
And download the template
Until now, we used 3D Built-In Render Pipeline to
create the project
But we will use Universal 3D
For the lecture in the second half, which is about shader graph
We're creating the project with URP
URP is a function to respond to multi-platform
and expand the graphic function of unity
It is one of the function called
Scriptable Render Pipeline
In this stage, the URP
won't be explained in detail
The detailed explanation will be
done in the Shader lecture
The project name will be called FPS
Press the project creation button
Let's try to develop it
First, I will make a flat surface
In the Hierarchy tab, press the (+) button and 3D object
Next, choose Plane
I will name this Ground
Then, the scale value in the inspector
X value which is 10, then modify the Z value to 10
I have formed a wide surface
I will try to color the surface
I will make a folder in the project tab
Press the (+) button and choose a folder from the top
Let's name it Materials
Double click the folder to go in
Click on the (+) button and this time, material
Choose the material menu in the middle and create it
Let's name it MatGround
Select the MatGround which is created
Move to the inspector tab on the right
In the Base Map
Select a white color
Based on your preference
Choose a color that seems to be considered as the ground
Let's make it a dark brown color
When the setting is finished, from the hierarchy
Choose a game object called Ground
Mesh Renderer from the right inspector
Expand the Materials menu inside
And there is a material called Lit
This will be replaced with the MatGround that we have developed
Drag and drop
You can see that it has turned into a brown color
If you don't like the color, select MatGround again
Choose the bar with the color on the Base Map
and adjust it
Next, Applying to the player that we are manipulating
We will make a game object
Press on the (+) button from the hierarchy tab
This time, I will choose 3D Object - Capsule
I will name this Player
Next, the position value in the hierarchy transform will be
set as 0,1,0
In the middle of the screen like so
You can see the capsule has appeared
This is the basic setting
Now, the character that has been developed
I will rotate the Player
I will rotate it based on the movement of the mouse
Before rotating this player, the character
I will rotate the camera first
I will develop a script that
plays the role of rotating it
Select the Assets folder from the project tab
Press the (+) button and select the folder
Name it Scripts
Double click on the Scripts folder
And then press the (+) button again
Choose C# Script
I will name it CamRotate
If it has been made, the CamRotate script
will be dragged to the main camera and put together
Drag and drop to the main camera
When the main camera is selected
CameRotate has to be added like so
Double click and open up the script CamRotate
In the script, first
We will get input on the user's mouse
Secondly, based on the input value that has been set
We will decide the rotating direction
And then, we will rotate the camera to the rotating direction
In order to get the input of the user's mouse
A class called Input is used
Through a function called Input.GetAxis()
Open brackets and as a string
Mouse leave space X
Depending on the left and right movement of the mouse
The result of value changing could be achieved
If you bring the mouse over on the function called GetAxis
It says float at the very front
When a code is implemented, this function
ultimately returns a value called float
Therefore, it puts the value at the front
We need to get it
In order to get the data
We need something called a variable
Because the data type is a float
The variable to hold the data
should obviously be made in the float data type
Let's say float mouse_X
When the mouse is moving left and right
The Mouse X will receive a result
Same for when the mouse is moving up and down
There will be a function that operates in the same way
So in the variable called float mouse_Y
this value will be held
Input.GetAxis and then Mouse Y
Using the value of mouse_X and mouse_Y
We will choose the rotation direction for the object
First, the current angle value
will be held in a local variable
This variable called angle will be made to a Vector3 data type
Over here, bring what is attached to the script
Bring over the transform of the game object
Hey, transform bring over
the eulerAngles value that you have
Get the current rotation value
Then Hey, Angle, I'll modify your X value
How much? As much as the modified value of mouse_Y
The reason why we modify the x value of the angle
mouse_Y instead of mouse_X
There is a Hexagon object like so
Let's say this is the X-axis
And there is a Y-Axis facing upwards
How would it turn out if
we rotate the object around the center of this axis?
It would rotate as if it is nodding its head
When we are moving the mouse up and down
we have to make it rotate like so
Therefore the up and down movement of the mouse's value
should be held in mouse_Y
It is requesting for the object to rotate as much as mouse's y to the X-axis
Then, what would happen if the mouse is moved up and down?
The rotating value as if it is nodding will be set
In reverse, what if the mouse is moved left and right?
Instead of rotating as if nodding, how?
Around the center of this green axis like so
We should rotate it as if its shaking the head
So what is this?
The value should change based on the Y-axis
So the current angle value of the X
Rotate around the center of the X-axis
+= and multiply to mouse_Y
We will multiply angular velocity
200 multiplied by Time.deltaTime
We have set it so that it would move in the same way in any situation
Instead of writing down 200 right away
Making a global variable will be
more efficient
So I will make an angular velocity through global variable
It would be able to be modified anytime to public through the inspector
This data type is will be called float rotSpeed
The initial value will be set to 200
So it could be replaced instead of this 200
For this 200, let's modify this to rotSpeed
We have to make it rotate around the center of the Y-axis
So hey angle, out of the things you have
What are you going to do to the y value? You have to accumulate it
How? Multiply the angular velocity to mouse_X
And multiply Time.deltaTime
And bring over the current angle to angle
It would be modified due to mouse_X and mouse_Y
Re-set the modified value to the angle value
That is number 3
So hey current transform,
What am I going to do for your eulerAngles value?
I will choose angle value
But if we do this, and move the mouse up and down
It will turn over to the back
Therefore, for cases of rotating as if nodding up and down
I will regulate the angle
In conclusion, before rotating the camera in the rotation direction
Third, there needs to be a regulation for up and down rotation
I will regulate it from -80 to 80 degrees
Hey if angle, what if your x value becomes smaller than that?
What am I going to do if it gets smaller than -80?
I would fix the angle.x value to -80
If it has become -90 degrees by moving up and down
It would be smaller than -80, then what?
I would force it to be a -80 degrees
Next, this time let's do the else if
Hey angle, if your x value gets bigger than what?
What? If it gets bigger than 80 degrees?
The x value that the angle has
will be fixed to the value of 80
Let's leave it there and come back to the unity engine editor
I will try to play it
So if I lift the mouse upwards
The camera view goes downwards
And if I pull the mouse downwards, the view goes up
But it gets odd
If I lift up a little, the viewpoint strikes to the surface
The left and right is fine
For the first question, something is wrong with the direction
If the mouse is lifted upwards, it should be looking up
But the view is facing downwards
And if I pull the mouse downwards
the view should be downwards as well
But it suddenly changes while trying to look upwards
Since it moves up and down depending on the mouse_Y value
Through the usage of substitution instead of addition
We will try to make it operate in reverse
And let's check once again
The view heads upwards and downwards depending on the movement of the mouse
But it cannot cross this horizontal line
What happens if it is kept on lifted upwards?
It strikes down and faces down once again
The reason is because
Internally, unity does not use negative numbers for angles
Lets say the angle was 0,0,0
It faces upwards the moment the mouse is lifted up
If we lift it up, it becomes a negative number value
Let's say that it has become -10
Then the x value will be -10,0,0
It would have to rotate as much as -10 degrees
In unity, this is modified to
What is 360 - 10? It is 350
350 degrees and -10 degrees stand for the same value
So internally, it is modified to a value of 350
Then what happens?
The 350x gets caught in this condition
Then what?
It's forced to be modified into 80 degrees
It will lead to this phenomenon
Therefore, in order to solve this problem
Instead of modifying the original angle
We will have a variable that
calculates the angle from the beginning
Let's change it to that way
First, I will make a variable that can hold
the rotation value that is calculated by the global variable
I will make float mx = 0
And float my = 0 as well
I will come to the bottom
and erase this angle instead of bringing it over
Then the angle.x value will be reformed to mx
And what should the angle.y value be? my
We are accumulating it to the variables that we have made
And then change angle.x to mx
Next lastly, for number 4
Using this mx and my
We will set it with a new angle
So using the keyword 'new' instead of angle
I will create a new vector
Open brackets and the x value is mx, y value is my
Then z will be set with the value of 0
In this case, mx and my value
does not need to be modified to a positive number, when it is a negative number
Because it is being accumulated to
the place where we have made a global variable
What do we do with this accumulated value?
We are just setting newly into the angle
Let's save this and play once again
Now when it's lifted upwards, it is looking up at the sky
If it's moved to the bottom, the view moves down as well
And if it is moved upwards, it doesn't go up any more
It doesn't get bigger than 80 degrees
Same for the bottom, it doesn't turn over anymore
Instead, we can see that it is rotating well in 360 degrees
In the CamRotate script, we have
regulated the up and down rotation
This is setting the minimum and maximum value
It doesn't get smaller than the minimum value
And it is regulated to not get bigger than the maximum value
There is a function that plays this role
A function called Clamp inside a structure called Mathf
The first argument value inside the bracket is mx value
Then the second argument value is the minimum value of -80
Next, the third argument value is the maximum value of 80
For example, let's say the mx has become -90
Then it is smaller than the minimum value
So what happens to this?
It gets the value of -80
The mx value isn't the one changing
Whether it is smaller than then minimum value based on the mx value
or bigger than the maximum value has to be checked
If it is smaller than the minimum value, it returns the minimum value
For the opposite case, let's say the mx has become a 90
Then it is bigger than the maximum value
So what number does the Clamp function return?
It returns the value of 80
Now, it has become a 70
So it is not smaller than the minimum value and is not bigger than the maximum value
How much does the Clamp function return? It returns 70
This mx isn't the one being modified
So when it is smaller than the minimum value and bigger than maximum value
Where should the value that is given by this function be set?
It is overwriting the setting on the mx
The if condition can be organized into a single sentence
I will erase the top
This time, let's rotate the Player
The player will only be rotated
based on the left and right movement of the mouse
Select the Scripts folder
Press the (+) button on the project tab
Create a C# Script
Let's name this PlayerRotate
Check that this script is assembled into the
Player game object through a drag and drop
Double click on it to open
Make it equivalent to CamRotate
But you have to eliminate every rotation value about mx
Make sure it is only rotating left and right
First, you need a rotation velocity
Then, there needs to be a variable to save the rotation value
So public float rotSpeed = 200
Next, float my = 0 to save the rotation value
We have to receive the user input
Using the variable called float, mouse_X =
Hey Input, use the GetAxis function you have
Over here, leave a space between Mouse and X
Leaving a space is an essential
And then accumulate the rotation value to my
+= and then mouse_X multiplied by rotSpeed
Next, multiply Time.deltaTime which is the time value
The following step is for the rotation value called my
Set it to the Player game object
which PlayerRotate script is attached to
So hey transform,
What are we going to do to the eulerAngles value you have?
I made a new vector and finished the setting
0 for x, my for y, 0 for z
Let me check. If you press play
When you move left and right, you can see the camera is rotating
But you cannot see the Player rotating
Select Player from the hierarchy and press the right
Insert a cube from the 3D object
I will name this Eye
Bring it over using object focusing
Utilize scale tool to make it equivalent to what I have made
Hold the blue axis and bring it forwards
Make it so that it looks like
as if it is wearing sunglasses
It is expressed this way to show that it is the front side
Create a material
And I will put it in the sunglasses part
Select Materials folder from the project tab
Choose MatGround which has already been made
If you press Ctrl + D, it is copied
Name this MatEye
I will color it black since it is sunglasses
Select the color from the Base Map of inspector
I will color it black
You can change it to the color of your preference
Next, select Eye which is chosen as the child of the Player
In the Materials of Mesh Renderer, instead of Lit
Drag MatEye and drop
Then it would turn out as if it is wearing sunglasses
If you move the mouse to the left
The character rotates
as you can see from the scene view
You can see that the camera and the player is both rotating
In order to create the form of FPS
Locate it to the eye
or we have to make it locate to the head
Then if the player moves, make the camera follow along
We have to make sure it follows the Player around
So select Player game object, and press right
Create a new game object called Create Empty
I will name this as CamPosition
And the y value of the position in the inspector will be 1.2
In the location that is a little bit higher than the head
the camera will be following along
A script will be made
which makes the CamPosition follow along every second
Press the (+) button from the project tab and then C#Script
I will name this CamFollow
This game object called CamPosition
Who should be following this around?
The main camera has to follow it
So this CamFollow script that is currently made
has to be attached to the main camera
So drag CamFollow
and attach it to the main camera
Make sure that it is assembled
Double click CamFollow to open it up
It has to keep on following CamPosition
so it needs to be implemented in an updated version
The goal is to keep on following the target
What is the target? It is CamPosition
A variable that will hold the transform which is the target
So the public data type will be transform
The name of the variable will be target
What I will be inserting over here is
I will drag the transform of CamPosition and insert it in
Next, what happens after the update is
Hey transform, what will be my position?
Write down the code in the sense that
the position value of the transform which I have to follow, will be inserted
If this continues, the position value of the target
and the position value of the target will turn out how? It will become the same
If this target moves, it means the camera will be following
Come back to the editor
Select the main camera
If you look at CamFollow, the target will say None
What do we need to put in here?
Drag the CamPosition from the hierarcy
and it needs to be inserted here
I will try to play it in this case
Move it left and right like so
I will move the character in the scene view
If the Player is moved, you can see that
the camera icon will follow and move along
Character movement
Now, we will try to move the character
I will develop a script
In the Assets folder of the project tab
select the Scripts folder
Press the (+) button and choose C#Script
Name it to PlayerMove
If it is finished, from the hierarchy
choose Player game object
Drag and drop the PlayerMove that has just been made
And then double click to open up the PlayerMove script
I will get the input of the user
Decide the direction based on the value that has been input
And try to move it towards that direction
First, let's receive the input of the user
Over here, when W,A,S,D has been entered
the value will be held in this variable
And then the movement direction will be set based on the value that has been received
Next, move to the direction that has been decided in number 2
Let's receive W,A,S,D key input
First, I will receive the value for the key A and D
Create a variable called float h and over here
Hey input, based on the functions you have
It can be received through a function called GetAxis
Over here, parameter called Hotizontal
This time, I will receive the value of key W and S
In case of float v, hey input out of what you have
It can be received through GetAxis function once again
This time, the parameter is different, Vertical
If key A is pressed for variable h, then -1
then if key D is pressed, the value is +1
If nothing is pressed, it will be set to 0
For v, if W is pressed then +1 Next, if S is pressed then -1
If nothing is pressed, it will be set to 0
I will create a variable that will hold the movement direction
I will name it as dir of Vector3
Over here hey Vector 3, out of what you have
Value h will be multiplied to a value called right
Then, to + Vector3.forward value
v will be multiplied
The value inside Vector3.right is
1 for x, 0 for y, and 0 for z
I have multiplied h over here
Then this value will be
multiplying h for each x,y,and z
Then what happens? It will be h,0,0
For Vector3.forward multiplied by v
I will try to express it this way
For Vector3.forward, it is 0 for x as well as y
A value of 1 for z is held inside
If v is multiplied, as you can see in the bottom
You can multiply v for each argument
Then it would be 0,0,v
If you add h,0,0 and 0,0,v What is the result?
A value of h,0,v will be the result of the Vector
If we press key D
and insert value 1 for h
Then if v has not been pressed
So the value of 0 has been inserted
Then how will h,0,v be modified? It would be 1,0,0
What does this point at? It points toward the right side
So what next?
In number 3, the dir direction is in the right side
It leads to the result of it moving towards the right
In reverse, h has the value of 0
And v has the value of 1
How does this end up? It would be 0,0,1
This leads to the Vector that shows the front direction
This time, let's say h and v both has the value of 1
Then it would be 1,0,1
This would receive the result of addition between the right Vector and the front Vector
When these two Vectors are added
It leads to a diagonal direction which points to the right front direction
According to this calculation, the dir decides the direction
if the direction has been set, I will try to move in that direction
I will accumulate the position value of the transform and the current value
+= and dir multiplied by speed value
We have not set it to a variable
We will choose a random value of 7 and modify it later
Multiplied by Time.deltaTime
I will create a variable that holds the velocity value
The velocity which is public float moveSpeed
will be set to 7
The number 7 which is inside the update
will be replaced to moveSpeed
Make sure to save everything
Come back to the editor and try to play it
It moves forward when pressing W, it moves backward when pressing S
And moves right if D is pressed
You can also see that it moves left when pressing A
However, the odd part is
I will move the mouse and face the left side
On the top, I'm facing left
When I press W, it should be moving in this direction
I will press W
But now, it doesn't move in the direction of this red sign
In the scene view, on the right upper side
It's moving in the gizmo direction
If D is pressed, the red gizmo on the upper right
It moves toward the X-axis
There are problems like so
So we need to let it move to the direction that the Player is facing or
based on the direction that the camera is facing
We need to let it move after the direction has been set
As I mentioned before, according to the gizmo direction on the upper right side
The blue axis direction will be over here
Then, the red axis direction is here
This direction which is referred as the gizmo's red axis
It is the value of Vector3.right
Then the blue axis direction is
the value of Vector3.forward
This stands for the x as 0,0,1
And the Vector3.right is 1,0,0
So until now, we have made it move in the
direction of the gizmo which is placed on the upper right side
This is called world coordinates
The player is currently facing this side but
I will control it so that it would face the other side
Then if we press W
Instead of moving in the direction of Vector3.foward
It would move in the direction which the Player is facing
Next, if I press key D
It would have to move to the right
based on the direction that the Player is facing
So let's change the value like so
This is called relative coordinates
In Vector3.right, it includes the direction of the world
But now, based on the direction that the Player is facing
Instead of Vector3, it needs to be modified to
transform which starts with a lower case
Now, when pressing W and S, it moves well
I will rotate the camera to the left and press W
Like so, you can see that it rotates in the direction that it is facing
If key D is pressed
The right side which the Player is currently facing
If key A is pressed
Based on the direction which the Player is facing
You can see that it moves to the left
Because you cannot clearly see whether it is moving
I will insert a huge cube in the center
Press the (+) button from the hierarchy
Set Cube from the 3D object
I will name it as Wall
Then insert 0,0,0 for the location
Move it slightly to the front
Press the key R to change to the scale tool
Scale it properly
If you play again after finishing it
The rotation of the camera or the movement
will be easy to take a look at
When rotating the camera by moving the mouse
or when rotating the Player
It is able to be expressed visibly due to the wall
The movement is expressed visibly as well
First, it was Vector3.right and then it was Vector3.forward
When I calculated it into numbers
What did I say about the results it would lead to?
I mentioned that Vector called h,0,v will be received
So instead of calculating it like so
just create the Vector right away
and insert value h and v as value x and y
Let's change it that way
Make sure to comment it out
Over here in the bottom, create Vector3.dir once again
Now I will create a new Vector
h for the x value and 0 for the y value
Insert value v into z
In this case, it will be moved based on the direction
which is calculated as world coordinate system
It needs to be modified to the relative coordinates
Convert the direction of dir based on the main camera
Let's overwrite on the dir value
Hey main camera, use the functions in your transform
There is a function that changes
the dir world coordinate system to the relative coordinates of the camera
A function called TransformDirection
If dir is inserted as a parameter
The direction that is calculated from world coordinates
will be converted to a direction calculated based on the camera
Coming back to the editor and playing it
We can see that it operates in the same way
But there is another problem
The velocity is equal for moving front and back, left and right
But when moving diagonally
You can see that it moves in a faster velocity
This is a problem that occurs because
the moving Vector is larger when moving diagonally
So we will Normalize dir
Make the size into 1
The size of dir will be 1
It is called Normalization or Normalize
Hey dir, the Normalize that you have
operate the Normalize function which starts with a capital letter
The size of dir will be 1
Let's play it
The movement of right and left
the velocity of moving front and back is equal
Next, it is the same when moved diagonally
This time, when moving the player
We are changing the location by
accumulating the position value of the Player ourselves
But in the unity, there is a component that is specialized in characters
Using this component,
I will try to move the Player
Select the Player game objectt from the hierarchy
Erase Capsule Collider
Press the key on the right, Remove component or
Capsule Collider, there are three dots on the right
Press the three dots to Remove Component
Press the Add Component button from the inspector
Type in Character in the search engine
You can receive the research result of Character Controller
Add the component
This component has the collision area
as well as a moving function
So utilizing the character controller
I will try to move it
Come to PlayerMove script
The variable that will receive the character controller
The data type should be set as character controller
I will name it cc
Through the keyword called public
We can drag and drop from the inspector
The character controller is in the same line as
the PlayerMove that is attached
It can be found through codes
So in the Start function
Let's bring over character controller component
I will insert the value into cc
Using the function called GetComponent
Bring over character controller component
Then in the variable cc
The character controller is all brought over
Using cc, I will modify the move over here
The code that directly revised the position
has to be commented out
In the bottom, hey cc, out of the functions you have
There is a function called Move
Open brackets and over here
dir multiplied by moveSpeed and Time.deltaTime
will lead to movement
If you save it and play, it will move in the same way
And when moved forward, we are not able to pass through this cube
It even has the collision handling function
Now, let's do an overview of what we have studied
Project creation and setting, character rotation
Project creation and setting Ground object; Hierarchy > + > 3D Object > Plane
Player Object: Hierarchy > + > 3D Object > Capsule
Main Camera Object Rotation
Input of the user's mouse input.GetAxis(); Decide the rotation direction based on the input value
Angle regulation for up and down rotation Mathf.Clamp(); Camera rotation into the rotation direction
Player Object Rotation
Main Camera It is equal to the code that deleted the mx value in the object rotation script
First person perspective Holds transform of CamPosition in the variable target
Position value of the target and the camera becomes equal transform.position=target.position;
Character movement
Player movement Receive input of user input.GetAxis();
Set movement direction Vector3dir=newVector3(h,0,v)
Move: key W -> front, key S -> back, key D -> right, key A -> left
has a world coordinates, change Vector3 to relative coordinates using TransformDirection() function
Invoke Normalize() function in order to normalize the size of dir into 1
Player movement using the component Erase Capsule Collider of the Player
Click on Add Component and insert Character Controller
Operate movement and collision handling