Getting Started With 2D Game Kits – Using The Inventory System [7/8] Live 2018/2/21


In this kit we’ve also provided
an inventory system. Let’s take a look
at the inventory system now. If we select “Ellen” and scroll all the way down
in the Inspector, we have an “Inventory Controller”, and this controls
what Ellen can collect. We can see that she has several
“Keys” that she can collect, and she’s already
holding a “Staff” and a “Gun”. These are already set up for the way
that they are set up for the game. In the game example, you can collect three keys
to open the door, and eventually collect
a staff and a gun. I’m going to just quickly… with “Ellen” selected, I’m going to
CTRL + click on Cameras and move Ellen
to this top area here, just so I can quickly
test that game design that I’m going to show you now. Let’s add a key
into this environment. I’m going to click and drag, from the “Interactables” folder,
in the Project window, and add the “Key”. Now that we have the key added in,
it’s just an object. If we look at the Inspector,
there’s nothing special about it: it’s just set up
as a regular object. If we press Play
and run into it, nothing will happen. It’s just an object, it has a nice floating animation,
and that’s about it. To make sure
that Ellen’s “Inventory Controller” expects this key to be collected, we need to make the key
an inventory item. And the way we do this is, with the key selected
in the Hierarchy, in the Inspector,
let’s click on Add Component and start typing in “inventory”. And here we can see the “Inventory Controller”
and “Inventory Item”. I want the “Inventory Item”, because it is an item
for the inventory. Now you can see
this big green circle that’s surrounding the key. This is actually
the key’s Collider. When Ellen walks into this area, that’s how we know
that she’s walked into it and can collect it. This is very large right now,
it’s not the size of the key, so I want to make it
the size of the key. In the Inspector,
on the “Circle Collider 2D”, I can select this “Radius” option and click and drag this down a bit, so it’s about the size… I think that’ll do…
about the size of the key. There we go. The “Inventory Item”
has this “Inventory Key”. Let’s not get confused
with this being a “Key”. This isn’t the same as the object. This is expecting
a unique identifier. So, a specific word that we set up for Ellen’s inventory
to recognize the object. Let’s take a look at Ellen
and see what she expects. If we click on “Ellen”
in the Hierarchy, scroll down again
to the “Inventory Controller”, we can see here that
in the sections we have “Key1”, and then the word “Key” here. And this “Key1” should match… If I change this to “key”,
it also changes what it is in here. But I want to keep it a “Key1”. This is the unique identifier
that we set for it to recognize an object
with the same name. If we go to “Staff”, the “Staff” also has
a unique identifier, and that’s “Staff”. We want to make sure that the physical key
that we have in the Scene has the same
unique identifier of “Key1”. Let’s go back
to the “Key” GameObject… and, in the “Inventory Key”, I’m
going to type in “Key1”. There we go. Right now
it’s got another “Layers” option, so anything that has
an Inventory Controller can collect this,
which isn’t what we want. Imagine if we put
an Inventory Controller on a Chomper and it walked past. It would be able
to collect the key, which is definitely
not what we want. We want to have the key. If I select “Nothing”
to clear all the selections, and then select the “Player”, then Ellen
will be able to collect it. However, if we play… nothing happens. I think this is being collected
in her inventory, but I don’t know that for a fact. There’s no feedback here
for the player. I’m going to exit Play mode and trigger some
events that happen. The first thing I’m going to do
is trigger a sound. Triggering a sound for this
is slightly different than earlier. If it’s an “Inventory Item”,
there’s a “Clip” option here which is expecting an “Audio Clip”. And we can find audio clips
in the Project view. And if we expand the “Audio”… and the “Interactables” folder, these are all the audio clips
associated with “Interactables”. And what I want is… there’s no single one
for a key specifically, but there is
a “GenericPickup” sound, so I’m going to click
and drag this in and now when I press Play, when I collect this, it should play
that “GenericPickup” sound. Let’s connect this
to something actually happening when we’ve collected it
in the inventory. Usually the stereotypical thing
is collecting a key to open a door, so that’s what we’re going to do. I’m going to exit Play mode and I’m going to grab a “Door”
from the “Interactables” folder. I’m going to put it
just next to it, just so we can see. Now, right now, just as before
with the pressure pad, when we walk over and collect this, we can hear the sound,
but the door doesn’t open. I’m going to exit Play mode and look at Ellen’s
“Inventory Controller” again… I’m going to scroll down
right to the bottom… and here is the event
when “Key1” is added. So, “On Add ()”… I’m going to press the “+” button
to add something to the list. And I’m going to drag the “Door”
into the empty slot. And, just like before with
the pressure pad, I’m going to find
the “Animator” section and the “Play (string)” function. And in that, I’m going to type in
“DoorOpening” again. And now when I collect the key, when it lands in her inventory, it should trigger that
in animation. Collect the key,
and now it’s opening the door. We need to do one last thing to indicate that the key
has been collected. You’ll notice
that when we run into it, it doesn’t disappear. And a good indicator to the player
that it’s been collected, other than an action happening, is if the key
doesn’t appear anymore. Let’s select the “Key”, and, in its “Inventory Item”, we have an option
to “Disable On Enter”. And this will turn off the object as soon as you
walk into it and collect it. If I press Play now,
when I walk into it, it’s going to disappear. And that looks
a bit better for the player, so they know that
they’ve actually collected the key, but if I was to have this door
somewhere else in my level and my key
somewhere else in my level, there’s another thing we can do
to indicate to the player that we have
the key still collected. We can do this with adding some UI. I’m going to exit Play mode, and in the Project window, still in the “Prefabs” folder,
we have “UIPrefabs”. And these are bunch of UI elements
that we’ve already set up. And what we’re looking for
is the “KeyCanvas”. Don’t get confused
with the “KeyIcon”: that’s just an individual icon. We want a canvas
to display over our screen. I’m going to click
and drag this “KeyCanvas” into the “UI” section,
in the Hierarchy. Now, this sectioning
doesn’t affect anything, it’s just so we can
keep things nice and tidy if we end up having a fairly large
level with a lot of objects: we can quickly find what we need. Now, if I press Play, I have the empty slots
on the top right, and if I walk into the key now, it automatically detects
that a key has been collected. The way it does this is by using
that same unique identifier that the inventory does. If we look at the “KeyCanvas”
in the Inspector, we have a “Key UI” component here
and we have “Key Names”. Here we’ve got
some separate “Elements”. This is just a bucket of options
that it’s looking for. Here we’ve got “Key1”, and when “Key1” is collected,
it’ll display that changed icon. We also have… Since we have an example game
with three keys to collect, we’ve got “Key2” and “Key3”. We can actually remove these and stop them
from displaying for this level. The way to do that is, where it says “Element 1”, we’re going to right-click
and “Delete Array Element”. And then, on “Element 1” again, this just moves up the list, we’re going to
“Delete Array Element”. And now we’re just left
with one key that it’s expecting. So, if I press Play now, we should have one icon
in the top right, and when we collect it,
that one icon will be filled.

Leave a Reply

Your email address will not be published. Required fields are marked *