what is up guys fah here here from
awesometubes.com whoa haven't said that
in a while anyways before we start
creating anything i am going to preview
the game that we're gonna create so i'm
gonna hit the play button and you will
see now that we have our warriors that
they will fight each other don't mind
the buttocks that you can see from the
character so you see the enemies chasing
us and trying to attack us actually he
hit me pay attention to the health bars
at the top left and top right corner and
by the way i can hold the shield like
this when i hold the shield he is not
able to attack
and now when i lower the shield he will
be able to attack you can hear the
public
behind in the background let me just up
the volume a little bit
so you can hear them cheering
they like when blood is spit anyways i'm
going to attack the enemy and as you can
see i am dealing more damage because hey
i don't want to lose in the game that
i'm creating i don't want to embarrass
myself
and
when his health gets to zero basically i
have to hit him two more times so one
more time back you see now he is dead
and voila i have one and the enemy's
health is zero as you can see at the top
right corner
basically this is the game that we're
going to create we have melee battle
system something we did not do before we
have enemy ai that enemy can detect
where the player is and it will chase
that player also when the player goes
away a little from the enemy
the enemy starts chasing the player
again so let's start creating this i
mean a new project let me close this one
because i don't want it to be open
let me close it like this so right click
here and finally it's closed anyways
this is our project here we have our
sample thing this is a new 3d project by
the way so you're simply going to go and
create a new 3d project name it however
you want to name it i named this one
warrior fight game
this sample scene i am going to change
the gameplay just so
i like to name things correctly and we
are going to import the assets by the
way the assets and the complete project
are available for you to download link
is in the description below
so select all of these assets model
sounds textures and ui and simply drag
and drop them right here inside of unity
of course it will take a moment or two
for unity to load all of this you know
the procedure it takes some time to
import the 3d assets
and uh yeah while we wait i can sing to
you or we can stop the video let's
actually try the singing
i'm just kidding i'm gonna stop the
video and come back when everything is
finished
finally unity took mercy upon us and it
has imported all the assets and we can
start building our game the first thing
that i want to do is create the terrain
or the ground where we are going to
stand and for that i'm simply going to
right click here in the hierarchy panel
and go under 3d object and create a
simple cube voila this is our terrain
now of course not this cube we aren't
going to stand on a cube actually we are
but i'm going to resize it and i'm going
to call it ground
for the scale x i'm going to say 100
for scale y 0.1 because i want to make
it you know thin like this i don't want
to make it you know lar i don't know
what's opposite of tin don't judge me
anyways i'm going to say here z 100 as
well voila this is what we have this is
going to be our terrain now of course it
is not pretty at all i don't want this
whiteness tearing my eyes apart so if
you pay attention here inside of the
folders or the acids that we have
imported we have textures inside of
textures we only have this grass texture
if i open it this is the grass texture
basically this somebody took a shot in
his
backyard anyways what we're gonna do
here
is we are inside of the textures i'm
going to right click and create a new
material so here it is right click and
create so again it's right click
and go under create and material this is
what we want and i'm going to call this
one ground material
and we are going to apply it on the
ground so now i can drag and drop this
on the ground and this is how you make
game objects look cool basically because
pay attention to our ground it's white
if i put this material on it it's still
white though but what i can do for
example is i can change the color to now
red or dark or silver or whatever
but i can also drag and drop this grass
inside of this albedo or albedo and you
don't have my highlight tool here it is
so if i take here as you can see i have
the grass
and if i drag and drop it right here
it will display that grass
voila on our ground voila this is how
you make things look good in unity and
in 3d
but
there is more to these materials and
shaders now this is the first time i
mentioned shaders so what are shaders
basically a shader determines how that
material or the texture that is applied
to that material looks like so if i take
here pay attention i have the standard
shader this is on the drop down list
right here so here it is i'm
highlighting that with my highlight tool
so when you click on that it's a
drop-down list and you can play with all
the different shaders you have built in
of course there are others that you can
import from the asset store but these
are built in
what i like to use is this one unlit
texture and you see it's already a
little bit different it's a little bit
darker so the grass looks a little bit
darker in comparison to what it looked
like a moment ago and i encourage you to
experiment with these shaders so that
you can see their effects not every
shader will make your material look good
maybe some will look bad others will
look better but you can find that out if
you experiment so for example if i go
here i don't know nature and say speed
tree maybe this will not make it look
good well basically it's a little
lighter and some shaders also have
other fields here where you can add a
texture you can have a normal texture
now i'm not going to go into that that's
a different topic and i don't know too
much about it but basically when you
combine these textures like a normal map
another texture some other textures that
come along with it you make your
material look or the game object that
has that material look good anyways what
you need to do is simply experiment with
these just experiment try them out and
see different effects that you will get
and that's how you learn the best
now moving forward we have our ground
and again the scale x is 100 and z is
100 y is 0.1 why well because potential
if i set the y for example at 100 i
don't want this what the hell is this
you see we don't want this we only want
our ground so we want to make it look
like a ground that's why i put the scale
so that it's not too large on the y-axis
so moving forward if i go back in assets
and models we have the arena here inside
of the models folder so in the models
folder we have the arena if i click here
this is our arena if i drag and drop it
here it is this is the arena
now of course it's too small so we need
to resize it make sure it is positioned
at 0 0 0 for x y and z and i am going to
call it battle arena so this is the name
of the game object
and the scale for x y and z is going to
be 25 so 25 25 25 and voila this is our
arena it already has materials applied
on it
if you click on the drop down list you
will see all of these different game
objects
that are put together to make this arena
if i you know deactivate some of them so
if i let's try to activate these you see
the effect if i deactivate them well
they are gone so that is another thing
that you can experiment but basically
these are just 3d models put together so
put together to create this battle arena
you can see here we have some people's
spectating or whatever yeah this weeder
look at that weirdo yeah these two yeah
that probably up to something
anyways i don't know what i'm saying but
this is our battle arena now moving
forward we are going to import our
character and here he is our warrior now
one thing to note is this is a headless
warriors if i drag and drop him here and
if i go to him voila this is a headless
warrior like a headless swordsman but of
course we are going to make him
with head so i don't know how you say
head what's opposite of headless
head head on us i don't know anyways
this is our game object so what i'm
gonna do is
first of all i'm going to take and
rename it so from body underscore 0 i'm
going to rename it to warrior
next we need to apply this head on him
so if you pay attention to the head now
it's too big we are not gonna
we're not gonna do this to him
pay attention to the head look at that
we're not gonna do this to him we are
going to resize the head a little so i'm
going to resize it let's say here head
is going 1-1-1 actually no
so the head is going to be
1.5
1.5 and 1.5 yeah exactly so 1.5 for the
head this is totally fine
and we need to select the warrior
click on this drop down list to go
through all of the elements that make
him or to his body parts so click on the
warrior then this
bip001 click on that drop-down list and
again you have
vip001 pelvis click on that one and let
me just resize this here then we are
going to go under spine then spine1 then
spine two man this model is killing me
then we have the neck and right here we
have the head
so what you can also do is in the search
bar if i take the highlight tool here
you have the search bar in the hierarchy
panel you can also select the search bar
and you can type here for example head
and it will find it for you it's this
one right here bip001
head
so this is the part of the body that we
need so now we're gonna drag and drop
the head right here normally we don't
have to do this because
other models come with the head but this
model is like this
so what i'm gonna do is i am going to
resize it or actually reposition it so
i'm going to say zero point
this is going to be for the x for the y
it's going to be 0.012
and for the z it's going to be negative
zero point let's say here i don't gonna
leave it just at zero
just as y'all leave the z at zero
and i think his head is all it's also
still too big
so yeah this is okay and let's take here
and resize the head a little bit so the
scale is going to be 0.47
for x y and z i think now it's okay so
when you select the head set the scale
to 0.47
that is 0.47 for x y and z the position
x x is going to be negative 0.0274
and y is going to be
0.012
and yeah this is where our head is going
to be exactly and it looks good it looks
good actually it looks like it's already
built on him
anyways as i said we are not going to do
this
that often because most models come with
the head but this one is like this and
what we can do or
yeah anyways select the warrior now
and what i'm gonna do is i'm going to
click on this add component here and we
are going to filter for character
controller this is a built-in controller
that comes with unity so if i select it
here again it's under add component and
filter here character controller as i
said this is a built-in controller and i
don't know if you can see the green
things around him so yeah i've turned
off the ground in the battle arena so we
can see this capsule thing around him
basically this character controller
comes with a collider and also comes
with a
rigid body
in it so we don't have to apply these
things this green thing that you can see
is a collider that's around our player
so what we can do is we can
re-position it so i'm going to put it on
y 0.96
and for the x i'm going to leave it z
i'm going to leave it a radius however
is going to be 0.25
and voila you see the radius it will
resize that collider now it can be a
little lower i don't know for example
0.21 yeah and you can see now it is
around our player well around the middle
of him
and this is as i said the character
controller that we are going to use in
order to make him move and by the way
let me just put him like this so if i
put him like this and if i hit the play
button actually where is my camera he is
standing still now even though we have a
rigid body we have a velocity in our
character controller
we still need to apply gravity on him so
we need to apply gravity and in order to
apply gravity basically we do that vi
script
so it's time to get our hands dirty
let's go back in the assets and right
click here and create a new folder which
i'm going to call scripts
inside of the scripts folder we are
going to right click
and create our player move script and by
the way before we continue what i'm
going to do is go back here in assets
and right click and create a folder this
one is going to be our prefabs
and click here in the prefabs and drag
and drop the warrior so bam this is our
warrior prefab now so we don't have to
worry about
deleting this game object from the scene
and doing all of this from beginning
so if we go back in the script we have
the player move script drag and drop it
on the warrior
and now hit apply here so that that
change applies to the prefab now one
thing i want to get out right now i like
when i go
and spontaneously have things that i
need to explain why we need to hit apply
to make that change apply to the prefab
and you if you're watching my tutorials
you've probably heard me saying that
like a thousand times
well this prefab right here is basically
now the original game object of this
warrior that we have in the hierarchy so
let me just take it so this warrior here
in the prefabs folder is the original
one this one right here is a copy of it
any change we apply to this copy for
example if i go here and if i attach an
audio source component pay attention
here i have attached an audio source
component on the copy in the hierarchy
the original one the warrior here in the
prefabs folder he does not have that
audio source applied on him because we
attached that one on the copy
if you want to have the original have
this audio source because you attached
it you will need to click on this apply
and this apply
applies that to the original prefab
which is right here here it is the
original prefab in the prefabs folder so
if i select that warrior prefab in the
prefabs folder it has the audio source
component attached on him now but pay
attention now if i go here to warrior
and remove and this is the warrior in
the hierarchy the copy if i now remove
the audio source component the original
still has the source component the audio
because we did not hit apply
but pay attention to the difference now
if however i select the warrior inside
of the prefabs folder the original one
and if i go here for example and filter
for a rigid body component and if i
apply that rigid body component on the
original
what do you think for the copy it will
have the rigid body applied because we
applied it on the original
and the copy
will simply follow because it's a copy
of the original but if we apply to the
copy then we need to click apply to make
that change applied to that prefab the
original one as well so if i remove now
from the warrior prefab the original one
we also don't have it on the copy prefab
so i hope this clears things out if not
ask what is not clear in the comments
below so let me go here actually this is
from the previous one the one that i
have
previewed it so moving forward this is
the new one that we are going to code
from scratch let me just click here and
hold enter to give a little bit space so
what do we need for our player move
first of all we need our private
character controller and i'm going to
call it char
controller like this not character but
char like this
short for character
we will also need an animator but we
will do that later on we also need here
public float movement
speed which is by default three and pay
attention i set it to be public which
means it is editable in the inspector
panel and what do i mean by this when i
say it is editable in the expect
inspector panel that means a public
variable is visible here in the
inspector pay attention here now you see
we have it right here let me click on
the highlight tool and here it is so we
have it it's right here which means we
can manipulate this value if i set here
value five now we have the value five
and not three so if i put it back at
three it is three basically the value
that we put here in the inspector panel
will override the value that is here
so we have the movement speed we also
have a public float gravity this is what
i was talking about which is 9.8
this is the gravity
in real life as well we also have a
public float rotation so rotation speed
which is going to be equal to 0.15 f
we also have a public float
a rotate
degrees per second which is going to be
equal to
180 degrees per second
so these are the variables that we need
to get started and the first thing that
i'm going to do is inside of the awake
function of course we have the start but
i'm going to change it to awake if you
are also following my tutorials you know
that i like to initialize things in the
awake function because the order of
execution goes like this when we run our
game this is the first function
that is called the second function that
is called is on enable this one right
here and this is the second function
and the third function is start
so start is the third function but by
default when we create a project we have
start and update of course we can use
start for initialization and in some
cases it is better to use start over
awake for example and i know somebody
will ask when is that case
well if you are initializing something
in the awake function and you need to
get a reference for that then initialize
it in the awake but get a reference in
the start function this goes for example
if you
select a player in the main menu will
you initialize the player in the
gameplay scene in awake but you need to
follow it with the camera then in the
camera
use the code that will find the player
in the start function we will have
examples for this and some other
tutorials so don't worry about that
anyways i'm going to say here char
controller is equal to
get component character controller
and voila we are basically done with our
character controller what is the next
thing to do is go here
and create void move and this is a
function that we're going to use to move
which we're going to call in the update
and here we're going to have move
so what are we going to do here first of
all we are going to test if input
get axis and here we are going to say
vertical like this
is greater than 0
which means if we have either press the
up arrow key or the w key on our
keyboard because if i take here quickly
the highlight tool and this is the
coordinate system so here we have y here
we have x and here we have z because
this is a 3d game
and basically up on the y is plus down
is minus on the x axis to the right is
plus which means the positive side and
on the left is negative and for the z
forward is the positive and backwards is
the negative and basically here this we
are testing if we are touching the
vertical axis which is the y-axis that's
the vertical axis but we're going to use
this to move our character forward and
right here below we're going to say else
if input dot get axis and here we're
going to say vertical if that is less
than zero
then that means we're going downwards
and to paint a clear picture i'm going
to use here print
and i'm going to say the value is and
i'm going to say here plus and we're
going to use this input and vertical so
that you can see the output actually
of this what this value will be
so if i go back here in unity and let me
go into the console
and clear it so if i hit the play button
pay attention now as long as i'm not
pressing anything it will be zero but
when i start pressing so i started
pressing actually did not so now i'm
gonna start pressing and i'm pressing
the up arrow key
and this is what we have let me just go
here because it happened so fast it's in
the update function it
updates every frame in a second so pay
attention here this is where i started
to press and notice the value is 0.04
then
0.096 then 0.14
then 2 1 then 3 1 up to 1. so it goes
from 0 up to 1 in these increments that
we just saw so 0.1 then 0.15 then 0.2
sun and so forth you get my point and
for the negative if you press down that
is the s key or the down arrow key you
will get from zero to negative one and
you saw the increments it goes slowly
from zero up to one if you want to go
directly from zero to one then you will
use get axis raw
get axis raw has the same exact
effect it will detect when you click or
actually when you press the up arrow key
or the w key or
when you press the s key or the down
down arrow key but when you press the up
key and w it will return from zero to
one but it will go directly from zero to
one it will not go in these increments
that we just saw you can print this and
see for yourself now one other thing
that i don't like about this code is
this right here not the word vertical
but instead me hard coding values this
is something i don't like to do so if we
go here in unity and go back in the
project tab
inside of the scripts folder i'm going
to right click and create a new script
that i'm going to call tags so tags and
here i'm going to double click it we're
not going to attach it on any game
object and i am going to remove this
public class declaration
so what are we going to do why why this
why did this go right here so what are
we going to do with in our class well
first of all i'm going to create here a
public class
called axes
and here we're going to have a public
constant string and i'm going to call
this one vertical so vertical axis which
is going to be equal to vertical like
this
and we're also going to have a public
constant
string and this one is going to be
horizontal so horizontal
axis which is going to be equal to
horizontal as you can assume because
we're going to have vertical and
horizontal now don't mind these red
errors that i have this because my
computer was open
for such many time or for so long so i
have to close visual studio and reopen
it again
to make this actually work so now when
it's open bam here it is
so now everything is working like a
charm
as i said i don't like hard coding
values like here because if we use this
on two places or even more it can happen
that i misspelled something because if i
type vertical with lowercase v it's not
going to work even though the word is
the same so capitalization needs to be
the same and when we use it like this
when you when we declare it in a single
place we can simply do this we instead
of typing it like this we simply say
here
axis dot vertical axis and bam this is
it and here we do the same thing axis
that vertical axis and back it's the
same exact thing except here
we have it in one place and notice here
this constant constant will not change
same like in math now you don't have to
be imaginious to know that but basically
if i do something like this if i say
axis that vertical axis is equal to me
changing it it is not going to work
because it's a constant
anyways moving forward now we are
detecting if we touch
or if we press the up arrow key or the w
key and here if we press the down arrow
key and the s key so if we press the up
arrow key we want to move forward and in
order to do that we're going to see here
vector 3 move direction is going to be
equal to transform forward what the hell
is this transform forward well if i
hover over it says the blue axis of the
transform in the world space in order to
paint a clear picture if i take my
warrior here inside of the scene and i
zoom him on him do you see this blue
axis this blue
arrow that i'm taking and dragging him
this is that axis indicating the forward
so no matter where he is because we can
rotate him if i do something like this
and i rotate him you see now forward is
this way not the way where it was so
basically it was this way forward but
when we rotate him
you saw what i mean and this right here
that transform forward is telling us the
move direction is to move forward so now
we're simply going to say move direction
dot y minus equals gravity multiplied
with time dot delta time in order to
apply gravity this is what i was talking
about in the beginning so
when we attached actually the character
controller i said that we need to
manually apply gravity to our character
and this is how we do it this is how we
apply gravity by subtracting from y
gravity multiplied with time dot delta
time why time that delta time well
because
we are calling the move function in the
update and if you want to smooth out
things because update is called every
frame or actually once per frame if we
have 60 frames in a second that's 60
times in a single second this function
will be called which means this one will
be called as well because we are calling
it in the update now if we subtract from
y gravity only which is 9.8 that's a
very very it's going to happen very fast
it's a huge number and we are
subtracting that every frame 60 times in
a second that's why we need to multiply
it with time that delta time and time
dot delta time is the difference
between each frame so how many time it
took to go from last frame to the next
one that is the delta time and it's a
very low value and this why on that that
is the reason why we use it to
smooth things out
so moving forward we have here applying
our gravity the next thing to do is
simply call our character controller and
move in order to move our game object so
we pass here the move direction this is
where we want to move multiplied with
the movement speed
multiplied with the time dot delta time
to smooth things out again smoothing
things out with time dot delta time same
reason as what i explained right here
and this is we're moving
in this direction but we are multiplying
that with the speed movement in order to
speed up the movement
now what i'm going to do is here for our
negative or actually when it's lower
than zero that means we need to go
backwards so i'm simply going to copy
these lines of code and paste them right
here
and since we don't have transform
backwards
we have forward but we don't have
backward i'm simply going to use the
negative so i'm going to say minus
transform forward which is simply the
negative up forward what is what is
negative
forward it's backwards think of it as
opposites
so this is going to have the same effect
except backwards and here we are using
move direction which is now backwards
multiplying that with the movement speed
multiply that with the time dot delta
time and we can test it out so if i go
here and one thing another tip from me
take notes when i say these tips if you
want for example if this is where you
want your camera to look for example in
your scene or when you run the game
instead of using the camera and
positioning it i know i don't know it's
10 20 30 position for example and this
is where that position is or dragging it
and dropping it simply what you're gonna
do is position here the view how you
want it or where you want the camera to
look in the scene select the main camera
in the hierarchy and on mac hold shift
and command on windows it's control
command and press f so when you press f
so now pay attention where the camera is
exactly same place where we are looking
in our scene
now that we have that done i can hit the
play button and i can test this out so
we have the player in voila i'm pressing
the w key now i'm pressing the arrow key
up arrow key and w is moving forward i'm
pressing the back arrow key and now i'm
pressing the s key he is moving
backwards
so a lot this works
the next thing on our agenda is to
rotate our game object or the player so
let me just go here and this one is for
the move and here void rotate
and this one is for the rotate which we
are going to call right here in the move
so we're going to say a rotate like this
because
move is called in the update and we can
safely call rotate below it or you can
simply take the rotate outside and you
can call it here it does not matter it's
the exact same effect
so in the rotate we are going to say
here vector 3 which is the rotation
underscore direction by default i'm
going to say it's equal to vector 3 0.
this is the default one because
vector 3 0 is a shorthand for writing
vector 3 0 0 0 for x y and z because
we're going to detect input again we're
going to say if
input dot get axis and we're going to
see here x is horizontal axis if that
value is greater or actually if it's
lower than zero
and here we're going to say else if
input dot get axis and again here x is
that horizontal now here we're gonna
test if it's
greater than zero
and we're gonna do the following so if
it's lower than zero that means we are
pressing the a key
again this is the coordinate system this
is the horizontal axis meaning the
x-axis going to the right side is the
positive going to the left side
is the negative so if we press the a key
or the left arrow key we're going to the
left side which is negative which means
this right here will be lower than zero
if that is the case we're simply gonna
say here rotation direction is gonna be
equal to transform that
transform point or actually transform
direction and here we're gonna say
vector three dot left
now i'm simply going to copy this and
paste it here and you can assume that
here it's going to be vector3 dot right
and by the way this one is going to be
separate so we're not going to do
if else or else if and why why did he
give me this axis here anyway here i'm
going to say if input get axis
we are not going to use if else simply
we're going to do if and if
because
we can press these buttons
simultaneously so if we press the a key
or the d key they will cancel each other
out
anyways moving forward what is this
transform direction well if i hover over
it's gonna say transforms the direction
from local space to world space what is
that what is local space what is world
space
let's go back in unity to explain the
local space if i take the warrior
and of course i'm gonna go in the scene
first and zoom in on the warrior this is
my warrior game object if i right click
on him and create an empty game object
that i am going to tag here with this
orange or yellow tag you see this game
object right now inside of our so he's
basically inside of our game object our
hero if i move him here and now he is in
front of him if i take the x set it at
zero take the y set it at zero take the
z set it at zero now he is back at the
center and his center is around his
warrior player that he's his local axis
that is the local axis his own origin so
his own origin of that game object and
with origin i mean where that game
object goes when we set 0 0 0 for x y
and z or when we reset the position on
the transform where does he go that is
his origin or his local axes local
position that is if i go here you see
transform direction from local space to
world space
but what is the world space well world
space is all of this here you see
everything that we see here so where our
cube is are actually our ground where
our battle arena is where our player is
that is unity's world space and this is
where we are moving when we move this
character we are moving him in the
unity's world space that's where
everything happens so we are
transforming the direction here which is
vector 3 left
and if you go here a shorthand for
writing negative 1 for x 0 0 for y and z
and the right is positive 1 for x 0 for
x for y and z basically we are
transforming this into the world space
and this will transform it into the left
side of our game object inside of
world space and it will do the same
thing here for the right direction
so now that we have the input for the
left and the right direction we have
here the rotation direction right here
we are going to say if input dot or
actually now we need to
rotate our game object so we're going to
say if a rotation direction is not equal
to vector3.0
so if it's not equal to zero
that means that we have here input and
that's the reason why i'm setting it
here to be equal to zero because we are
resetting it every time
if we don't reset it res at it reset it
here then the value that we get here
will stay the same and here it will stay
the same every single time so we need to
basically reset it here to zero so
vector three zero but if it's not equal
to vector three zero that means we got
input here
so what we're gonna do is simply we're
gonna say transform a rotation e is
equal to quaternion dot rotate towards
and this will rotate from
rotation that is from the transform
rotation to rotation it's going to be
quaternion look rotation it's going to
be a rotation direction
in the given speed
and or actually degrees and the degrees
is going to going to be a rotate degrees
per second multiplied with time dot
delta time
what the hell is going on here teacher
what are you doing you are driving me
crazy and okay let me just calm down
calm down i'm gonna not drive drive you
crazy anymore anyways
you see here transform rotation is gonna
be equal to this rotate towards you see
here rotates a rotation from towards two
this is from
so it will rotate from this rotation
to this rotation in basically these or
this degrees but
this is clear it will rotate from the
current rotation it has towards this
rotation right here now look rotation
basically creates a rotation with a
specified forward and upward direction
basically this will do the following
since we are rotating around the y-axis
this is the y-axis and it will create a
rotation around the y-axis and the
forward will be
placed on the y-axis basically if the y
rotates over here so now y
is here then the forward will be here
you get my point now the reason for that
is because let me take our warrior
you see here pay attention pay attention
to the blue axis so this one right here
if i rotate him you see
you see how the blue axis rotates along
with him basically this is what we are
doing with look rotation it will look at
the rotation from the y-axis it will
basically rotate it from the y-axis
which is what we want we want to rotate
on the y-axis
and this is the degrees by which we are
rotating the game object so rotation
degrees per second that we added here
180 multiply that with the delta time to
smooth it out if we don't smooth it out
every frame we will rotate exactly 180
degrees and that will be fast
really fast so if you want to test it
out you can do it by simply doing this
right here basically let me just do it
just to test out and show you the
effects of the smooth time so if i go
here and if i run the game so pay
attention now if i rotate you see this
is what happens when you rotate like
this you see this is what happens
because we are rotating so fast
this is 180 degrees per second if we
don't multiply it with the delta time to
smooth it out
then you saw the effect exact same thing
will happen for this year if you don't
multiply without the time exact same
thing will happen here if you don't
multiply that with time dot delta time
but if i hit the play button now that we
are multiplying that with delta time pay
attention now i'm gonna press right
right actually so right you see how he's
rotating now smoothly and left and he's
rotating also smoothly so now we can
move and rotate so now our game object
can move and rotate at the same
time you see
moving and rotating and voila
so i'm pressing left and i'm pressing
right and he's rotating towards the
direction where i am pressing
voila this is how we move a character
and how we rotate a character so the
next thing to do is to make the camera
follow the player of course we also need
to animate him but using the camera to
follow him will be much easier because
there is less code involved so i'm going
to right click here and go on create and
see sharp script and i'm going to call
this one camera follow and i'm going to
show you two ways how can we make the
camera follow the player because this is
programming there is always more than
one
way to solve a problem so select the
main camera and drag and drop the camera
follow script on the main camera so drag
and drop the camera follow script on the
main camera i'm simply going to double
click it and open it here in visual
studio and let me just tag the class and
we don't need here that many lines of
code basically we need the following so
we need a private transform which i'm
going to call target
and this is going to be our player we
will get to it in a moment we also need
a private vector3 offset now
it's a private variable because i don't
want it to be accessible in other
classes but i also want it to be visible
here in the inspector panel because i
want to edit it so in order to do that
we simply open square bracket and type
serialize
serialize field and voila so open square
bracket close square bracket in between
type
serialize field because now
if i go back in unity that private
variable will be visible here in the
inspector panel bam and here it is
right here
and you can simply remove what did i do
so let's go back here and you can simply
let me just hide this
so you can simply remove this line of
code to test it out to see for yourself
if you remove it it will not be visible
in the inspector panel anymore
so moving forward from the awake
function not the start i'm gonna get the
target so i'm gonna say target is gonna
be equal to game object find the game
object with the tag player tag dot
transform because we're getting the
transform property again two things two
issues we have here first of all this
right here is a no no i don't like it so
we need to go back here in our tags and
we're gonna create a public class called
tags
and we're simply going to have a public
constant string
player underscore tag which is going to
be equal to player this is the first
thing and the first issue same as with
our axes and i explained that why
so if i go back here remove this
and we're gonna say tags dot player tag
it's the same exact effect now in order
for this to work you need to make sure
that your player the warrior when you
select him and right here you have the
tag property in the inspector panel you
see where i'm pointing with the mouse
you can click on it so let me just hide
here and you can click on it and you
need to select the player tag by default
i think it's from unity 5 version and
upwards
you have here
a tag player if by any chance you don't
have it you're gonna click here on add
tag so click on add tag and now here we
see tag and list is empty click on this
plus button so this small plus button
right here that i just pointed to
so when you click on it it will open
this short window you would you will
type here your tag
click save and then that tag will be
available to you
so uh now that you created a tag you're
simply gonna select the game object go
here and tag it so click on the tag you
want to tag it and pay attention please
please this is a tip so note it down
if the name here is player with p
capital case p it needs to be the same
exact name right here if you type here
lowercase p it will not work even though
it's the exact same word so
capitalization also needs to be in order
this is really important
so now we have a reference to our player
we have the offset here inside of our or
actually below the update we're gonna
create void
follow player and we are going to call
this function inside of the late update
function so no update but late update
function and what the hell is a late
update
well let me first explain
we have the update function
and we have the fixed update function
update function is called every frame in
a second basically if we have 60 frames
in a second
then it will be called 60 times update
function is good for moving a game
object using the transform property or
the character controller as we are
already doing and also for getting input
which we are also doing here so we're
getting input we do that in the update
function
fixed update function is not called
every frame but it's called in a fixed
number frame intervals what does that
mean it's basically called every third
or fourth frame and this right here is
dependent or that is dependent on if we
go under edit
and go here under project settings and
then time if you take a look here let me
just clear it with the pencil you see
here this fixed time step is set to 0.02
it's dependent on that time basically
fixed update function will be called
every 0.02
of a second
and it's good for physics calculations
so if you want to move your game object
using the rigid body you will do it in
the fixed update but what the heck now
is the late update well late update is
same as update it will be called every
frame but it will be called after the
update finishes so if we have something
like this for example i don't know int a
b and c and we want to say here c is
equal to a plus b
this calculation will be already done
and we can use the value of c in the
late update and it will be this value
here calculating a plus b and it will
give us the value c and we will be able
to use that in the late update late
update is really good for using code or
typing out code that will be
for for example like this in order to
follow the player because we are moving
the player as you can see here in the
update so in the update we will
calculate his position and he will
arrive at that position
so now in the late update we already
know where the player's position is and
we can follow him smoothly
now in order to follow the player
smoothly what we simply need to do is we
need to say transform that position our
own position of the camera
is equal to target
player so the target which is the player
transform as you can see right here and
we're going to use the player transform
to
transform direction or actually
transform the point and we're going to
transform the point off set so offset
and basically let me just go here i'm
going to name it offset
position not to be confused with that
wrapper offset it's offset position
what is transform point you see here
transforms position from local space to
world space again basically
same thing as what we did with transform
direction it will transform this point
from the local point of the target but
pay attention here pay attention please
we're using here target which is the
player's transform as you can see here
so it's the player's transform it will
transform that point
from the position of the player so from
the position of the player it will
transform that point
and it will transform it from his local
position to the world position and if we
test it out right now the beauty here is
that our main camera now has these
visible inspector variables for the
offset x y and z and what we can do when
i hit the play button you will see now
where the camera is it's basically here
but what i can do is i can move here the
y-axis i can move the z-axis you see now
and i can set it where exactly i want it
to be in order to follow our player and
i can set here zero for our y-axis and
basically
what i'm going to do is i'm going to set
this at 30.
so 30 for the camera for the rotation
and 0 for the rotation y
but if i hit the play button now you
will see that it will transform now that
position and what i can do is this
so voila
you see now this is how i can make the
camera follow the player now of course
here for these values i'm going to say
2.25 for the y
and for the z negative
1.9 or actually it can be a little
closer
something like this maybe negative one
this depends on you but now
pay attention now that we saved or
actually we did not save these values
because we are in the play mode
what i'm gonna do is click on this gear
icon you see the gear icon on the camera
so click on the gear icon and it will
open this list here and you're gonna
click on copy component
so when you copy the component now and
if i go back here you will see that the
values are back to zero because we edit
edited
those values inside of the gameplay mode
which means they will not be saved in
order to save it we did that gear icon
copy values so now that we have them
copied we're gonna see here paste
component values bam and voila so now we
have the values here negative one for z
2.25 for y and if we test this out right
now if i hit the play button you will
think that the camera is going to follow
the player which actually is the case
but if i rotate you'll see what is
happening
this is not the effect that we want to
achieve now we got carried away we
programmed the position of our camera
but we did not take the rotation into
consideration and i like to do things
like this i like to show what is the
problem and i like to show what is the
solution to the problem because we are
rotating our character we saw that so we
are rotating the character right here as
you can see and let me just move this
backwards so we are rotating the
character but we also need to rotate the
camera so here i need to say transform
that rotation is going to be equal to
target dot rotation simple like that
because our transform rotation is the
rotation of the camera
so transform referring to this class or
that game object that's holding this
class so transform and rotation is going
to be equal to the target rotation and
target that rotation is the player's
rotation so the rotation of the player
game object and now if we go back in
unity and if i hit the play button you
will see that when we rotate our player
we will also rotate you see rotating the
player rotating the camera and we can
move the z-axis let's say negative two
yeah negative two for the z-axis
remember that or actually just close the
game select the main camera and set
negative two for the z-axis so negative
two if i hit the play button again
now we move forward we rotate the camera
rotates along with the player
so this is how we do it we also set the
position and the rotation if we also
want to follow the rotation of the game
object
now is there another way to make the
camera follow the player a more simple
way of course it is because this is
programming this is creating an
application and there are multiple ways
always to solve a certain problem
this one is much simpler and for that
i'm simply going to duplicate the main
camera
and i'm going to set this main camera
turn off the first one the main one and
this one i'm going to rename to camera
in player so i'm going to rename it to
camera and player and i'm also going to
remove the camera follow script because
we don't need it so remove component so
remove the component camera follow
script
and what i'm gonna do is simply drag and
drop this camera on the warrior game
object and it will make it a child of
the game object so basically now
wherever we move if i hit the play
button because the parent is moving you
see now the child is moving as well and
the best thing here is you can position
the child wherever you want so we can
say the x position for the camera is
going to be 0 the y position 2.7 and the
z position can be negative let's say 1.8
so negative
1.8 will be the
z
position and rotation we can leave 30
for x only and basically this is how it
looks like so you can rotate it like
this however you want it but i'm gonna
set it at 30. again you can experiment
with this this is totally up to you
the more you try the more you will learn
i encourage you to try all the time so
experiment with it
now that we have this camera as a child
of our player game object i'm simply
going to hit play and when wherever the
player goes and however he rotates the
camera simply follows without a script
because
what the parent game object does the
child will follow and the warrior here
is the parent the top most parent all
game objects when you click on the drop
down list on the warrior all these game
objects are children of the warrior game
object and if we click here on the drop
down list for this vip00
these footsteps are children or a child
game object of this
vip001 so on and so forth you get my
point but they all are
the
child of the topmost parent which is the
warrior now we need to hit apply because
we attach this camera in the player but
again this is another solution depending
on your situation so sometimes you also
want to have the camera inside as a
child and it will follow the player
wherever
he goes and it's more simple basically
we did two ways one was using a script
and another one is simply making the
camera game object as a child and it
will follow the parent wherever the
parent goes the next thing to do is to
create the animations and we're gonna go
here in the models and here we have the
animation so right here i am going to
right click here where all these
animations are so right click and i'm
gonna create and i'm going to create an
animator controller so click here and
i'm going to call it
warrior controller
like this
because if i select the game object
which is our warrior you will see that
he already has an animator component
attached on him so here it is
and we simply need to drag and drop this
animator controller the one that we have
just created we need to drag and drop
him right here in this empty field and
then we can add animations to it and we
can control it vi code so if i take here
the warrior controller drag and drop it
here bam so this is the controller now
we have it attached on the animator of
the warrior the next thing to do is go
inside of the animator tab itself and
this is where we are going to create the
animations now if you don't have this
tab what you're gonna do
is the following go under window and
then you have here animation and here it
is animator tab so this one right here
so you have the animation you have the
animator
we want the anime tour so animator this
one right here now in order to create
these animations basically we don't need
to they are already created here but we
only need to connect them with the
controller so while you're in the
animator tab right here right click and
create state and empty so right click
create state empty voila this is going
to be the idle animation and in order to
rename this one you see the name is new
state if you want to rename it simply
click on it
and now here in the inspector panel you
will see here at the top let me just go
here and clear the console or actually
clear the pencil so here it is you see
the state this is where you will
rename it in here where it says motion
is where we are going to drag and drop
the motions or our animations so here
new state is going to be idle
and simply what we need to do here is
you see we have the idle zero and we
have the idle attack so if i select this
one this is that animation if i go here
and preview it so click the animation
basically clicking on the drop down list
for this idol and here we have idle
underscore zero now if i preview it so
if i zoom in on this preview here at my
bottom right corner and this is the idle
animation i'm not gonna preview the
whole animation because it's long so go
back here in animator click on idle and
here where i said that you need to drag
and drop that animation or motion
right next to it you have a little
circle let me show that circle it's
right here you see
when you click on that circle you will
be able to select these animations so
here we have idle zero and select the
idle zero and voila idle zero is not
here
why
well if we go here in the console it
says
animation clip idle zero cannot be used
in the state idol legacy animation clips
are not allowed in animator controller
what does that mean basically it means
the way how we import them so we need to
select all of these animations so select
from attack 0 to idle attack 0 and walk
select all of these animations
and right here now inside of the
inspector panel on my right side you see
here we have model tab we have the rig
animation material model so on and so
forth
click on the rig tab
in the rig tab we're going to change the
animation type from legacy to generic so
again click here generic click on the
drop down list where it says legacy
which is right here this right here you
see
when you click on that drop down list
you will change it from legacy to
generic and you will hit apply and bam
unity will you see like re-import these
assets with this animation type but it
will re-import these assets right here
only these animations and of course it
takes a little bit of time where i can
use it to sing no no
i had fun recording these videos man
anyways animation type will be generic
and you hit apply and you see unity
re-imports them and voila we are good to
go
so now select the idle animation click
on the circle and now select the idle
underscore zero animation it works here
is our idle animation i can even now
preview it if i select the warrior
inside of the hierarchy panel and if i
go into the animation tab so not
the animator tab but animation which is
located here window animation in
animation this is the animation tab so i
can click here the play button to
preview it and now we are previewing
the
we are basically previewing the
idle animation for our player c is like
moving this is all diagonal animation
everything here that you see is
basically the idle animation and why am
i
why am i actually
running the game anyways this is the
idol animation as you just saw
so let's go back and create the rest of
these animations so we have here the
idol we are also going to have the walk
animation so right click here create an
empty state and i'm going to call this
one walk
and i'm going to click on the little
circle and i am going to select the walk
animation
we are also going to have the attack
animation so right click here
empty state and this one is going to be
attack
one
and like this
we are also going to have another one
for the attack but let me just click
here on the circle and for the attack
one i'm going to select attack
underscore zero
and let me just preview it quickly so
attack underscore zero
yeah this is the one i want
and right click again and create an
empty and this one is going to be attack
two rename it to attack two and for the
state i'm going to select attack
underscore one but let me quickly
preview it here so attack underscore one
if i preview it yeah exactly these two
so underscore or attack underscore zero
and attack underscore one and the last
one is gonna be creed empty
and this one is going to be our defend
animation so defend like this and for
our defend animation i'm going to click
on the circle and
here we have counter attack damage
defense underscore zero so select the
defense underscore zero again if we
preview it
it will be this one you see he's like
moving his arm upwards to defend from
the attack
so now we have these animations
we
need to
now create a script and handle these
animations via script so when we move we
want to simulate or play that move
animation when we attack we want to play
the attack animation you get my point
but how can we do that
well in order to do that we first need
to create something that is called a
parameter so we need to create something
that is called a parameter and here
click on the plus sign so click on the
plus sign
and the first parameter that we're going
to create is going to be the walk
parameter and if you notice i clicked on
the plus sign and selected the bull so
i'm going to do it again so delete it
click on the plus sign and this one is
going to be a boolean we are going to
call him walk
so a boolean walk parameter
now the next parameter we're going to
click on the plus sign here and we're
going to create a trigger and this one
is going to be defend
and we're going to click on the plus
button here and create another trigger
this one is going to be attack one
click on the plus button trigger and
again attack but now two so attack one
and two walk defend attack one and
attack two
these are parameters because we need to
have transitions what are transitions if
i right click on the idle animation and
i click here make transition i can now
point on the walk animation and click on
it bam you see this white line going
from idle to walk that means this is a
transition from idle to walk animation
that means when we call this transition
in our code we will go from idle to walk
animation but this transition in order
for it to happen it needs to have a
condition and this condition
is right here when you click on this
line here this arrow when you click on
it it goes blue and you have the
properties here in the inspector panel
when you scroll a little bit down you
will see here conditions and it's a list
where we can click on this plus sign to
add a condition well the conditions are
going to be these right here these
parameters
so for example
if i click on the plus sign here and i'm
going to go from idle to walk
when the walk parameter is equal to true
and notice here in the drop down list i
can select true or false because when we
created this parameter it was a boolean
boolean has a value true or false
so we will go from idle to walk
animation when the walk parameter is or
has a value true and we will set that in
our code so we will set it in the code
for it to have the true
value and when that happens we will
transition from idle to walk animation
similarly right click on the walk
animation and make transition to the
idol and now we have two arrows one
pointing from idle to walk and another
from walk to idle so click on that arrow
and now i'm gonna click on the plus
button here and the walk here is gonna
be false because when walk is true we
are gonna walk when walk is false we are
gonna go back to the idle animation
now we are also going to go from our
idol to attack so click on idle make
transition to attack
and we're also going to make transition
from attack back to idle so click on
this
transition from idle to attack and the
condition here is going to be attack
simply this right here attack one
because we're gonna go from idle to
attack one it's a trigger we will
trigger that in our code we will see if
something is not clear right now wait
until we get to the code part then i
will have more material to explain it
more clearly to you we will see it
visually and it will be even more clear
so we are also going to go from idle to
attack 2 and make transition back so
click on the transition from idle to
attack to and the condition is going to
be
attacked you don't worry about the
transition from attack to idol we will
cover that so don't worry we will cover
that but basically
from attack 2 to idle the transition or
the parameter is gonna be when walk is
equal to false same here from attack one
to idle the transition is gonna be when
walk is equal to false because
why when walk is equal to false
because we're also going to have from
walk to attack one and from attack one
back to walk and this transition from
walk to attack one is also going to have
a attack one trigger parameter but this
parameter here when we want to go back
from attack to walk that's when the walk
will be equal to true and similarly here
so from attack
to back to walk and from walk to attack
two click on the transition from walk to
attack two click on the plus button
and here we're gonna select attack to
trigger
and from attack two back to the walk
walk will be true but we will do
something else here as well so don't
worry about that we will come to it we
are also going to go in our code and
then everything will be clear now we are
also going to go from idle to defend and
back from defend to idle so when we want
to go from idle to defend that's when
defend will be equal to true so when we
click here actually this defend
parameter excuse me we're going to
delete it it's going to be a boolean so
click here bool and defend is going to
be a boolean parameter not a trigger so
attacks are gonna be triggers but here
we're gonna go from idle to defend
when the defend parameter is equal to
true
and we are gonna go back
from defend to idol when defend is equal
to false
right here now we're also going to go
from walk
to the defend
and here we're going to set the
condition defense is going to be equal
to true but we're not gonna go back from
defend to walk we're only gonna be able
to go back from defense to idol
nation now this is for setting them up
we will see them again in action we will
see them in action don't worry about
that
now before we start coding because we're
gonna go here in assets and scripts and
i'm gonna right click here and i'm going
to create a new c sharp script and this
one is going to be character animations
this is where we are going to create our
animations so this is where the
animation magic is going to happen but
one thing to note is remember those
parameters that we used now let me
demonstrate that so here we're going to
have a private animator and i'm going to
call him anim
and in the awake function we are going
to get a reference to it so anim is
equal to get component animator
component voila
now this is where we are going to create
our animations and our first animation
i'm going to delete the update function
is going to be public void walk which
takes a bull so bull walk parameter and
in order to execute it we need to call
here anim set bool and that bull is
going to be our walk parameter and we're
going to set the value to this walk that
we pass right here so we're going to set
that walk
because we need to call it with the name
that's why we named it here if we go
here in the animator
and if we go into the parameters you see
the name is walk it's same as with find
game object with tags so the same with
tagging we need to have the name here
you see the walk it's capital w so also
here it needs to be capital w or
otherwise it will not work now this is
what i was talking about a minute ago
before we create these you see we have
these parameters and you know i hate
hard coding so we're going to go here in
tags now when i say i hate hard coding
there are there are some exceptions
where you will use hard coding like this
instead of creating it here but we will
see examples of that in some other
tutorial don't worry about that
so don't worry that you will never use
hard code but if you're going to use it
often it's better to put it here in a
single script so here i'm going to
create a public class that i'm going to
call animation tags so or actually not
animations animation tag with without s
here
and here we're going to have a public
constant strength this one is going to
be the walk parameter
and we're going to see here walk so the
value is going to be equal to walk
capital w so capital double u or
otherwise it will not work because the
names need to match up if here you have
a lowercase w so if here you have a
lowercase w then here as well you will
use the lowercase the point is the name
you put here
for the parameter it needs to be exactly
the same name you put right here
so we also have a public constant string
defense or defend
parameter which is going to be equal to
defend like this this is the value this
is the name and we're also going to have
a public constant
so it's not connection what am i doing
here const
string and this is going to be attack
underscore one underscore trigger
which is gonna be equal to attack one
like this
and public
so it's public constant string not
public it's public
and instead of using here attack
underscore 2 underscore trigger because
i want to differentiate them i think it
will be better if we put them like this
so it's attack
underscore trigger underscore 1
and attack underscore trigger underscore
two because i can see this one until
clearly when there are at the end of the
name one instead of when they are in the
middle of the variable name
so now we have everything basically and
we can go here in our character
animations here for our walk instead of
using this we're simply going to call
our animation tags dot and we're going
to call the walk parameter bam here it
is we are also going to have a public
void defend
animation and here are also going to
have a bull defend which is a parameter
that we're going to pass
and here we're simply going to call
animset bull and you call the
parameter by the name so let me explain
that so animation tags dot defend
parameter and here i'm going to call
defend and yes i notice here that i did
not type
parameter so i need to go back here in
tags and it's the default parameter
thank you very much
you see here i'm calling set bool
because when we created this parameter
we created it as a bull when i click on
the plus button and click bull it's
created as a bool so you will call set
bool if it's an integer you will call
set int if it's a float set float if
it's a trigger set trigger which we will
see right now so here we're also going
to have a public void
attack underscore
one
and here we're simply going to call anim
set trigger not target but set trigger
and simply here we're going to call
animation tags so animation tags dot
attack one trigger bam it's like that
you see set trigger
and for attack two i'm gonna duplicate
this and paste it and here instead of
attack one i'm gonna say attack two and
here instead of underscore one i'm going
to say under score two and basically
that is that bam we are done with our
animations
see this is a class and this is how i
like to do things i like to create a
separate class for everything and this
is how unity prefers it now this is not
my wisdom
i've heard that i don't know what was
the name will goatstein or or that allen
guy the guys who do official unity
tutorials i was watching one of their
tutorials and yes i watch tutorials
don't judge me i mean i'm saying don't
judge me but you should also watch other
tutorials as well
anyways
i heard him saying that unity prefers
that you have a separate class for every
separate task so i like to separate my
tasks so here this class will only
handle animations and now when we want
to call these animations for example the
move or the walk
we simply go here in the move script
and right here below our private
character controller we're going to say
private character animations and i'm
going to call this one player animations
or player animation how we want to say
and here in the awake i'm going to say
player animations is equal to get
component
character animations i'm calling it
character animations the class name
because we are also going to attach this
on our enemies now before i animate
anything i'm gonna go here in unity and
we need to attach that script on the
warrior if we don't attach it it will
not work so select the warrior click on
add component and filter for character
animations and click on
that script and hit apply so that that
change also applies to the prefab
so if we go back now in our visual
studio so if i go back here at the
bottom i'm going to create void animate
and this is going to animate the walk so
animate walk this is how i'm going to
name it
so animate walk
and how are we going to animate the walk
well basically i said that the move
direction
it has it is moving so it has velocity
we can test it out so we can call here
if
move actually i'm saying move direction
but it's actually a character
controller so excuse me for that one so
the character controller we're using it
to move
so we can test it out so we're going to
say here if our character
controller.velocity that square root
magnitude is not equal to zero
then we are going to animate we are
going to call playeranimations dot walk
is equal to true
else here we are going to call player
playeranimations.walk is equal to false
bam bam bam what is this why are you
bamming teacher i don't know why i'm
banning but what this here is
we have our character controller and we
are going to get the velocity you see
the velocity is the
velocity speed over time if we are
moving velocity will increase over time
only if we are standing still now this
is from physics i'm not i'm not no
physics guru i just know this from
looking online it's really very simple
so as i was saying
if we are moving velocity will increase
over time if we are standing still
velocity will not increase think of it
like you have a gas pedal in your car
and when you are pushing the gas pedal
to the end you know the car does not go
from one from zero to 100 in a second no
it goes slowly over time well that
slowly over time is the velocity
so if the velocity and the square root
magnitude you see this square root
magnitude and i need to do it like this
because for some reason visual studio is
giving me here
some errors i don't know warnings for
whatever reason but let me go here
character controller so char controller
dot velocity you see the current
relative velocity of the character
meaning if he's moving or not that the
square root magnitude it returns the
length of this vector because the square
root magnitude
basically this velocity is a vector
value a vector has a value of x y and z
and why that because we have speed on
the x y and z axis and it will return
that speed this square root magnitude
will make that
value one which basically means if that
value is not equal to zero we are moving
so if that value the velocity is square
root magnitude think of the square root
magnitude simply converting this value
into a one
so number one value
if
this velocity has any value if it's zero
then it will return zero the square root
magnet will return zero if we are
standing still if we are moving we will
have any value here we will convert that
value to one which will not be equal to
zero meaning we are moving meaning we
will animate the walk
meaning
if we are not moving then we will not
animate the walk instead we will animate
the idle animation before we proceed to
test this out we need to call the
animate walk in the update function so
animate walk function call it in the
update function
and now we are ready to test things out
so if i go back here in unity and if i
hit the play button
when i move we will see that we are not
animating now what is the reason pay
attention here to the idle animation
this blue line when this blue line
finishes now i'm gonna go to walk
animation and walk animation freezes
what the hell is going on teacher you
said that this right here will work and
why is it not working first of all let
me fix these animations so if i double
click on the idle animation here we need
to make sure that the loop time is
checked so when you select the idle if
you double click on it it will take you
here to the settings of the animation
and if you scroll a little bit down you
have this loop time which is right here
that i have circled right now
this loop time it needs to be checked
because the idle animation needs to loop
and when you check it hit apply at the
bottom left or excuse me bottom right
corner
also do the same thing for the walk
select it double click it
scroll down hit the loop time and hit
apply because again
the walk animation needs to loop it
needs to play over and over and over
again for us to be able to simulate the
walking and the idle animation the
defend animation here
so select it for the defend animation we
will leave it for later we will see what
we are going to do with it because we
are going to add some extra code for
that as well
so we will leave it for now these attack
animations are not going to loop but now
since these animations are looping if i
hit the play button pay attention here
in the animator you see animations are
moving you see this blue line it means
that the animation is playing but if i
try to move you see i'm moving
but you see all only now the animation
is playing
i don't know if you see this and you pay
attention here you see what's going on
you see here now what is the reason for
this
well the reason for this are these
transitions so we need to fix them if
you select the transition from idle to
walk animation you will notice here that
we have conditions we set the conditions
to be true that is okay
but you also have these settings here
and you have the exit time and pay
attention here this exit time
transitions has a fixed exit time and if
i hover over the exit time it will say
exit time and normalize time from
current state basically this means
this exit time is how much time we have
before we exit from the animation and
usually this means that we will let the
animation to play till the end which we
saw the blue line was
filling up in the idle animation and
only when the blue line has finished
filling up that's when we
transitioned to the walk animation so in
order to fix that we're gonna uncheck
the has exit time and the transition
duration we are going to see at point
one because we want to transition as
fast as we can
from our idle to the walk animation
now in regards to the walk animation
we're gonna do the same thing so click
here the transition from walk to idle
uncheck has exit time fixed duration is
gonna be equal to point one simple like
that
so what about transitions from idle to
attack basically we can test these two
out first so if i hit the play button
now
pay attention now i'm gonna hit or now
i'm gonna start walking you see as soon
as i started walking he started to walk
you see as soon as i started to walk and
here what we need to do is
er the transition or actually the
condition is false but i think in the
code
so else
player animations walk is equal to false
something weird is going on here because
the parameter here is okay it's false
and we saw that in the animation nothing
is wrong so let me try this i'm going to
print here and i'm going to say
the velocity is colon and i'm going to
say plus character controller or char
controller dot velocity dot square root
magnitude and i'm going to print that
out to see the value quickly so if i go
here let me just clear the console
and if i hit the play button we will see
that initially it's set to zero but now
i'm gonna start moving so now it's nine
nine points so on and so forth i'm
stopping so i stopped moving
whoa
never saw this before
you see i'm i don't move but for some
reason the character controller still
has velocity
now i'm really not sure what is going on
but let me try this
let me try to stop our character
controller instantly so basically here
we're checking if input so on and so
forth
if we have input we will move our
character controller else if we don't
have input so basically here if we don't
have any input to move the character
then we're simply going to say character
controller dot move and here we're going
to pass vector 3 0
again vector 3 0 is a shorthand for
writing 0 0 0 for x y and z
and basically it will stop the character
controller from moving so if i go back
here in unity and clear the console
and if i hit the play button
so if we go now and if i stop
then it stops you see we just need to
stop it but i'm not sure why it's not
stopping on its own i'm using the same
code from the project that i showed in
the beginning it's the exact same code
i'm following that it worked like a
charm and did not use this character
controller move zero but basically if
you have this issue this is how you will
fix it and if i get to bottom of this
issue i will create a video talking
about what the issue was it's really
interesting to see but this is how we
fixed it so yeah you need to improvise
in programming as well
so
this is how we stopped our character
controller to make sure that the square
root magnitude is equal to zero indeed
so what i'm gonna do again is test it
out so that we can see an action our
transition so
if i
walk now you see automatically he goes
from idle to walk i'm gonna stop
automatically he goes back from walk to
idle animation the reason for that is
because we edited these transitions so
we don't have the exit time transition
duration is zero basically exit time
will allow us
to finish the animation before we
transition to another animation and
transition duration is
how much seconds it will take for this
transition to happen so we set it point
one we want to happen as fast as
possible and we saw this in action so
now things are clear you see here
we go from idle to walk when the walk
parameter is equal to true this is what
we're doing here parameter walk is equal
to true
and if i go into character animations
and
set bool this is the walk parameter we
set it to this walk parameter that we
have here and we specified that walk to
be either true or false so if it's true
it will transition from idle to walk and
if it's false it will transition back
from walk to idle and we talked about
these
parameter
conditions or settings exit time fixed
duration so on and so forth we explain
what they are and what they did and so
now the animation is a little clearer if
not a lot more clear what we are doing
here now this is in regards to the idle
and the walk animation we of course have
these transitions that we need to fix
but i would rather first test it out and
then we will proceed to fix them so that
we can see it in action and make the
animation concept even more clear so for
that we need to go here in our project
and script folder and right click here
and we are going to create a c sharp
script that i'm going to call player
attack input and this is where we're
going to get the input for the attack
and we are going to proceed to attack so
of course we need to attach this on our
warrior character and i'm going to hit
apply
and now we're going to double click and
open this script in visual studio the
first thing that i'm going to do is tag
the class here at the bottom and give a
little bit of space so that i can scroll
up and down and we can see more clearly
what am i doing here so for now the one
thing that we need for our player attack
input is going to be our character
animations and i set it to be private
because we will use get component
and i'm going to name it player
animation and inside of the awake
function so change start to awake
we're going to say player animation is
equal to get component and we're going
to get here the character animations
component like this
so what is the next thing to do well the
next thing to do is to detect input so
we need to detect input in order to
simulate the attacks and we will
simulate them with the animation
so when we press our key code let's say
j and k so when we press j we will
defend ourselves and when we press k
we will attack so here if our input get
key and we're going to see here key code
dot j so when we press j we are gonna
defend ourselves so defend when j
pressed
and simply here we are going to call our
player animation so we're going to say
player animation
defend and here we're going to say true
because if you remember
if i go back here in unity and inside of
the animator component or actually in
the animator panel you see here defend
we will go from idle to defend when
defend is equal to true and we will go
from walk to defend when defend is equal
to true
now going back here we set it to be
equal to true which means that
when this is called we will play the
defend animation now this is when the
key is pressed
what we want to do when the key is
released well we want to see here if
input dot
get key
down and this is going to be key code so
key
code dot j
same key when we press it and when we
release it we are going to say here
player animation defend now is equal to
false we will test this out in a moment
we will see some issues and we will fix
them one by one because i like to first
show you the problem and then show you
how to get to bottom of that problem
and here we're also going to say if
input
get key and we're going to see here key
code k so when we press k
we are going to attack but we have two
attacks if you remember if i go back
here in unity you see here we have
attack two and we have attack one this
is in our animator panel so here they
are attack two and attack one so if i
select the warrior here and if i zoom in
on him you will see here that if i
select the attack one animation you see
now this is how he is attacking if i
select attack two
this is how he is attacking
so
how can we call these animations because
i want to randomize that and in order to
randomize it when we press our key code
k we're going to test here if our random
dot range from 0 to 2 if it's greater
than zero now to declare this right here
right away
or actually to explain it
you see this random range will return a
random value between two numbers when we
use integers if i hover over here you
see it's it is using integers if we say
here f and here f it will think that we
are using floats which you will see here
when i hover over random float as you
can see random range for a float but if
i remove f
and set it to be an integer or actually
just remove f it will think we have
integers why is this important because
when it comes to integers
this number here is inclusive and this
number here is exclusive what does that
mean that means that inside of this
random range it will include this number
but it will not include this number
which means basically here it will
return either zero or one
two will not be returned it will return
either zero or one and if we have here
for example five
it will return zero one two
three and four but it will not return
five so five is
excluded but if they are a float so if i
say here f so two f and zero f that
means we can either get zero one or two
or in between decimal point numbers
so when it comes to a float both numbers
are included when it comes to an integer
then the second number is not included
which means this will either return zero
or one which means 50 50 chance to
perform one attack or the second attack
so here i'm going to simply say
playeranimation.attack1
and here i'm going to say else and i'm
simply going to save attack animation
too so player animation and attack to
animation and voila we are good to go
and since this is already being called
in the update function we can safely
test it out so we need to go here in
unity
and pay attention now what is going to
happen so here i'm going to hit the play
button
pay attention to these animations so
let's say i press k
you see i pressed k
so what is happening first of all we
need to wait for our random integer to
pass and let me just go here quickly so
here i'm calling attack one and here i'm
calling attack two and in character
animations
so here animations attack trigger one
and here we have attack trigger two so
let's go back here again and what is
going on pay attention what is happening
here so i'm gonna press it and now we
have attack two but again we need to
wait for the idle animation to finish
because we did not check for these
transitions and now he's attacking and
you see how much it takes to play the
attack animation so we need to fix this
and of course in order to fix this we
are going to fix the transitions so the
transition from idle to attack one so
here it's not gonna have an exit time
and for the transition duration i'm
going to see here point three this is
from idle to attack one but when it
comes to attack one back to idle it's
gonna have an exit time
and here the transition duration i'm
going to say 0.1 so the transition
duration will be 0.1
but it will have an exit time with the
value of one
for our transition from idle to attack
two we are not going to have exit time
and point three will be that transition
duration
for
this transition from attack two back to
idle we are going to
check the exit time and the value will
be one and here the transition duration
will be equal to one now when it comes
from our attack or from walk to the
attack animation so this one right here
to attack one
uncheck exit time point three for the
transition duration
going back here from attack one to walk
we are not gonna have or actually exit
time is going to be one
and transition duration is going to be
equal to six
and similarly here from walk to attack
two we are not going to have an exit
time but 0.3 for the transition duration
and for our other transition
same as with from attack one to walk we
will use the same thing for attack two
to walk so exit time will be one and
transition duration will be 0.6
and for our defense so we are also going
to fix defend now because it will have
the same issue it will have the same
issue as we just saw so we are going to
see here uncheck exit time and point
three for our
transition duration we're gonna do the
same exact thing here from walk to
defend so uncheck exit time 0.3 for
transition duration and going back from
defend to the idle animation
one for the exit time 0.6 for the
transition duration
so now let's test it out and see what we
have here so if i go and test it out
again so pay attention now
did you see i don't know if you see here
that we are attacking with two we
attacked just two what is you see here i
just pressed the number i just pressed
it once you see i pressed the animation
only once as you can see here so again
but it's attacking multiple times
you see here now i'm not even pressing a
button now you're like okay teacher do
you know what you're doing i do know
what i'm doing but i want to show you so
let's go back here what is the problem
the problem is here get key
so we should not use get key because get
key
returns true while the user holds down
the key basically when we press and when
we hold the key now of course i did not
hold the key what i did is i simply
i simply pressed it but since this is
called in the update function which is
run
every frame in a second
it can happen that i held
unintentionally for two three frames
while i press the button it was
basically held in quotes
and those two three frames caught that
and it is called that many times so what
we need we only need to detect
when we press down the button and
basically for that we are going to say
get key down so you see here get key
down this is what we need and here also
we're gonna say get key down and here
we're gonna say get key up i wanted to
write it before but for some reason i
wrote down get key down will detect when
you press the button only once so when
you press it and even if you hold it it
will detect that only once get key up
it will detect
when you
release the pressed key we're gonna use
this when we press this right here we're
gonna hold the shield and when we
release it then we will release or lower
down the shield now that we are using
get key down let's go here and let's
test it out so if i hit here the
play button so if i hit the play button
pay attention now so if i press the key
you see now automatically only one time
it is executed and only one time we go
into the animation now one thing that
you're noticing is that the animation is
very slow so you see now he's like a
slow motion when he attacks you saw that
in order to fix that you're simply going
to select and let me just go here take
the highlight tool so you're gonna click
on the animation right here the motion
state that is and right here you have
the speed of that animation so it's
right here where i'm pointing and for
attack one we're gonna say the speed is
going to be four
and for attack two we're gonna say the
speed is three that means it will run
four times as faster than its original
speed and
this one the other one will run three
times faster than its original speed
basically so this is what we are saying
so now if i test it out so if i go here
and hit the play button
if i press you see now how fast it is so
it's playing these animations fast we
are transitioning from one animation to
another fast you see everything is
basically working now you see and you
can see here inside of the animator the
animations that are being played so when
i press here automatically it goes to
attack two when it finishes it go back
to idle so on and so forth when i press
the shield you see he is holding his
hand but i also release the button so if
i press it and hold it you see now he
also gets back
we need to fix this now in order for you
to understand what i just said we are
going to import the shield and the sword
so if i go here in models
and here we have our weapon so here is
our weapon this one right here did i
imported no for some reason i did not so
basically this is our weapon this right
here and the weapon i'm going to call it
sword
and we're going to click on the warrior
drop down list then vip object then
pelvis here then spine then spine one
spine two then neck
then a right clavicle upper arm i know
this is tedious and here is our right
hand this is where we are going to
position our shield on the right hand
what you can do is you can type our hand
and here it is so this one right here
the r hand
and you're going to select the sword
and for the position here x i'm going to
say negative 0.0389
for the position why i'm going to say
zero 0.0128
[Music]
and for the z i'm going to say 0.0034
now the rotation x is going to be
negative
negative 1 to seven
the rotation y is going to be equal to
negative 100 and the rotation z is going
to be negative one or four so negative
one hundred and four basically that is
that this is how we are going to hold
our sword as you see right here
now for the shield so we're going to go
back in models and now we have shields
and here it is and i'm going to rename
it to shield this one is going to be
positioned under the left hand so simply
type here l hand and here it is when i
say type here i mean right here in the
hierarchy search tab so this one right
here you simply type l hand
and here it is this sounds like some i
don't know some swordsman l hand anyways
take the shield and attach it here on
the l hand
and let me just reposition it so for the
x position is going to be 0.041
for the y it's going to be negative
0.008
for the z it's going to be negative
0.005
and the rotation
98 for the x
for the y 81
for the z it's going to be 180 and
basically that is that so this is our
shield and this is our sport again pause
the video here if you want to copy these
exact values where the shield is
positioned and as you can assume when i
was preparing this project i have
positioned this experimented moving it
up down left and right
until i got it to the place where or to
the position where i want it to be
and basically now we have our
shield and we have our sword now let me
select the sword and you can also pause
the video for the sword so if you want
to position it so this is where we can
position our sword
and voila this is it so now if i go and
test this out you will see this in
action way better so now we have the
sword if you see we are holding the
sword and if i press the key you see now
we are attacking with the sword we are
stabbing come on stab okay he's stabbed
and stabbed pay attention now if i hit
this for the shield you see he simply
holds it up and lowers it down if i hit
the j key and hold it you see again he
plays the animation and now you see what
is happening here the animation is not
working basically now the reason for
that is if i select the warrior and go
to the defense zero one animation this
is that animation this is how the full
animation looks like you see this right
here he basically
puts the shield up
and then he lowers it down in the same
animation what we need to do is we need
to freeze the animation where we are
somewhere around here so at the middle
approximately or frame i don't know
frame 30 frame whatever so basically
when we are here somewhere something
like this this is where we are going to
freeze
the animation so that it stops
and in order to do that we need to go
back in visual studio and inside of the
character animations right here at the
bottom below our attack too
we're going to create here void
freeze
animation and not free but freeze
so it's freeze animation like this
and here we're simply going to say
anime.speed is equal to zero now setting
the speed of the animator and anim is
our animator if we set the speed to zero
that means we will basically turn it off
shut it down it will not move the speed
is zero
and in order to turn it back or make it
work like normal so we're going to say
public void on freeze animation
and here since we are unfreezing we are
going to say anime speed is equal to 1
again so 0 and 1 is the speed for the
animation 0 stopping the animation
completely so stopping that animation
completely
and again for our freezing we are going
to set zero and voila we are good to go
now this freeze animation we are going
to call inside of this animation right
here we are going to use animation
events
now you see this animation that we have
imported so i'm gonna see here clear you
see this animation defense it's read
only if you pay attention here it says
read only read only means that we can
only display this animation we cannot
edit this animation in any way we cannot
delete these steps for the animation see
we cannot right click and remove
properties i cannot add any animation
events right here
and in order to do that what we need to
do is locate this animation so defense
zero here it is it's inside of the
models and animation
defense underscore zero and this is the
animation clip so we want the animation
clip which is inside of this game object
you see here that i've circled but this
is the clip that we want you see this
defense underscore zero what you're
gonna do is you're going to take this
clip
and you're going to hold command on mac
on windows you're going to hold control
and you're going to press d
this will duplicate it so now we have an
independent defense animation as you can
see right here we have an independent
defense animation what does it mean to
be independent well it means that our
animation is
not dependent on this one right here on
this defense that we have imported and
we're going to rename it to defense
animation
and now i'm also going to check this
loop time right here so pay attention
when you click on this defense animation
you will have a loop time here check it
because we want the loop time to be
checked
and if i go here in animator and select
the defend animation
what i'm going to do is change the
motion here for the clip so this right
here exactly where this is pointing so
this one i'm going to click on this
little circle right here
and filter for our defense animation so
click here
defense animation here it is
now because it's independent it's not
read only if i select it here it's not
read only anymore it's basically now we
can edit it so we can hit here the
recording button we can add more to this
animation so on and so forth but this
means that we can press
this button
notice this and actually not this right
here so clear you see this right here
that i've just circled inside of the
animation tab that is called an
animation event keyframe so i can go for
example here and let's say i don't know
at frame 20 exactly here
i want to add an animation event so i'm
going to click on that button that i
just showed you this one right here that
you see
so click on it when you click on it and
let me just hide the highlight tool so
when you click on it you will see now
that in the inspector panel first of all
you will see here at that frame you see
this blue line
that's the animation event and when you
click on it you see the properties here
in the inspector panel basically this is
a drop down list that we can click here
and we can select which one of these
functions we want to be executed when we
hit this frame
so when we hit this frame and go over
this animation event inside of this
animation
so i want to freeze the animation so i'm
going to click here and select freeze
animation so now when we play this
animation and we get to this point right
here it will hit or pass on this
animation event which has this function
here and this function is right here
inside of our character animation script
so it will execute this function
and in order to unfreeze the animation
we need to go in our attack input and
right here key code up
we need to call here
playeranimation.unfreeze animation in
order for this to work so here we are
going to unfreeze because inside of this
animation we are freezing it in our
animator does not work
so in order for this to work we need to
unfreeze the animation and make
the animator work like normal
so if i go here and hit the play button
pay attention now if i hold press and
hold the key code j you see now he is
holding you see now he is holding his
shield you see he is basically holding
if i release the button he releases the
shield and this is how we can see this
way better so i'm gonna go here inside
of the scene
and here is our player he's playing his
idol animation so pay attention on the
scene view i'm gonna press j you see he
is holding the shield and i'm pressing
the g key and holding it i'm currently
holding it and now i have released it
and he released that animation so
basically we simulated this with our
animation and with our code again what
we did is in our character animation we
froze that so we are calling freeze
animation which will be called at this
exact frame so defense animation at this
frame right here it will be called so
this is the frame right here and it will
be called at that frame it will go
inside of the script which has this
freeze animation function it will
execute here
and in our player attack when we release
the key so get key up defend when ga is
pressed down
and here we're going to say
release defense
when j is released
so when j key is released so when we
release it we unfreeze the animation and
set the defend animation to false to go
back to the idol animation
and now we can finally create our enemy
because we have all the ingredients
basically our enemy is going to be the
duplicate of our player so what i'm
gonna do here is duplicate the warrior
and i'm going to change him to so his
name actually to enemy so now we have
the enemy name here we are not gonna
have the character controller so you're
gonna remove that we are also going to
remove player move script and player
attack input the only thing that we are
going to leave here is the character
animations
now we are also going to have a box
collider so attach a box collider on the
enemy but what i'm gonna do is i'm going
to take the enemy and move him here
so right here and i'm going to rotate
him 180 degrees on the y so that he is
facing the player basically as you can
see right there the enemy is over there
you can see him
so now
let me first edit this boss collider
because it's not in place
so for the y position of the box
collider or the offset it's going to be
point 92 z we're not going to touch as
you can see i'm editing these right here
so the center for the whites point 92
the size x is going to be 0.28
the size y is going to be 1.81
and the size z is going to be 0.34
and voila this is our collider right now
i'm also going to take the character
animation and move it right here below
our box collider yes continue break the
prefab i don't care because
we're gonna go here into navigation now
what is this navigation what is this
right here
well this navigation is first of all you
can locate it here under window and then
a i and then navigation and probably
this tab will be floating like this so
it will be floating i'm gonna dock it
right here where my inspector panel is
and what you're going to do inside of
this navigation is that we are going to
bake our areas bake the objects that we
are basically going to use for
navigation so if i go on the fourth tab
here so pencil and clear you see the
four tab which is object when you select
that then you click on this right here
mesh renderers so select on that click
there
and you see all of these game objects so
we're going to select all of these draw
calls and the ground and we're not going
to select anything else so all of these
and the ground basically i'm going to
click here navigation static and i'm
going to say walkable but for these draw
calls let me select them and uncheck
that and navigate actually navigation
static but not walkable
so these are going to be not walkable
and the ground is going to be walkable
the word says itself so it's
self-explanatory basically walkable
means we can walk on it we will be able
to navigate on it
and non-walkable is none or not walkable
so all of these draw calls they're going
to be not walkable click first
navigation static and then not walkable
then click on the ground which is our
cube which is where our game objects are
standing which is our battlefield
and navigation static and then walkable
so when you do that this is how you
denote to unity's ai system on the
navigation
that we can walk on these areas
basically these are not walkable so it
will avoid them but these that are
walkable we can walk on them and i'm
going to go now in the bake tab right
next to the object tab we have the bake
tab
and simply click here big at the bottom
right corner when you click on that bam
unity will do its thing and you see here
these blue lines or
this is light blue or light green i
don't know what this color is so don't
judge me anyways
this what has this color this light blue
or green
this means that it is navigationable we
can walk on these but those that don't
have for example like this right here
this is not navigationable and we cannot
walk on these areas
why is this important so now i'm going
to go back in the inspector panel and
clear this t mesh from the search this
is important because now we can select
our enemy and i can attach another
component which is the nav mesh agent so
i'm going to click add component and nav
mesh agent attach it
remove the character script or actually
move it down
and this is our nav mesh agent now here
for the angular speed i'm going to set
it 800 this is basically the rotation
speed of the enemy how fast he will
rotate so this is the only thing that
i'm gonna change so how fast he will
rotate
now that we have the enemy and we have
baked the walkable areas we attached the
navmash agent i'm gonna go inside of the
scripts folder and right click here and
i'm going to create a new c sharp script
and this one is going to be our enemy
controller so i'm going to call it
enemy so come on enemy controller
and select the enemy game object itself
so select it and drag and drop the enemy
controller on the enemy now before we
continue let's go here in the prefabs
and drag and drop the enemy prefab right
here inside of the prefabs folder and
now we can go here in enemy and i can
double click this enemy controller
script and i can open it in visual
studio let me just
tag the class here and voila yeah of
course why not why should not visual
studio give me these red errors again
it's been open too much this happened
already so i need to close it and i need
to reload it or open it again for this
to work for some reason it happens like
this sometimes so
i don't know why don't tell don't ask me
because i don't know
anyways in order for us to use the nav
mesh agent we need to see here using
unity engine dot ai
which is
well where our nav mesh agent is located
ai is for artificial intelligence i'm
also going to create here a public enum
that i'm going to call enemy state
and inside of this public enum i'm going
to create two state we are all either
going to have the chase state or the
attack state so either chase or the
attack
now here
inside of our enemy controller right
here right below that is we are going to
have a reference to a private
character animation which i'm going to
call enemy anim so enemy anim underscore
anime we are also going to have a
private nav mesh
agent which is the nav agent that we
have attached he is going to help us
navigate and we will see that in a
second
so navmesh agent which i'm going to call
nav agent
we are also going to have a private
transform which is going to be our
player target
we are also going to have a public float
move underscore speed which is going to
be equal to 3.5 this is the movement
speed of our navmesh agent
we are also going to have a public float
attack distance so attack underscore
distance which is going to be equal to
one f now this attack distance is the
distance how close can we get to our
player before we start to attack
basically how close we get to the player
before we start attacking so attack
distance
public float chase
after
attack
distance
which is going to be equal to one f and
is going to be chase underscore player
so chase player after attack basically
if we attack and after that attack the
player decides to run
this is going to be the distance we will
allow him to run before we start
attacking or chasing him we will see
that don't worry we are also going to
have a private float
wait
before
attack time which is going to be equal
to three this is how many seconds we're
gonna wait before we attack we will see
this in a second
and our private float attack
timer and this one is going to denote
when we start or when should we attack
and of course we also need this enemy
state so here i'm going to create a
private enemy state which i'm going to
call enemy underscore
underscore state
like this
so now inside of the awake function so
awake change start to awake we need to
get ourselves some references first of
all we are going to get our enemy atom
so enemy anim is going to be equal to
get component
character animations
and we are also going to get the nav
agent which is going to be equal to get
component nav mesh agent
we are also going to get our player so
we're going to say player target is
going to be equal to game object
find the game object with the tag
tags dot player tag
dot transform to get the player and this
is what we need for now
inside of the start function
so right here in the start so create the
start function
inside of the start function we are
going to set the enemy state to be equal
to enemy state chase
and our attack
timer is going to be equal to weight
before attack time now in order to
understand this we need to create two
functions
so right here we're going to create void
chase player
like this
chase player and right below we're going
to create void
attack player like this
and in the update function if the enemy
so enemy state
enemy underscore state is equal to enemy
state chase
so if it's equal to the chase we're
going to chase the player
if our enemy underscore state so
underscore
come on here enemy underscore state is
equal to enemy state
attack
then we are going to attack our player
like this so we're going to attack him
okay now let's code it out don't worry
when i code this out i will revise the
code again and it will be very clear
what is happening
so as you can see here
in our chase we are going to say nav
agent set destination so set the
destination come on man set destination
player target.position what the hell is
this you see set destination is the sets
or updates the destination triggering
the calculation for a new path basically
the destination where the nav agent
should go will be our player's position
this is the destination nav agent that
speed is going to be equal to move speed
so this is the speed of the nav agent
that we are going to use
and if our nav agent velocity that
square root magnitude is equal to zero
then we are going to say enemy anim dot
walk and we're going to see here false
and this is basically the same thing as
with our player else except here we will
not have those issues that we have with
that we had actually with our players so
here we have now true
and right here below we're going to test
if our vector 3 distance between the
transform that position which is the
current position of the enemy and our
player target that position
if that is less than or equal to the
attack distance meaning we are in the
distance where we can attack
enemy state is gonna be equal to enemy
state attack
basically we can test this out because
we set here the enemy state to chase and
here we're gonna set this to chase so we
can explain this right here if i go in
unity
and if i take my
warrior so i'm gonna take the warrior
and i'm gonna move him a little bit here
so a little bit away from the enemy and
before we test this out if you remember
we created the enemy by duplicating the
player so what you need to do is select
the enemy and make sure that the player
tag is not selected because if we go
back here in our script we will get the
player target by using the find game
object with the tag so tag player it
will search for that same tag so you
need to be careful with this so select
the enemy and go here and i'm going to
click on add tag and click on the plus
button and create an enemy tag
and select the enemy and tag the enemy
with the enemy tag and i'm going to hit
apply button so be careful with this
because we are using the code here we're
using find game object with the tag
player tag
and it will be confused if it has two
tags and it only needs to find one if
it's find game objects with the tag then
it will get the array of game objects
who have that tag player but when we are
searching only for one
and we have two in the scene then unity
will be confused and your code will be
broken so you need to pay attention for
that so if i hit the play button now and
go back here in the scene view you will
see how the enemy is coming closer to
the player you see he is going towards
the player when he gets where the player
is
so basically he needs to stop here but
we did not tell him to stop he goes here
where the player is and voila that is
that let me just see here if i take the
player and if i position here no he
arrived at his destination and that is
where the path ends i thought that if i
move the player he will
re-re-evaluate his destination but no we
need to do that through code but let me
explain what happened here
basically let's go from the top
this right here is our character
animations and the nav mesh agent that
we're using so this is clear this is the
transform of the player this is the move
speed of our nav agent this is the
attack distance and chase player after
attack distance we will see this in more
depth when we create our attack so don't
worry for these two
but what is going on here is
that we are getting our component
character animations we are getting the
component nav agent and also we created
here these states by the way chase and
attack and we set the state enemy by
default to be chase and here in the
update if the state is equal to chase
then we will chase the player
and when that happens we are going to go
inside of this function so what does
this function do basically you see this
function here will set the destination
meaning the target where should the nav
agent go because the nav agent you see
this one right here that we have
attached on our enemy so let me select
the enemy you see the navmesh agent he
is the one who knows how to navigate on
these
green lines or these this green light
green area because we bake that using
unity's navigation all of that
information is available to our nav
agent he knows how to navigate in this
area
and for it to navigate so for the nav
agent to go towards a certain position
we need to call so we need to call here
set the destination
so nav agent set its destination to the
position where we want to go we want to
go towards the player so where the
player is this is where we want to go
this is our destination
and
here we set the speed to the movement
speed so move speed so that we can move
by the speed that we added right here
which will
set the speed of the nav agent while
going towards this destination now you
see here our nav agent as if i run the
game again
and if i click here on the scene you see
how the enemy is coming closer to the
player so our nav agent has velocity he
built inside of it and we talked about
velocity it speed over time and i set
the example when you press your gas
pedal on your car
your car does not go from zero to 100 in
one second it goes slowly so while
you're holding the gas pedal it goes
from 10 20 30 40 up to 100 speed that's
that is velocity it increases over time
basically if the nav agent is moving the
velocity has a value and square root
magnitude will simply set it to a number
meaning if that number is greater than
zero so if it's equal to zero basically
that means we're not moving so if the
velocity's square root magnitude is
equal to zero we are not moving which
means we are not gonna animate the walk
animation
else
if
the square root magnitude is not equal
to zero meaning it's greater than zero
meaning we are moving
then we will set the walk to true and we
saw that we're animating our enemy so
i'm not going to go back but we saw that
in action
so the next thing here that we have is
our vector 3 distance
and this is this measures the distance
between two points basically here i'm
measuring the distance between the
transform position which is the current
enemy's position and between the
player's position so think of it like
this actually here is my highlight tool
think of it like this if the enemy is
here and the player is here i am
measuring this distance between them so
this distance between them will be
measured and we will check if that
distance is less than or equal to the
attack distance so if the distance again
between the enemy and the player if that
distance is lower or equal to the attack
distance we are in the range where we
can attack then we are going to call
here enemy state change it to the attack
state which
will make this true so if the enemy
state is equal to attack
we will call the attack function player
now inside of this function here what's
gonna happen we need to stop our navmesh
agent you saw that he reached where the
player is and he was moving basically so
let's do that again just to test it out
to see what i'm talking about so he will
reach where the player's destination is
you see here he's going towards the
player going going going he gets where
the player is and he stops but basically
he's not moving but
he is playing the move animation
we don't want that what we need to do
here is we need to call nav
agent.velocity is equal to vector30 this
will completely stop our nav mesh agent
it will make him stop in an instance
basically this is like you apply instant
break on your car and it breaks
instantly it does not move an inch after
that
we're also going to say nav agent is
stopped is equal to true basically this
will stop nav agent nav agent is now not
working when we type here is stopped and
if i hover over it will say the property
holds the stop or resume condition for
the nav agent
even here if we set the destination but
if we call here is stop so if i say nav
agent is stopped and equal to true
it will stop it will not try to go to
this destination because it is it has
instructions to stop
so now we stopped the nav agent by these
two lines of code the next thing to do
is call our enemy walk or enemy anim
walk is now equal to false
our attack timer plus equals to time
dot delta time and if our attack timer
is greater than weight so
wait before attack time this is where we
attack and again we have two attacks so
we're going to say if random dot range
from zero to two
is greater than zero 50 50 chance that
we're gonna attack with attack one or
attack two so here we're simply going to
say enemy anim
dot attack one
else
so not here enemy anim but else
else attack two voila and we need to
reset here the attack timer so attack
timer is equal to zero so that we can
basically attack again
now
we can test this out but we also need to
start chasing the player if we can
attack because the player can move away
from us this right here will stop the
game object it will stop it let me test
it out and see that for yourself so if i
hit the play button now let me just try
to move a little bit closer to our enemy
so come on just move a little bit closer
go here into the scene so pay attention
now the enemy will stop near the player
so it will stop and it will start
attacking you see it starts to attack
but if we run away
we run away and voila this is that you
see but the enemy will continue
attacking here does not know that we
went
so we need to tell it that so we
basically need to tell our enemy that we
run away
so going back here right below this if
statement we need to test if our vector
3
distance
again the distance from the enemy
transform position and the distance from
our player target position
if it's greater
than the attack distance
plus
chase player after attack
so if this is true then we're gonna call
nav agent is stopped is false
enabling nav agent to move enemy state
is gonna be equal to enemy state chase
so that we can start chasing our player
so what is going on here basically we
saw that the enemy was coming towards
the player we explained that right here
we explain what is going on setting the
nav agent destination towards the player
will make the nav agent move towards the
player this is the speed
here we are testing if we are moving we
will animate if the distance is lower or
equal to the attack distance then we
will attack this happens when we attack
we stop the nav agent from moving we
tell him you are stopped don't use nav
agent at that time
we set the walk to false and now we
trigger these attack timers
you saw that in the start function here
i set the attack timer to be equal to
weight before attack time and here i'm
adding to the attack timer delta time
and when the attack timer has a value
greater than weight before attack time
we will attack
basically this is that when we go here
and let me just bring the player closer
to the enemy so now pay attention when
the player gets as close to the enemy he
starts attacking right away so that is
that first attack and now he waits you
see he waits a little bit then attacks
again
basically
this right here because we set the
attack timer to equal to weight before
attack time as soon as we add here time
delta time to the attack timer it will
have a value greater than weight before
attack time
because again we set it here we set that
here to be equal to so when we add a
little bit value to it it is greater
which means we can attack i want to
simulate that because as soon as we get
close to the player i want the enemy to
attack the player that's why it happened
like that
and then we reset the attack timer to
zero if we don't call this if i remove
this from here
and if i go back
basically let me just turn this off i'm
telling now in the word basically too
much i don't know for some reason let me
just take the warrior and move him a
little bit closer to the enemies so that
we can
test it out faster
so if i take the scene view and as soon
as the enemy comes here he will attack
once
and since we are not resetting the
attack time you see what is happening
basically he is attacking every second
now the reason for that is because the
attack time will always have a value
greater than weight before attack time
if we don't reset it back to zero so now
we are going to reset it back to zero
which will wait this many seconds that
we specified right here so now you
understand these variables we wait this
many seconds three that we specified
here before we can attack and we're
adding time.deltatime to the attack
timer
so that eventually it will have a value
greater than weight before attack time
and this is what we are doing here
and basically when we attack again with
the word basically so after that when
this is true we will test the random
range between zero and two if it's
greater than zero we will attack
with the attack one if it's not greater
than zero
then we will attack with the attack two
same as what we did with our player
randomizing the attack and we explained
this
so now we are coming close to this you
see vector 3 distance transform position
so if the distance between the enemy and
the player is greater than the attack
distance which is this one right here so
this is the attack distance plus this
distance right here so if it's greater
the attack distance plus this here we're
gonna give just a little space for the
player so that he can run away before we
start chasing him and we can see that
right here so
first of all let me take the enemy and
i'm going to set
the attack distance and 1.3 because and
i'm going to hit apply to apply that
change to the prefab because i don't
want the enemy to come that close to the
player he can come at 1.3 distance which
is this distance right here you will see
now
you see this right here is exactly
enough and this is how it looks like in
our game view and basically you can play
with this attack again with the word
basically you can play with this attack
distance value so the higher the number
then
the
enemy will be far away from the player
before he starts attacking but pay
attention now when i start to run away
you see he will wait a moment or two but
before he starts chasing me so he will
wait
the attack distance plus this chase
after attack distance
until he starts to chase and here we
need to say nav agent is stopped is
false because here we set it to be equal
to true which will stop the nav agent so
set it to true we'll stop it if we don't
call here
this so if we don't call now it's equal
to false to tell the nav agent that he
is not stopped anymore
pay attention what will happen so if i
hit here the play button and the enemy
will come close to me so he will come
close now he will start attacking but if
i try to run away you see he is not
following me because he is stopped you
see nothing is happening because
we need to tell the nav agent his top
now is false
because again here we set it to be true
and now we are telling him you are not
stopped anymore you can move and he will
start moving and then we simply set the
enemy state to chase which again will
make this right here true which we'll
call the chase player function this will
locate where the player is setting the
destination to the player's position
setting the speed to the nav agent and
moving the nav agent animating him all
of this stuff that we already explained
and we are basically reusing these two
functions
one after another and here if we get
again close to the player then we will
say state is attack which will make this
true and it will execute the attack
player function and we explained
everything what is happening in the
player attack function so basically we
are going from one function to another
chasing the player then attacking if he
runs away we chase him again and again
with the word basically i don't know
what's wrong with me so when he runs
away we start chasing again when we get
close we start attacking again
so what is the next step to do because
we are attacking
but we are not dealing damage and we are
not detecting that attack let's fix that
so go back in unity and select the
warrior here in the scene i'm gonna zoom
in on him on him and click on the sword
so this is our sword for the player and
i am going to right click on the sword
game object itself and create an empty
game object this one is going to be the
attack
point
and i'm going to tag this attack point
with let's say this orange color so that
i can move it and let's say somewhere
around here i think it's okay so this is
where it's going to be at
one 0.0018 eight for the y axis just
move him a little bit here and basically
that is that
so this is where the attack point is
going to be for our warrior and of
course for our enemies gonna be at the
same exact place
so let me just select the enemy here
click on its sword and right click on
the sword and create an empty this is
going to be the attack point for the
enemy
and let me just copy this position so
0.001959 for the y-axis and bam and i
can also tag it here with the red color
so that we can see where it is so we see
them both so we see the orange one and
we see the red one for the player and
for the enemy respectively
what is the next thing to do the next
thing to do is right click here inside
of our scripts folder and create a new c
sharp script and i'm going to call this
one attack
damage and of course as you can assume
we are going to attach this on the
attack point so select both of these
attack points and go here
and filter for the attack damage script
and basically voila
so
now i'm gonna double click on the attack
damage and here it is so what we're
gonna do here is we're going to detect
that collision
what we need for that collision well we
need here a public layer mask which i'm
going to call layer we also need a
public
float radius which is going to be equal
to by default one notice it's public we
can edit this in the inspector panel
we also need the public float damage to
deal damage and that by default i'm
going to set to be equal to 1 of course
because it's public we can change the
value in the inspector panel
we don't need the start function for
this one we only need the update and
inside of the update we are going to say
here collider
array which i'm going to call hits is
going to be equal to physics
overlap sphere
at the transform position using the
radius using the layer that we have
provided what the hell is going on
you see this physics overlap sphere
it is going to overlap a sphere at this
position with this radius and this layer
basically this is the following
so if i go here come on what are you
doing to me
so here is the warrior i'm gonna zoom on
the warrior we
see where the point is so we see where
the point is here it is this orange
thing if i take the animation of the
warrior and let's say attack zero and
point or actually pay attention here i
wanted to say pay attention at point
actually attack two because attack two
you see it's like this he he moves the
hand behind him and then he attacks
this overlap sphere will create a sphere
at that position because we attached
this
on the attack point that sphere will be
created on this position where the
attack point is so it will be created
like this think of it like this this is
my highlight tool here
so it will overlap a sphere at this
position it will create a sphere like
this it's a circle think of it as a
circle it will create that circle at
this position
this is the radius of the circle you
know circles have radius so the larger
the radius the bigger the circle is
and this layer right here means that the
circle
will search for collisions with game
objects that are put on this layer right
here now what the hell is this layer
this layer is this one right here so if
i go here and let me just clear the
pencil this is the layer you see here at
the hierarchy for every game object you
have this layer all game objects are set
on a default layer by default when you
create them and that layer
can be used for collision detections we
can do things like this right here we
can check here in the code if we are
colliding with the game object that is
on that layer we can even exclude
collisions between layers
with well the layer option so what we're
gonna do is we're gonna select the
warrior let me just pull this we're
gonna select the warrior and we're gonna
go under layer and we're gonna click on
this add layer now this is a drop down
list your click on when you click on the
layer you have the default ones that are
created automatically by unity but we
need our own so i'm going to click here
on add layer and here i'm going to
create two new ones one is going to be
the player another one is going to be
the enemy
so if i select the warrior and click
here and we're going to select the
player and you see this will pop up do
you want to set the layer for all child
game objects no only for the parent we
don't want these children game objects
we don't want the them to have the
player layer because the warrior the
player he is important so we're going to
do the same thing for the enemy click
here and select the enemy layer now know
this object only hit apply here hit
apply here now what is beautiful here is
that we can because here in our code we
are searching here for collision with
the game object that is put on this
layer right here you see on this layer
right here so what we're gonna do is
we are simply gonna select the attack
point for the player and we're gonna
search for the collision
on the enemy layer because the player is
attacking the enemy and i'm gonna hit
apply to make that change apply to the
prefab and here for the enemy i'm going
to click on these fold or however these
are called these arrows and select the
attack point for the enemy and we're
gonna search for collision on the player
layer
and click apply so that that change
applies to the prefab because when we
attack with the enemy
we are attacking the player so we need
to search for collisions with the player
or for the player layer and here we're
searching for collisions with the enemy
layer
now how can we
know if we have collisions well simply
simply here because this overlaps here
returns an array of hits or array of
colliders
if we test here if our hits
dot length is greater than zero that
means we at least have one hit meaning
we at least touched one game object so
at least one game object is touched with
this overlap sphere if the length of the
array is greater than zero and here
simply we're going to say print and i'm
going to say
touched the game object
and basically we can test this out right
now so if i go here in the console and
clear everything so clear from the
console
if i go and try to attack even if the
enemy attacks me now so pay attention in
the console what will happen you see we
are printing this too much you see it's
printed like 300 times you see it here
in the console we need to limit this so
we need to limit this well how can we
limit when we touch our game object we
can simply see here game object set
active is equal to false and this is how
we can deactivate
the attack point
but this will create another issue
because again we are dealing damage here
remember here we are dealing damage here
we are going to get the health script
and gonna apply that damage and you saw
it was called 300 times in a second
so the reason for that is because the
game object is active in the scene what
will happen now is the following
when i go here and when we attack pay
attention so only once or actually it
managed to attack me two times or
actually no i attacked him once so yeah
mine was also triggered and enemy was
also triggered but now we are not
detecting collision between each other
anymore
so we need a better solution
but by now i hope that you get the point
why we need to deactivate this game
object the reason for that is
if we leave it open if we leave it on or
actually leave it active you saw that it
is every time it touches or detects that
collision it will execute the code and
here we're gonna do this we're gonna say
hits and the element that's it index
zero dot get component and we're gonna
get the health script component and
we're gonna say here apply damage and
we're gonna pass the damage for the
float now we did not create this but we
will create it and this is going to
apply damage to our game objects and
if we allow it to apply that damage 300
times as we saw a moment ago
then we cannot play our game our players
will die instantly
so what we need to do is select these
attack points and turn them off so turn
one attack point off turn another attack
point off and for our warrior
so starting with the warrior what i'm
gonna do is go here in the player attack
input
and right here
right here below our character animation
so below the character animation what
i'm going to do is i'm going to create a
public game object which is going to be
the attack point
and at the bottom here
i'm going to say void
activate
underscore attack point
and i'm gonna say here attack point set
active is true to activate that attack
point and below i'm gonna say void
deactivate underscore
attack point and simply here i'm gonna
say attack point set active is equal to
false or set it to false
but
one thing to note here is because we are
deactivating here there is a chance that
we will deactivate this game object
before it's even
deactivated here
because notice what we're gonna do here
we're gonna test if
our attack point so attack point
is active in hierarchy so if the attack
point is active in the hierarchy then we
will deactivate it the reason for that
is because we're gonna go now
and we're gonna take these two
animations
so we're gonna take the attack zero one
and attack zero two or actually attack
underscore zero so click it here here it
is attack underscore zero and duplicate
it
and attack underscore one and duplicate
it
so i'm gonna take the attack that's
going to be attack two
and this one is going to be attack one
so i'm going to name them attack one and
attack two and go back in the animator
and select this right here and change
the motion or the animation clip from
attack zero to attack one and for the
attack to do the same thing except here
we're gonna select attack two because
you can assume why we already did this a
moment ago we're gonna go in our
animation tab right here and for the
attack one so i'm gonna preview it so
this is how it looks like
and
here
i'm going to click on this
animation event and i'm going to select
to activate so activate the attack point
and when it attacks so somewhere around
here i'm going to click another
animation event and i'm going to type
here deactivate
the attack point
and for our attack two animation i'm
gonna do the same thing for some so
somewhere around here here this is where
the attack will go or this is where the
animation will get and i'm going to call
here
activate attack point and when it gets
here
i'm going to call another animation
event and i'm going to say here
deactivate
attack
point so you get my point so do you get
what i why i'm doing this here because
if we attack and we don't hit a player
if we get a hit here for the player or
for the enemy then we will deactivate
the game object but if we attack and we
don't get collisions here then when the
animation finishes let me go here player
attack input when the animation finishes
then we will
as you can see deactivate these attack
points and we're gonna copy these two
scripts and go inside of the enemy
controller
and here right below the enemy state i'm
gonna say public game object attack
point
so declaring the attack point and going
here at the bottom so right above the
class declaration and calling these two
functions
and we don't have to do anything for the
enemy because the enemy has the same
animations we're using the same animator
controller for the enemy and for the
player so if we go here we have the
attack as you can see it has the exact
same effect
what what is left for us to do is select
the warrior and drag and drop his attack
point here inside of the player
attack input sorry i bugged there for a
second so player attack input and for
our enemy select the enemy and here for
the enemy controller drag and drop the
attack point in the attack point and hit
apply so that these changes apply to the
prefab
and basically we are done we can now
test things out and i will explain them
again so let's go here in the console
and see how things goes how things goes
how things go notice now if i attack so
i'm attacking the point is being
activated but we are not hitting the
enemy so now you see and the enemy
attacked me but now you see why is this
here necessary
the reason for that is because you saw i
attacked the enemy was not there
we activated the attack point but
we did not have any hit we didn't have
any hit here so this will not be
executed it will not hit this line of
code so game objects active false will
never be called
meaning when we get to the animation
frame where we call this
then if the attack point is active this
right here will test if it's active
meaning we did not call set active or
it's basically active if i select the
ground
and if i click here this is now not
active you see when i click on this
check box which is right here you see
where the check box is
right to the left side right to the left
to the left side of the layer that's the
check box and if we click on it now it's
unchecked meaning the game object is not
active if it's checked then the game
object is active we can even see it in
the scene and we are doing this exact
thing in the code with this line of code
set active is false will not make it
active and it's not visible in the scene
and true will make it visible and active
in the scene so this right here tests if
the game object is active if it is then
turn it off because we tried to attack
we
activated it but we did not made any hit
so yeah it's not active and now we can
test it out so if i go here you see the
enemy is attacking in and touch the game
object and here i touched him again and
now i touched him and now he will attack
me and he touched me and now i touch him
i know this sounds weird i touch him he
touch me but hey don't don't think of
anything anyways
you get my point and you see
how can we detect collision by using
this right here now we can safely delete
this because we see it is working we
printed everything out so we see that we
are detecting collision so we are
detecting we can touch the player and we
can touch the enemy so we can basically
attack each other the next thing to do
is create this health script that we
commented here out so i'm gonna copy
this name health script and go here in
unity and go back in the project because
we need to subtract the health value
from our enemy when we attack with the
player and vice versa so in the scripts
folder right click here and create a new
c sharp script name it help script and
as you can assume we are going to attach
it on the enemy and on the player so
select both of them so select them click
on add component and click here
help or actually filter for the held
script and voila here is the health
script
so we need to double click it and open
it here in visual studio let me just tag
the class here because inside of our
health script we are going to subtract
the health value which means that we're
going to have here a public float health
which by default we can say it's equal
to 100 so when we start the game we are
going to have 100 for the health
now here i'm going to remove
start and update and we're going to have
here a public void apply damage which
will take float damage as a parameter
and simply what we're going to do is we
are going to see here health
so our health variable minus equals the
damage that we are passing here as a
parameter because we are going to
subtract
damage from the health now this minus
equals is a shorthand for writing health
is equal to health minus
damage so instead of typing health is
equal to health minus damage we can
simply say minus equals
and it is the same exact effect
and here let me just tag it so apply
damage
and here we're going to test if our
health is lower or equal to the value of
zero
meaning
health dropped to zero here we're going
to print or we're going to say here
the character died so the character died
and here in our attack damage now we can
uncomment this line of code and you can
see here the beauty so we have the
damage here which is the parameter we
set it to be public basically this one
is the local variable and here we have
the as you can see we're passing it as a
parameter we set it to be public which
means we can edit the value in the
inspector panel so when we call apply
damage we can say for example this value
here to be 100 and it will subtract 100
from the current health value then it
will test if the health value is less
than or equal to zero if that's the case
we will print that the character has
died now in order to
see this in action i'm going to go here
in the console let me first clear the
console that is
and if i take the warrior
since we are if you select
here our warrior and the attack point
you will notice that the damage is one
if i set the damage to be 20 for example
and go here select the enemy and you see
here the health is currently 100 you can
pay attention right here let me just
clear my highlight tool here i'm going
to select the enemy and now when i play
the game you will pay attention to the
health value of the enemies let's go and
attack the enemy so here we are and
attacking pay attention now it's 80 then
60 40 20 and one more bam you see the
character died so the character
died
now this is not the effect that we want
we don't want to print simply in the
console the character died instead we
want to do some things and in order to
do those things we are going to create
here a private float
x that this is going to be the rotation
which is going to be equal to negative
90 because what i'm going to do is let
me explain first
i'm going to take the warrior here
and when one of the game objects dies
what i'm gonna do is simply rotate him
backwards so rotate him like this
negative 90 degrees so he falls on the
ground because we don't have a that
animation if i go here in models
so models and animation we don't have
here the death animation so we cannot
simulate that with an animation because
this 3d model for some reason don't ask
me why i don't know
so i don't know why doesn't it have the
death animation but usually you will
have that animation and then you will
play that that animation but here we're
going to simulate that so we're going to
see here private float x that
private float that underscore smooth so
smooth which is going to be equal to 0.9
f
uh private float rotate underscore time
which is going to be equal to 0.23 f
uh private
bool which is going to be equal to or
name of it is going to be equal to
player died
so what do we need to do well now we're
going to get back the update function so
we're going to see here update and here
what i'm going to say is if player died
so if that is true so if our player has
died
we are going to do what we are going to
call a function that we are going to
call rotate after
death now of course we did not create
this function so let me just go here hit
enter to
give a little bit more space here come
on what are you doing here and here so
if player died if this becomes true and
by the way by default a boolean
value is equal to false so if we simply
declare a boolean value like this so if
we declare it we don't type it's equal
to false or if it's equal to true then
by default it will be equal to false
that means this right here will not be
true we need to set it to be true
and here we have this rotate after that
so simply here we're going to create
this void rotate after death
and simply here we're going to call our
transform euler angles you see the
rotation as euler angles in degrees you
see this euler angles is vector 3 so we
need to pass a vector 3. so this is
going to be equal to
new vector 3
and we have now values for x y and z for
our x we're going to use mat f dot lerp
from transform euler angles x
towards the x that
in time dot delta time multiply that
with the that smooth
for our y we're going to say transform
euler angle is y and for z transform
euler angles dot z so what is going on
right here so what is this
well basically we are going to learn and
lerp means you see this math f lerp it
linearly interpolates between a and b by
t this is a
in order to paint the picture this
lerp will go from this point to this
point in this timely manner so this is
point a so this is right here point a
and this right here this x that excuse
me so let me just go here so this is
point a
this right here
this is point a
this x that is point b so here we have
point b
and t is time so the time interval which
is this one right here time dot delta
time multiplied with the that smooth
that is t or time now again y times that
delta time well because
to
smooth things out and you see here i'm
using that smooth so we need to smooth
things out
again we will go from this value towards
this value in this timely interval and
the current value of the euler angle x
which is the rotation x will be zero
we're going to go towards the x that
which is negative 90
and basically that's what we are doing
and in order to activate this what we
need to do is here we are going to
create an i enumerator not enumerable in
numerator and we're going to call this
one player or actually we're going to
call it a low allow rotate and here
we're going to say player died is equal
to true to allow the player to die
basically it will activate this right
here so it will go here in the if
statement this will be true and it will
activate the rotate after that function
then we're going to say yield return new
wait for seconds and that's going to be
the rotate time which is 0.9 if we go
back here this is the rotate item
actually 0.23
so this is the rotate time
and then simply here we are going to
call our player diet is equal to false
now we will see in a second why are we
doing it like this so if i go here if
health is less than or equal to zero
then we are going to die so we are going
to call here starco routine and we're
going to call allow rotate co routine
which will
enable player that is equal to true
which will make this true which will go
inside of this function and it will
rotate so it will go from the current x
rotation to this the targeted which is
the x depth
rotation which is negative 90 in this
time interval and we will leave the y
and the z rotation intact
if i go back now and what i'm gonna do
is i'm gonna do that with the player
or actually let's test it out on the
enemy because we have the same script on
the enemy first so if i go here i will
need to
attack the enemy five times so one
two
three
four and notice now when i hit him five
bam you see but
basically nothing happened why is that
well we need to also deal with some
other things such as we need to
deactivate these scripts if i select the
warrior for the warrior we will need to
deactivate his animator player moves on
and so forth for the enemy we need to
deactivate the nav agent we need to
deactivate the enemy controller so we
are going to start doing that with this
variable so we are going to create here
a public bool variable that we are going
to call is player so call it is player
like this
and when we die so when we die here
inside of this
if statement so if health is less than
or equal to zero the first thing that we
need to do is we need to say here get
component the animator so animator
component and we need to say enabled is
equal to false so that we can deactivate
the animator so deactivate the animator
now next we are going to test here if is
player so if we have a player we are
going to disable his script so we are
going to see here get component
player move and we're going to say
enabled is equal to false we're also
going to say get component player attack
input and we're going to say enabled is
equal to false we're going to do two
more things but we will see that in a
second else if it's not the player so if
it's the enemy
then we're going to say here get
component and we're going to say enemy
controller component enabled is equal to
false and we also need to say here get
component nav
mesh agent like this
and we need to say that enabled is equal
to false but
you see here we cannot do that because
we need to go here and type using unity
engine dot ai so that we are able to use
nav mesh agent as a component and
disable it
so you see here we have is player and we
have else which is not for the player
and we are going to differentiate
between the player and the enemy because
they both
carry this health script so what we need
to do is go here in unity and select the
warrior so the warrior we're going to
select him and he is going to have this
is player check box checked inside of
the hell script i'm going to hit apply
to make changes to the prefab so you see
here that his player is checked but for
the enemy his player will not be checked
and i'm also going to hit apply for the
enemy so that that change applies to the
prefab so for the enemy is player is not
going to be checked so let's test this
out right now and see if it actually
works
so if i go here i will need to attack
the enemy
four or actually five times so one more
time notice one more time after i hit
the enemy bam you see now the enemy
falls and pay attention you see his nav
agent is not active his enemy controller
is not active so we had to disable those
scripts you see we had to disable these
scripts in order for this to work
because we are simulating the rotation
we are simulating the that by simply
rotating the character to fall down
basically this is what we are doing so
he is falling down
and
this is how we are simulating that
but since we had the enemy controller
script which was attached on the enemy
we also had the nav agent which was also
attached on the enemy they are
interrupting this process right here so
we had to disable them so we had to
disable them right here so enabled is
equal to false basically means this if i
select the enemy and i disable his enemy
controller script and i run the game you
will see now that the enemy is standing
over there it's not working because if
you pay attention here on the enemy you
see where his enemy controller script is
you see the check box right here it is
disabled meaning this script will not
will not function if i enable it bam you
see automatically starts
running towards me because his script is
also running and it is working
now let me take here the enemy and what
i'm gonna do is for the enemy attack
point i'm going to set the damage to be
equal to five to make this somewhat a
fair battle and i'm also going to take
the warrior and set his health to be
equal to five so set the health of the
player to be equal to five because
we want to test this out i want to show
you two issues that we have pay
attention now when the enemy kills the
player bam you see the camera went over
here and notice the enemy is still
attacking pay attention the enemy is
attacking and the player or the camera
is at the ground
so we need to add two more lines of code
here if he is player so if he is player
we are going to disable the player move
and the attack input and notice here we
are disabling the animator for both the
enemy and the player so we are disabling
for both again the enemy and the player
because we don't want their animator to
play anymore but here if he is player
and we checked the checkbox for the
player so we saw how that goes so we
check the checkbox if his player then we
also need to do two more things we need
to
say here our camera so camera main
dot transform set parent is now equal to
null because
pay attention here the camera and let me
just fold all of these so fold this and
this you see the camera is a child of
our player so whatever the player does
so if the player is being rotated let me
select the warrior if i rotate the play
you see the camera is following because
the camera is a child of the player
whatever the player does if he moves
left the camera moves along with it if
he moves right the camera moves along
with him so if he rotates the camera
will rotate because it's a child of the
warrior game object so what we need to
do is we need to set the parent of the
camera null meaning
the player is not the parent game
object for the camera anymore
and you also saw that the enemy was
attacking so we need to disable the
enemy script we need to make the enemy
basically not attack the player and we
are going to do that by calling here
game object
find the game object with the tag
tags dot enemy tag so enemy
underscore tag and we need to create
this enemy tag in the tag so going here
and here we're going to say public
constant string enemy tag is equal to
here enemy
so we are going to get the enemy and we
are going to see here
dot
get component and we are going to get
our enemy controller component and we're
going to say that enabled is now equal
to false because we want to deactivate
our enemy game object we can now test
this out but we need to make sure two
things first of all you need to make
sure that the enemy is tagged with the
enemy tag and that the camera main is
also tagged so if i go back here select
the enemy you see that the enemy is
tagged with the enemy tag
select the camera in player so this one
right here see the camera in player and
it's untagged so we need to select this
main camera tag it is build by default
so by default it is in unity so select
the camera in player and put the main
camera tag on it because as i said this
camera main it's built in unity and if i
hover over it will say the first enable
camera tagged main camera so in order to
do this or use this in our code we need
to tag the camera with the main camera
and if i test this out now because i
have set the health of my player to 5 as
soon as the enemy attacks me notice now
i'm dead the enemy is standing still he
is not attacking me because we disabled
his
enemy controller script as you can see
he is playing his idol animation and i'm
land or standing here dead or laying
down dead and the camera pay attention
the camera is now free if i select here
it is you see camera in player is now
free it's not under the player game
object it's completely free it does not
have a parent game
object any more basically this is what
we did so again here briefly
we are testing here if health is less
than or equal to zero this is clear when
that happens we will disable the
animator we want don't want to animate
we will allow to rotate and we explain
here what we are doing because we only
want to rotate 90 degrees angles we are
going to allow the rotation wait for the
rotate time and disable or set the
player that died is equal to false so
that we don't
so that we don't activate or don't do
this anymore because if i remove this
right here let me just go and test it
out so if i go here and wait for our
enemy to come closer
come on unity run the program run
notice now when the enemy hits me bam
you see
this is the reason why
this is the reason why i am allowing and
then not allowing so if i say here
player died true then we will allow to
rotate then we will not allow to rotate
you saw the reason why because this
right here will execute over and over
and over and we don't want to allow it
to glitch as you saw a moment uh go
so what is the next thing to do the next
thing to do is to display the health
values
to our user so if i go here in unity i'm
going to create a ui canvas now i cannot
go too much into ui i will cover that in
some other video so
it will be too much this video is
already almost i don't know three hours
anyways i'm gonna right click here and
go under you and create a image so let's
go here and create an image and this is
our ui canvas and i'm going to name it
to ui canvas
and we're going to leave it at screen
space overlay which means basically it
will overlay the screen but i'm going to
set this canvas scaler script to
constant pixel size to screen with
screen size or scale excuse me not
screen with screen size scale with
screen size
reference resolution is going to be 1280
by 720
and i'm going to say here 0.5 to match
width and height equally basically what
i did here is that this canvas will take
this reference resolution at
like its main resolution and it will try
to make these game objects
apply or or resize according to this
resolution as i said i will cover canvas
and everything specific to the canvas in
a separate vdo
so now i'm going to right click on the
canvas and create an empty game object
and this empty game object is going to
be our player health it's gonna be empty
as i already said and we're going to set
the width and the height 1280 by 720
and the first thing that we are going to
do with this player help i'm going to
right click here
go under ui and create a text
component
and i'm going to rename it here to
player text and there inside of the text
we're going to type layer this is what's
going to be displayed
i'm also going to align the text at the
center or actually we're going to align
it
left but center so left here center but
on from the left side
the font size is going to be 59 and i
know that you currently don't see the
text but you will see it in a moment
the color is going to be white so click
here on the color if you ever worked in
photoshop or any other editing software
in regards to images you will you will
know these options or any text
software so you know the alignment you
know what is a font size you know what
is a font style bold italic normal sun
and so forth
now for the width of our text i'm going
to say
386 and for the height i'm going to say
81
i'm also going to set here the anchor at
the top left corner basically this will
make the center of the player the top
left corner so this is the anchor it
will make the center of our text top
left corner the position x is going to
be 214 and the position y is going to be
negative 40. so this is for our player
the next thing to do is create rbg which
is our image i'm simply going to drag
and drop it here under the player help
so drag and drop the image and i'm going
to rename it to bg
set the anchor of it to top left corner
and the width is going to be 228
the height is going to be 29
and i'm going to set here the position x
at 130
and the position y is going to be
negative 100 101.
now i also need to duplicate this health
so i'm going to duplicate it this bg and
i'm going to rename it to player health
display
and for this one we need to go here in
our project and ui and we have this
health ui which is simply this image
this white thing right here
but what we need to do is we need to
select it
and in the inspector panel because if i
try to drag and drop it right now here
it's not gonna work and let me just
clear here i can drag and drop it here
for the image to represent to be
represented by that image but it's not
working now because the default value or
the default texture type of this image
is set to default
you need to click here on this right
here where it says texture type
so you need to click on that and you
need to select the sprite 2d and ui and
hit apply at the bottom right corner so
now we are able to use this health ui as
an ui or actually drag and drop it here
and simply create a health ui from it
now again you don't see the difference
but now i can click on the color and i
can change the color to red which you
can see but what's most what's more
important i can click on this image type
inside of the image script i can click
on the image type and i can set it to be
filled
and i can change this from radial to
horizontal and pay attention now when i
move the slider left and right so you
see i'm moving the slider left and right
and we are filling
up
the
so we are filling up the image
with this red value we are filling it up
you see and lowering this is going to be
our health this is going to display the
health so what i'm going to do is
duplicate this player health and rename
it to enemy health so here enemy health
and i'm going to take the text and it is
going to be enemy text
and position is 214 but i'm going to set
the anchor at the top right corner and
the position is going to be negative 214
and the alignment is going to be on the
right side
and here we are going to say enemy like
this
for the bg we're going to do the same
thing so x is 130 set the anchor at the
top right corner and this one is going
to be negative 130
and for our enemy display held so change
it from player to enemy health display
i'm going to set the anchor at the top
right corner and set the x position to
negative 130 but you see now we need to
change the origin of the fill because it
is filling from the right side we need
to change it to the left and this is all
or this happens in all of these options
so right here so you see the fill
right here i change it from left to
right so select on this drop down let's
change it from left to right so now when
we fill you see it's going
from
this side or from the left to the right
now what we need to do to make this
happen is simply go here in our visual
studio and in the health script right
here
we also need to type below using unity
engine a i we need to type using ui so
unity engine dot ui
and right below our public bull is
player we're going to create a private
image which i'm going to name health
ui
and we made it private because we don't
want it to be accessed in any other
class
so don't want it to be accessed in any
other class but we also want to drag and
drop so we want to drag and drop these
health display values
in appropriate slots so you see here we
don't see them but since we made it
private i'm simply going to see here
serialize field and it will make them
visible now in the inspector panel so
you see now they will be visible in the
inspector panel so for the warrior
drag and drop the player health display
and for our enemy health or actually for
the enemy drag and drop the enemy health
display so for the warrior we have the
warrior or player held display for enemy
we have the enemy held display and
inside of here inside of our apply
damage
right here after we subtract from the
health the damage value
we are simply going to see here if our
health ui so health ui is not equal to
null so we have a reference
we're going to say health ui.fill
is equal to the health value but we need
to divide that by 100 because
if you go back here in unity
you will see that when you select let's
say for the player health display if you
select it you will see that the fill
amount goes from the value of zero up to
one so it goes from zero to one it
cannot go from zero to one hundred so
from zero to one that's why we are
dividing this by 100 because if the
health current value is 100
and we subtract from it 20 that will be
80 80 divided by 100
it's eight
so that's why we are actually not 8 but
0.8 excuse me so 80 if we divide 80 by
100 so 100 that's equal to 0.8
and it will fill that image amount
so if we go back here in unity what i'm
going to do is set the warrior health
back to 100 and for the enemy health
so select the enemy the help is also
equal to 100
so what we need to do inside or actually
now simply test it out so if i hit the
play button
let's go here and pay attention when the
enemy attacks the player you see now pay
attention to the player's health value
it's decreasing and also when i attack
the enemy you see i'm decreasing his
health value decreasing even more and
even more and even more and when i hit
him one more time he dies and as you can
see he is lying dead
and basically this is how we are going
to
this is how we are going to display
the
health value now that we are displaying
the health value let's shield ourselves
from the enemy and in order to do that
i'm going to go here in scripts and
right click
and go here c-sharp script and create a
player shield script as you can assume
this is only going to be attached on the
player so select the player and drag and
drop the player shield script on him hit
apply so that that change applies to the
prefab
go here and double click player shield
and this one is going to be a very
simple script here we are going to have
a private
and that is going to be our health
script so health script which i'm going
to call health script
and in the awake function we're going to
get the reference it so awake here and
we're simply going to say held script is
equal to get component health not head
off health
script component
and here we're going to have a public
void activate
shield which is going to take a bull
shield active
and what we need to do here first of all
we need to go in the health script and
we need to create here a public
public bull and this one is going to be
shield activated
and it is public because i want to
access it here because i want to see
here
healthscript.shield activated is equal
to this value here shield active so it
will be either true or false
so that's why i want it to be
public i don't want it to be private
because if i set it to be private it
will not be able to access it here you
see
is do it's inaccessible due to its
protection level so we cannot access it
because of its protection level
so we need to make it public i but i
don't want it to be visible in the
inspector panel if i go here and select
the warrior pay attention to the player
health script i don't want this shield
activated to be visible here
so what can i do same as here serialize
field for the private variable we can
say open square bracket hide in
inspector for a public variable and it
will be hidden
in the inspector
panel
now what we're gonna do with this shield
activated is here on top
of our apply damage you see
i'm going to say if shield activated i'm
simply going to say return now you're
like but we cannot use return and avoid
function
yes we cannot use it to return a value
but if we say here return this will
break out of the function what do i mean
by break out of the function
imagine like this if this is true when
we enter this apply damage function so
this is the first thing that we're going
to check if this is false then we will
proceed down to execute the rest of the
code so from here up to the bottom where
we have the code inside of this function
but if this is true if shield activated
is true and we hit this return statement
we will exit out of this function so it
will go outside and we will not proceed
below
to execute all of the code that we have
so we will not proceed to execute the
code basically we will exit out of the
function
in order to see this in action i'm going
to go here in our player attack input
right here
and right below our public game object
attack point we're going to create a
private player shield which i'm going to
call shield
in the awake function we're going to say
shield is equal to get component and we
are going to say here player shield
component
and simply here when we defend you see
when we defend we are going to say
shield dot activate shield is true
and when we release the j key we are
going to say here shield activate shield
is now equal to false
which will go inside of this script
right here it will call this function
and passing here true or false it will
assign that value to the health script
shield activated
which is this value right here again so
it's this value right here shield
activated
and
if that is true it will return and break
outside of the function and we can test
it out if i go back in unity let me just
clear the console this works only if you
press the j key and hold it you can it
does not work if you press and release
it if you just press it and release it
it will not work so you need to press
and hold it pay attention now if i go
here
so if the enemy attacks me now pay
attention to my health it's going to
subtract again it's going to subtract
and now i'm going to hold the shield pay
attention to the health value pay above
pay above pay attention above you see
the player health value is not
is not decreasing
while i'm holding if i release now he is
going to subtract value if i press and
don't hold you see if i press and don't
hold it's not going to work so you need
to press and
hold so press and hold for this to work
because
this is what we're doing in our attack
input if we press the key down then we
will defend if we release the key up
then we will
well release the key and then we will
not defend so you need to press and hold
for this to work and release then for it
to
well not
defend itself any more
so this is for our defense the last
thing that is left for us to do is
create our sound effects so let's go
back in unity and i'm going to right
click on our warrior and create an empty
game object this is going to be our
player sound like this
and here we're going to click on add
component and filter for the audio
source component and voila but i'm also
going to uncheck this play unawake
checkbox so don't check this because it
will play a sound any clip that we
attach here on awake we don't want that
i'm also going to do the same thing for
the enemy right click on the enemy
and create an empty game object and
we're going to call this one enemy sound
and here attach the audio source uncheck
play on awake
and go here in our project and in
scripts and right click and see sharp
script i'm going to call this one
character sound fx
select the enemy sound and the player
sound so select them both
click on add component and attach the
character
so character
sound effects voila and we do need to
hit apply for the enemy and for the
warriors so for both of them so that
that change applies to the prefab but
now open so select the player sound or
character effect sound script open it
and what we are going to do here is we
are going to get a reference to the
private audio source
so here on top private audio source
which i'm going to call sound fx
and we're going to do that in the awake
function so awake
sound fx is equal to get component audio
source component
we are also going to have here private
audio clips so audio clip attack
so attack underscore sound underscore
one attack underscore sound underscore
two
and di
sound simple like that now they are
private but we don't want to allow them
to be used in other scripts but we are
going to make them serialize field
because we are going to drag and drop
them
and here we are simply going to create a
public void
attack function underscore one so attack
one and we are going to play the attack
one sound so we're simply going to say
sound
fx dot audio clip or simply clip is
equal to
attack sound one
and simply call sound effects dot play
to play that sound effect it's simple
like this programming is beautiful if
you want to change a clip that's
attached to the audio source dot clip so
you say sound effects or the audio
source dot clip is equal to the clip you
want to attach
sound effects or the audio source that
play to play that sound and i'm going to
duplicate this function and this here is
going to be attack 2 and we're going to
attach the attack to sound
and here we're going to have our die
sound so public void die and we are
going to attach here the die sound and
play the die sound it's simple as that
now this character sound we are going to
have in our health script we are going
to have it in our player attack so let
me just go here player attack help and
enemy controller
now what we need to do is we need to
declare here uh private in the health
script that is so private and we're
going to see here character sound
effects
and we can call it sound effects so i
can call it sound come on
sound effects like this
and inside of the awake function so void
awake
we are going to say here sound fx is
equal to get component in children like
this and we're going to say character
sound effects now why get component in
children because if we go back here you
will notice that this player sound is a
child of the game object we cannot say
get component we use get component on
the game object itself so if we for
example we have the warrior game object
if we want to get the character
controller then we will say get
component because it's attached on him
but if we want to use this script
character sound which is attached on the
warrior
then we need or actually excuse me
attached on the player sound that which
is a child of the warrior then we need
to use get component in children and pay
attention this only works if you have
if you have only one script attached so
if we have a duplicate if we have two
player sounds it will work but it will
get you one of these it will not get you
both it will get you one of these so
when you have one like here player sound
it only has this character sound effects
then you can use get component in
children and it will get you that one
component and since we are here i'm
gonna go quickly in sounds
and here we have arena so these are
arena sounds unit and these are attack
sounds you can preview them you can
click on it and preview it here
this is attack one or tag zero here we
have
this one and this one
i'm going to attach these two so select
the enemy sound and play your
i'm gonna attack attach
man attack one
and man attack two for the sound attack
one and two and for the die sound let's
see this one
yeah this one can do
yeah the second one i mean you can play
with these and you can
take any one that you want so for our
player sound i'm going to attach that to
and for our enemy sound i'm going to
attach i don't know that nine i'm not
gonna even check it out so i'm gonna hit
here apply and apply for this one as
well i'm not even going to check it as i
said because well we don't care we have
the attack sound we have the dead sound
so it's not important how they sound
so let's go back here we have the sound
effects and we're going to get it here
and when we die so this is where we die
in our health
and if health is less than or equal to
zero we are going to call sound effects
die and this will work both for the
player and both for the enemy so when
they die they will display the die sound
and before we test it out i'm gonna go
also here in our player attack so right
here i'm going to create private
character sound effects and this is
going to be sound effects
and simply here sound effects is going
to be equal to get component in children
character sound effects
and i'm going to copy this line of code
because we're going to use it in our
enemy controller and when we attack so
this is where we are attacking so here
i'm going to call sound effects dot
attack 1 sound
and here i'm going to say sound effects
dot attack
two sound
simple as that it's very simple so when
we attack with the one attack sound
attack one when we attack with the
second attack attack two and we're going
to do the same thing here for our enemy
so if i go here in our enemy
right here at the top
private character sound effect
sound fx
right here in the awake below our player
target sound effects is equal to get
component in children and get that
component
and when we attack here so let's go in
the attack here it is attack and m1 so
we are simply going to say sound effects
attack
one
copy this and paste it below and attack
you this does not require any
explanation because it's
self-explanatory i've explained it right
here so we are going to call the clip an
attack and here simply set the clip to
the clip that we want and play it set
the clip to the clip that we want and
play it and voila it's gonna do that for
us and we are calling this when we
attack so when we attack we will play
attack one sound and attack two sound
when we attack with the second attack
this is what we are doing for the enemy
and for the player as well
now in order to spice this up we are
basically done with our game so we are
done and in order to spice this up what
i'm gonna do is right click here and
create an empty game object this is
going to be our bg sound
and i'm going to
reset the position and attach two audio
source components so audio source here
and another audio source so two of them
because i'm gonna go here inside of the
sounds and arena and we have these arena
sounds so you see
and this one also
[Applause]
so pay attention
what i'm gonna do is select the bg sound
and for the first one attach the arena
one sound and for the second one arena
underscore two sound and pay attention
here play unawake if you don't check it
so if it's not checked if i run the game
we will not hear these sounds so if i
run the game the sounds we don't hear
them
so make sure that you check these check
boxes so i'm gonna check play on awake
let me just find it play on awake and
loop
loop means when the sound effect
finishes to play same like on your
it's same like in your mp3
app so when you click loop it will
repeat the song so check here play on
awake and loop and notice here it's
warning us that we have two audio
listeners in our game now the reason for
this is because we have one main camera
that has an audio listener and we also
have another camera that's attached on
the player that has a listener so you
see here camera here and camera here
so i'm gonna take the main camera this
one that's not active and remove the
audio listener from it because this is
the audio listener that will be
getting that sound and
make us hear the sound effect
now also one thing that you have in this
audio source is the volume so pay
attention here have the volume one it's
100 so i'm gonna lower it
for arena one i'm going to say 0.4 for
example and for arena 2 i'm going to say
i don't know point
25 maybe
because i don't want that sound to be
heard like we heard it a moment ago it's
too loud for our game and practically
congratulations this is our game we are
finished we can test it out to hear the
sound effects
so i'm gonna shut up
so you see here and let me just check
here again we have two listeners
so let me just find where they are
so we have ui where are these two
listeners so we have the main camera we
have here
camera in player audio listener
and event system
i don't know where the second listener
is it will not hurt our game if you find
and just remove it let me just remove it
from here so select the camera in player
and
remove it so let's see if we can hear it
now
i literally don't know where the second
audio listener is but if you find it
just remove it i don't care it's not
important it will not hurt our game and
we can play our game so hear the sound
effects
and also
pay attention to the player's sound
death and also for the enemy and their
attack sound so pay attention now
here
i don't think it's i don't think we can
hear it because of these sounds so bg
sound i don't know this one is going to
be at 0.2
and this one is going to be at 0.1 so
like this so the volume for this one
actually i set the pitch here so pitch
is at one volume needs to be at point
four excuse me for this one and for this
one
0.25
so it can work now i forgot i said the
pitch not the volume so yeah set the
volume to 0.4 and 0.25 for the second
one so that we can hear it so pay
attention now
yeah now we can hear it a little bit
something like this
yeah
0.1
or 0.2 for the volume here and for this
one is going to be 0.1 finally i decided
which volume i want so hit the play
button let's test it out finally
so you hear it listen to the attack
sounds
again
and again
so now when i hit him he will die
and you heard
the
die
sound effect
and we can also test for the player to
see if the player can die so i'm gonna
set the health value to five and we will
die instantly just so that we can test
that the die works for the player as
well so pay attention now
voila
so the die
also works for the player he is dead the
enemy will be standing still because he
well we talked about it we deactivated
scripts so on and so forth
now congratulations this is a big
project even though
it's not that big when you look at it
visually we don't have 20 players
fighting with each other but we learned
combat we learned melee battle system we
learn how to
deal damage how to subtract health and
all of the good stuff that you don't see
in any other tutorial online basically
you cannot find this anywhere else on
youtube so yeah thank you awesome dudes
and you're welcome anyways if you want
more awesome tutorials go on
awesometubes.com subscribe there more
actually awesomeness.com go on youtube
awesometubes and subscribe on my channel
and you will have more cool tutorials
like these
until then fa here here and by the way
you can download this complete project
so this complete project and all the
assets link is in the description below
until next time fi here here from
awesometubes.com i will see you guys in
another video