hello and welcome to fist of damage in
tutorial i'm rana from a mountains and
today we're going to talk about weapons
so the top down engine includes a
generic weapon system that allows you to
pick up weapons use them of course have
ammo count reduce you know so you can
manage magazines and reload and all that
stuff and with it you can create
projectile based weapons many or
whatever you can think of switch to
another weapon and so on the engine also
includes a few examples of weapons that
you can have a look at such as all the
one in the quality machine but also the
left free DD machines and a few other
ones and you cannot use that as a basis
to create your own in the previous
tutorial that focused on animation i
showed how to create a meta weapon in
freely in this tutorial i'm gonna focus
on creating 2d weapon for this koala
character that should project us so the
first thing we want to do is exit play
mode and start creating a weapon to
start creating my weapon the first thing
i want to do is grab my koala 2d
character so he would be in prefabs
playable character and I'm gonna go and
drop him into my scene and I'm gonna use
that as a reference for the sizing and
positioning of the weapon so next I want
to create an empty object and I'm gonna
call it tutorial we got and that is
gonna be the basis for Myka
I'm gonna go and add weapon actually a
project I weapon component to it and I'm
also gonna have weapon aim to the
component on it that is going to allow
me to aim my weapon just to position it
correctly I'm gonna nest it into that
weapon attachments lot and
position it at zero now I want my weapon
to to be seen just like the DK over
there so what I'm gonna do is I'm gonna
go into my game folder and I prepared a
sprite for it so I'm gonna drag my
sprite and put it into my tutorial gun
and I'm gonna put it at zero like that
I'm gonna set its Saltine layer to
correctors and I'm gonna adjust its
position I would like it to be somewhere
like that when aiming to the right next
thing I want to do is I'm gonna call
that tutorial gun model or something
later now the next thing I want to do is
make prefab out of this weapon so I'm
gonna go and click on my tutorial again
here and drag it into my project view
here and now I can remove it safely from
my Cola tractor and I can actually
disable my koala character from here I
may reuse it again later so now if I go
into my color to the project part select
my koala prefab here you can see I have
correct a handle weapon component and
that component is responsible for as the
name implies handling weapons it is the
component that will make sure you know
that when you press the trigger
something actually happens it connects
your character with its weapon from its
inspector you will be able to optionally
select a weapon to start with and that's
what we are gonna do right here so I'm
gonna go and grab my tutorial gun and
put it into its initial weapon slot that
guarantees that when the character gets
instantiated into the scene it equips
automatically this weapon this component
will also let you specify weapon
attachment so in this case on the koala
prefab you can see we have a weapon
attachment right here into its model and
that's just to transform that let's
you say okay when I attach a weapon to
my character it gets attached to this
plus an offset that you can specify and
it will also be responsible for changing
the weapon and generally that's your
connection to the weapon now I'm gonna
select my prefab again and on its weapon
aim component I'm gonna set its aim
control to Mouse I could have it to
primary movement in which case the
weapon would point at any direction I'm
walking to secondary movement that would
be the secondary joystick by default the
mouse all script and you you could do
that for AI for example have a custom
script that automatically aims maybe at
the closest enemy or stuff like that I'm
gonna go with Mouse I'm gonna set its
weapon rotation speed to zero that would
be instant but you could have a delay as
well you can define here the rotation
mode I'm gonna go with free so that's
free 360 degrees rotation that you could
have straight to divert two rotations
that would be left and right for
directions north east west south and
eight direction with diagonals I can
also decide that I want a reticle on by
the with that for now but I'll show you
maybe later so if I press play you'll
see that I can move my character around
and I as I move my mouse it moves but of
course if I try to click and shoot
nothing happens and actually I get
errors because we don't have an object
puller for our projectile weapon so
that's what we are gonna fix to be able
to shoot to protect our weapons in the
Kogi engine uses object pools so that
recycles your project eyes and make sure
your performance remains decent so I'm
gonna need to create a projectile so to
do so I'm gonna create an empty object
and name it draw your own project I'm
and under it I'm gonna drag my tutorial
project I sprayed which is so this is
here I'm gonna make sure this is at zero
and I want the sorting layer for this to
be corrected so I can see my four data
there we go next
I'm gonna go and add a project a script
to it and also maybe a Collider 2d would
be nice
I could spend I was adjusting this thing
but I'm gonna go and say this is okay I
want mom my bounce based on my Collider
2d so I you could also have your bounce
based on the render size or you know you
don't want them undefined but Collider
or Collider 2d all renderer what you
want to go with I'm gonna say it's
lifetime maximum is like 5 seconds
considering the size of the level and
the sidelines 5 seconds is more likely
to be never met but still I want that
project ID to face its direction I want
its speed to be maybe 50 with an
acceleration of 2 and I'm not gonna
change anything else I'm gonna go also
and add the damage on touch component to
it and that is gonna handle damage when
that projectile hits stuff so I want it
to be able to hit enemies so I select
enemies on my target layer mask if I
wanted to also apply damage to obstacles
I would you know check obstacles as well
I won't click perfect impact that's more
for 3d projectiles that go really fast
speed I'm gonna say it causes 10 damage
that's all not back like so and has an
invisibility duration of 0.5 that's just
fine
and it also takes damage so maybe it
takes 10 damage every time and 2
undo damage I'm gonna had health
component to it
and I'm gonna say this projectile has a
health of 10 so any time it takes damage
like it takes 10 damage it's gonna lose
its cells and sort of die there are a
few more components I want to add the
first one will be a rigidbody 2d this
will ensure that our collisions work
fine so I'm gonna make it kinematic and
I'm gonna say that I don't want it to
ever sleep and I can set the
interpolation to interpolate for example
next I want to have a prevent passing
through 2d and this is a mechanism that
will make sure that we never go through
an obstacle without colliding with it
I'm not gonna say I want to collide with
obstacles and enemies I want to make
sure you know I never go through any of
these and the last component I need is
an mm pull-up object and that will let
the system know that it can recycle this
thing I want the bounds based on the
Collider 2d and the lifetime to be 5
seconds like before now I can drag my
tutorial project out here to make it a
prefab is everything ok
[Music]
yeah I'm gonna change the nut back to
maybe 300 like that apart from that it
looks fine oh yeah forgot to I forgot to
set the else model so I'm gonna go back
here and say on health the model for my
projectile is actually this thing and
I'm gonna apply the changes I think it
should work now on my gun I'm gonna have
also a simple object Pula and this
simple object puller will be basically
my magazine not the actual magazine of
the weapon but like reserve where I put
all the projectors I'm about to shoot
and they recycled they are they get
recycled and that way I don't have like
tons of objects instantiated in the
scene at once so I'm gonna drag my
tutorial projectile into my game object
pool field I could say Mutua is waiting
pools if I had many characters using
that same weapon that way any time
someone tries to shoot the weapon would
request projectile from the pool to
common pool and use it and I'm gonna say
you know I have a pool size of 20 and
that pool can expand if I press play now
you can see we don't have errors anymore
and when I click I have projectiles that
get shot it is a between right now
because my project eyes get shot from
like the center of my koala and they
actually push my koala around so that's
not what we want we would like them to
start from the the top of the cannon or
something
to fix that I'm gonna select my
projectile again and on actually now I'm
gonna select my weapon and on my weapon
on my projectile weapon component at the
bottom of it
I have a spawn pot and I'm gonna say I
want the project I spawn offset to be
free so now if I press play again when I
shoot it is more or less at the right
place I could I guess also offset a bit
the thing so I'm gonna go and select my
weapon and as you can see I have a nice
gizmo here that lets me tweak it so if I
change the Y value I cannot just sit
pretty smoothly I'm gonna go maybe with
2.8 and point to point 15 I could go on
for 4 hours like that I can also define
the projectiles per shot so maybe I go
with free projectiles and I want them to
spread is it is it the y axis we'll see
let's press play again wasn't the y axis
it was the z axis I think let's say 10
on the z axis just play again yes okay
so now I'm shooting free projectiles and
as they go you can see them spreading
I'm gonna go back to one projectile for
the sake of clarity but you get the idea
now so I'm gonna revert that to zero and
one you could decide also to have a
random spread or a very line spread so
it's a puts a lot of freedom rotate
weapon on spread is a good example is
the
kolja EK where it's basically spreading
every shot so it's only shooting one
protect i'll but randomly picking a sort
of position and that also helps with
like the feeling of the gun now you may
have noticed that all projectiles are
going through walls and that's not what
we want so i'm gonna select my
projectile again and make a few more
changes the first one is i want to make
sure my lair is project out and yes i
want that to change on children i want
to make sure my collider is indeed a
trigger
I want my target Liam nice to include my
walls so they on the obstacles layer so
I'm gonna add that one we do indeed take
ten damage every time so every time we
collide with anything we apply 10 damage
and that brings all health to zero so
that's fine we could also shoot at other
projectiles so that's why it does V so
system in place and I think that should
be it
I'm also gonna increase the speed
because they are like super slow right
now so I'm not gonna increase to 200 and
I'm gonna press play again and now when
I shoot you can see that my project eyes
get destroyed when they hit a wall and
they can kill enemies as well so we now
have maybe a very bare-bones in terms of
visuals but at least it is shooting and
working now let's try to improve the
visual quality of all that because right
now it is very lacking we just have
project out that's that randomly
disappear and that doesn't look really
good so to do so I'm gonna drag my
tutorial project are in the scene again
and I am also gonna cheat a bit and take
a shortcut by grabbing a projectile from
the cola demo like the gun bullet maybe
and I'm gonna grab a few stuff from it
so it contains as you can see quite a
few things so we have the top level
which is the logic level we just rebuild
pretty much exactly the same thing we
per level object we have a project time
or health damage on touch we prevent it
from passing through stuff a Collider
and a rigidbody so all that we just
covered we also have smoke trail so when
it moves you see it emits this sort of
nice particles we have the bullet itself
which is just a sprite we have a bullet
impact and that is a particle effect
looking good and we have feedback so we
have a feedback when we hit something
that is damage Abul and we have another
one when we hit something that is
non-dimensional so I'm gonna go and grab
this subject and put it next to my
bullets so I can work with it of course
you could create new effects ah just to
speed things up I'm gonna reuse what I
have here so I'm gonna go and copy my
smoke trail paste it into my tutorial
project I'll make sure it's at zero and
I'm gonna maybe change a bit the color
of this thing so that it sort of matches
what I have in mind for this blue one
I'm also gonna copy the bullet impact
and paste it here like so same thing I
want to make sure it's at the center and
when it explodes
it explodes but right now it is
exploding into a very yellow color and I
want to make that a bit different so I'm
gonna go and change maybe the material
that this thing is using because I think
it's it's using material that is very
yellow I'm gonna duplicate that material
I'm gonna go and drag it into my game
folder like that and I'm gonna change
its color to something more blueish
like this and I'm gonna make it pop I
don't know like this maybe then I'm
gonna go and select my bullet impact
again ranra here so on this one I'm
gonna go and say use this material and
I'm gonna rename it to Doyle projectile
material and the site particles back
they are using black so now if i play my
bullet impact again i have something
that much is much more my blue particle
my blue project i sorry i could change
the shape of it maybe i don't know what
happens if i do that
nothing i could change the lifetime to
maybe point four so now they you know if
I change the right particles it may have
an impact 25 all right so now I have a
bigger explosion I'm gonna change the
shape to what happens if I do a box it
isn't me
what a penetration that ya know not very
convincing I think I'm gonna go back to
a circle I like it like that and so now
I have my bullet impact I have my smoke
trail I'm gonna need a feedback I can
now get rid of my quail egg and bullet
I'm not gonna use it anymore and now
what I want to do is every time my
projectile hits a wall I want to have my
bullet impact play and to do that I'm
gonna use the feedback system so I'm
gonna create an empty game objects I'm
gonna call that damage feedback
and I'm gonna add what is certainly my
favorite piece of code so the mmm
feedback system and what this does is it
lets you on-demand play a number of
things so you could have a cinnamon
impulse you could have chromatic
aberration trigger you could have a
camera shake you could have some light
play in our case we want first of all we
want particles to play and this system
is built in the engine you can use it
with pretty much every ability every
weapon everything everything and I
encourage you to do so because it's
really a lot of fun to play with so in
this case I want a particle to play so
I'm just going to drag my bullet impact
into this and so every time this
feedback gets triggered it's gonna play
these particles and now I need to to
tell my system that every time it takes
it hits something I want it to play that
feedback like that the last thing I want
to make sure I change is the delay
before destruction so otherwise that
feedback will be disabled instantly so
I'm gonna say maybe 1.5 second I'm gonna
apply all these changes disable my
project our press play and now when I
shoot that stuff you can see that I get
this nice explosion which already feels
a bit better and we're gonna make it
even better so now I suggest we turn
things up to 11 by adding a bit more
feedbacks so we're gonna go back into
our prefabs electrode damage feedback
and we're gonna add first of all we're
gonna add the screen shape because that
makes everything better
we're gonna leave I think everything by
default that should be fine we're gonna
also add a freeze-frame so every time it
hits we get a nice freeze-frame
and what else we could have we could
have something else like a flash so
flash is gonna have an impact on a flash
GUI component that we have and it's
gonna turn this the screen to white I'm
gonna turn it 2.1 so that it doesn't
flash too bright but we're gonna make it
blue so it looks like it's related to a
projector and that should be a good
start
we could also add a sound I'm not gonna
do it just to not mess with the
recording but there's a lot you can do
we can also yeah maybe some
post-processing chromatic aberration is
always fun let's go with it
and also maybe some yeah chromatic
aberration no I went with that one
that's not the one I was going for lens
distortion yeah that should that should
do it
so now if I press play again you can see
that every time I shoot I get some
screen shake I get lens distortion I get
something that is much more you know
memorable and it stacks up nicely so
yeah that's how you add power to your
projectors in a very similar way we can
add a muzzle flash to on to a gun I'm
gonna cheat again I think and just put
my I'm gonna clean up the scene I'm
gonna put my tutorial gun in my scene
I'm gonna go and find my koala 2 D my
koala gun and inside the gun you can see
that I have a weapon used feedback so
every time the weapon gets used it
triggers all that stuff including
this muzzle flare feedback that there's
you know some sort of muzzle flash but
also spawns some sort of shell and I'm
just gonna copy that one put it into my
tutorial gun like this adjusts its
position I think it could be a bit more
like that nice I'm gonna change the
color of the particles to something more
blueish this has pretty much zero impact
because it's not the right material
again so I'm gonna go into game right
here and say use this one instead and
probably the same thing for my renderer
right here here we go
and I could also change I guess the
color of the shell will that work I have
no idea I think I've already get some
blue ones I think it's this one's yep
okay so nice nice reuse here and the
last thing I want to do is of course
create the feedback that triggers all
that so I'm gonna create an empty one I
call it shoot the feedback and same as
before I want to add an mm feedbacks
component to it I want to make sure that
every time this feedback is triggered it
plays my particles so that would be
muzzle flare right here I want to make
sure that every time I shoot I have I
should feedback so I'm gonna drag that
into my used mm feedback right here my
slot on my gun what else was I doing on
this one I was doing bloom
hi bloom bloom is a nice idea bloom and
maybe a flash a big one this time so I'm
gonna go with a bust process bloom make
sure it's not too long that seems nice
I'm gonna maybe turn the amplitude up a
bit so you know is it and I'm gonna go
with some sort of camera flash make it
blue and set the Alpha 2.5 maybe that
should be noticeable I'm gonna remove
that call again from the scene I'm gonna
apply my changes to my tutorial gun
disable it press play and now when I
shoot I get a blue flash I get my muzzle
flash and I get my impact so I would say
this is starting to be a weapon that is
enjoyable it is definitely way too much
lens distortion on impact but I'm just
gonna leave it like that because I can
so now that we have a functioning weapon
that looks actually good we're gonna try
and turn it into an ammo based weapon so
the first thing I want to do is remove
from my playable corner I want to make
sure that it doesn't have an initial
weapon anymore
so it starts without any weapon unarmed
and we will need to make a picker for it
just like for this a Kay and we'll need
to make an ammo picker as well so it can
shoot so to create our ammo based weapon
the first thing I'm gonna do is bring my
tutorial gun back in and I'm gonna add
Webb
I'm a component to it I want to make
sure that it matches my inventory names
so that would be koala main inventory
and I'm gonna say ok you'll find ammo
into this inventory and we're gonna come
back to it for the moid and I'm gonna
rename that gun ammo based create a
prefab out of it and I'm gonna make it a
variant
now we're gonna disable that for now
next we're gonna need a picker and we're
gonna need an ammo picker so I'm gonna
copy these two and I'm gonna paste them
and I'm gonna move them over there
I prepared some sprites for it so
instead of it being a rifle it's gonna
be a nice T 4 tutorial and the same
thing for the ammo I prepare the sprite
that looks like this so now they look
good but then if I if I were to pick
them they will still give me a rifle and
they will still give me rifle ammo so
I'm gonna go here and here I have my
craw - koala rifle ammo scripted objects
and these are what define you know that
that is what is stored into the
inventory system by the way the
inventory system is the inventory engine
it's a separatist as usually but it is
being provided as a gift to top down
engine users it is built into the system
if you want to know more about it and
learn more about how to create items and
how they work there is a complete
documentation for it complete video
tutorials as well so don't hesitate to
check them out
I just duplicated these two items so I
can sort of reuse them and I'm gonna
move them into my game folder I also
want to create a folder
resources and the name is super
important like don't try anything else
it won't work so you put them always
your script of objects always in a
resources folder so that unity can pick
them up and I'm gonna rename that into
tutorial gun and tyonne
ammo and I want to make sure that my
item ID for both items matches my file
name that is pretty cool
alright so I'm gonna go through each
field so this is a weapon it is capable
its name is not no more assault rifle
but tutorial gun it's short description
would be this music and tutorial and
yeah we would have a long description
for it we can move the object we can
swap it I didn't create an icon but
that's where you would put yours
we're gonna need prefab for it so let's
actually create these so to do so I can
grab my corner rifle picker and actually
I'm I'm gonna pack these and rename them
into tutorial you can pick ax and ammo
pika I want the tutorial gun pika - give
me the tutorial gun and I want my
tutorial I'm a picker to give me
tutorial ammo these are fine so now I
can make prefabs out of these same for
the ammo pika
and if I go back into my item I can now
say that when I drop my gun from my
inventory I want to drop and actually
can pick a prefab
it is a weapon it should go into the
Koala weapon inventory if possible and
on equip it should give me a Cheeto
Niall gun ammo based variant now I'm
gonna go into my tutorial I'm on and do
the same thing so this is a tutorial I'm
oh not what this was description here I
can multi object again swap it
same thing I didn't create an icon for
it you would put it here when it gets
dropped it drops tutorial I'm a picker
this one and I think everything is fine
apart from that that seems alright you
know resources we have everything that
looks fine so now if I press play you
can see that I start the game empty
ended I have nothing in my inventory if
I pass my picker I automatically pick my
weapon you can see that the icon in the
top right corner is still the one from
the AEK because we didn't create one for
it but it is just right there
I can unequip my weapon if I select it
you can see it is the tutorial gun with
the long description and so on
I can equip it again and if I pass on my
ammo I don't get any ammo and that is
because that is because I probably made
a mistake somewhere that is because our
weapon
to tell you these names are not easy to
read yeah I forgot to put the my D which
would be which would be the one found
right here so that is tutorial mo so if
I go back to my weapon my D press play
again sorry about that
I can now shoot and as you can see when
I shoot I consume ammo I'm gonna try to
empty this because we went to all that
trouble so now wow that is really a nice
feature unity being able to zoom like
that very useful so I can shoot pretty
much everywhere I drop and that's it I
emptied my gun I don't have em anymore
so yeah if you made it this far you now
have a weapon that shoots your own
project tiles that you can store in your
own inventory and you can switch to
other weapons and switch back to yours
and it it is really the start of a lot
of fun
the weapon system in the engine is
actually quite powerful and will let you
create all sorts of weapons we've seen
how to create a projectile weapon in
this tutorial we saw how to create a
marijuana in the animation tutorial and
it's not even the end of it I suggest if
you're interested in weapons that you
have a look at the weapons documentation
that I have right here and as you can
see it contains a lot of stuff so we've
seen how the character handle weapon
works we've seen weapon classes project
their weapon melee weapons you'll find
an example to create in details
many weapon we've been over I think in
details over the projectile weapons and
projectiles timing something I think I
didn't mention that much but if we
select our tutorial gun in the
projectile weapon right here you can see
that you can define a number of things
like the delay before use so that would
be a time between the moment where you
press the trigger and the moment the
weapon gets actually used you can have
that being interrupted by or not so if
you decide that it is interrupted boom
if you release the button then the shot
won't happen think of it like the
charging Mega Man maybe we can have
magazines as well so you could decide
that your you need to reload your gun
like I collected 50 bullets for this a K
but my magazine could only be maybe 10
and then I need to reload and what
happens when you reload so there's a lot
of stuff like that if we go back to the
documentation there are also combo
weapons I suggest you look at the two
sword examples that are included in the
engine one is in the Kohala to the
demoscene and the other one is in the
minimal sword free demo scene you can
also have laser sight I think pretty
much every weapon in the loft 3d demo
have this definitely the ones from the
enemy's weapon aim we've seen that mo
we've covered it inventory as well
weapon okay so that would be specific to
3d and that lets you you know connect
your hands to your gun and you can see
an example that in the loved 3d demo
scene and ammo display it would be that
thing in the corner here so of course
you can risk in that to whatever you
want thank you for following that
tutorial sorry about the tiny mistakes
here and there but this is all very
improvised oh I hope you learned
something new today and I'll see you
next time
by