Simple Inventory (PART 1: Adding Items to Player Inventory)
Summary
TLDR本视频教程介绍了如何创建一个简单的库存系统,让玩家能够存储和查看所有物品。首先,我们设置了场景,包括第一人称玩家角色和之前的教程中创建的模板。接着,我们创建了几种基本物品,如草块、石头和泥土,并为它们制作了简单的材料。然后,我们将这些物品转换为预制件,并为它们创建了可复用的有限状态机(FSM)模板,用于定义物品行为。通过设置物品的精灵图标和是否可存入库存的布尔值,我们为每个物品配置了FSM。最后,我们通过玩家的交互模板与物品的FSM进行交互,实现了物品添加到玩家库存的基本功能。视频承诺在后续教程中教授如何在UI中展示库存以及如何将物品放入其他容器。
Takeaways
- 😀 视频教程介绍了如何制作一个简单的库存系统,让玩家可以存储和查看他们的物品。
- 👷 使用Unity引擎和Playmaker插件来创建这个系统,需要有Unity和Playmaker的基础。
- 🏗️ 首先创建了一个第一人称玩家角色,并使用了之前教程中制作的角色控制器模板。
- 📦 制作了一个箱子和地面,将它们设置为预制件(Prefabs),并创建了世界(World)父对象。
- 🔨 创建了三种物品:草、石头和泥土,并为它们制作了简单的立方体模型和材料。
- 🛠️ 将每种物品都转换成了预制件,并为它们创建了FSM(有限状态机)模板,用于控制物品行为。
- 🎨 为每种物品设置了不同的颜色和图标,以便于玩家在游戏中识别。
- 🔑 为物品设置了“可放入库存”的布尔值,以决定哪些物品可以被玩家拾取。
- 🤖 通过FSM模板,创建了一个通用的物品模板,可以被草、石头和泥土等物品使用。
- 📚 玩家的交互模板(Interact Template)通过射线检测(Raycast)来与物品互动,并触发事件。
- 📦 通过设置物品的刚体(Rigidbody)和物理层(Layer),确保物品在被添加到库存后不会影响游戏世界。
Q & A
视频中讲述的是什么主题?
-视频讲述的是如何制作一个简单的物品库存系统,玩家可以在其中存储和查看他们的物品。
在Unity中创建物品库存系统前需要哪些基础设置?
-在创建物品库存系统之前,需要有第一人称玩家角色、玩家移动模板和玩家交互模板,这些模板都利用了角色控制器。
视频中提到的'Cinti资产'是指什么?
-Cinti资产指的是Cinti公司提供的Unity资源包,视频中使用了Cinti的多边形原型包中的地面和箱子模型。
如何将Cinti资产中的箱子模型分离出来用于自定义?
-通过在Unity中右键点击箱子模型,选择'Unpack Prefab',然后重命名分离出来的模型,例如将箱子和箱子盖子分别命名为'chest base'和'chest lid'。
为什么需要将物品制作成Prefab?
-将物品制作成Prefab可以方便在游戏世界中多次使用,并且可以为它们添加FSM模板来控制它们的行为。
视频中创建了哪些类型的简单物品?
-视频中创建了三种类型的简单物品:草(grass)、石头(stone)和泥土(dirt)。
如何为这些物品设置不同的材料?
-为物品设置不同的材料需要创建不同的材质,并为每种物品分配相应的颜色,然后将这些材质应用到对应的物品模型上。
为什么需要为物品创建FSM(有限状态机)?
-为物品创建FSM是为了定义物品的行为,例如当玩家与物品交互时,物品可以发送事件到玩家的库存系统。
在FSM中设置的'item sprite'变量和'can stash'布尔值分别有什么作用?
- 'item sprite'变量用于设置物品的图标,'can stash'布尔值用于标识物品是否可以被添加到玩家的库存中。
如何在游戏中实现物品与玩家交互并添加到库存?
-当玩家与物品交互时,物品的FSM会发送一个'inventory add'事件到玩家的库存FSM,然后玩家的库存FSM会将物品添加到库存数组中。
视频中提到的'ignore all'层级的作用是什么?
-'ignore all'层级的作用是让在这个层级上的所有物品对物理系统不可见,即玩家不能与之交互,射线也无法击中它们。
如何在游戏中隐藏已添加到库存的物品?
-通过设置物品的可见性为不可见,并将物品移动到'ignore all'层级,来实现在游戏中隐藏已添加到库存的物品。
接下来的教程中将会学习哪些内容?
-接下来的教程中将会学习如何在游戏中显示库存UI,以及如何将物品放入其他容器,例如视频中提到的箱子。
Outlines
🎮 制作简单的库存系统
本段介绍了如何创建一个简单的库存系统,用于玩家存储和查看所有物品。视频展示了一个第一人称玩家角色,使用之前教程中创建的模板和组件。接着,作者创建了一个空的父对象作为世界层,并展示了如何将箱子和地板从预制体中解包,然后重命名为'chest base'和'chest lid'。为了构建库存系统,首先创建了三种立方体物品:草、石头和土,并将它们设置为预制体。此外,还为这些物品创建了简单的材料,并在新的文件夹中管理这些预制体和材料。
🛠 为物品创建FSM模板
这一段详细说明了如何为物品创建一个可复用的有限状态机(FSM)模板。首先,创建了一个名为'item'的FSM,并将其保存为'simple inventory item'模板。模板中定义了两个输入变量:'item sprite'和'can stash',分别用于设置物品的精灵图标和决定物品是否可以存入库存。作者还展示了如何将模板应用到草、石头和土这三种物品上,并为它们设置了相应的精灵图标和材料。
🔗 物品与玩家交互的FSM设置
本段讲述了如何设置物品的FSM以响应玩家的交互。首先,为物品创建了一个'init'状态用于初始化操作,并设置了一个过渡到'standby for interaction'状态,该状态等待接收'interact'事件。当玩家与物品交互时,物品会发送一个事件回给玩家的FSM,通知玩家将该物品存入库存。作者还介绍了如何设置玩家的FSM以响应'inventory add'事件,并在库存中添加物品。
📦 将物品添加到库存并从世界中隐藏
在这一段中,作者展示了如何将物品添加到玩家的库存中,并从游戏世界中隐藏这些物品。通过设置物品的'visibility'为不可见,并将其移动到一个名为'ignore all'的层,以确保物品不会与游戏世界中的其他物体发生任何物理交互。此外,还设置了物品为'is kinematic',以防止Unity进行不必要的物理模拟。通过这些步骤,玩家可以拾取物品,而物品在世界中的存在则变得透明,不会影响玩家的移动或游戏的物理效果。
📚 完成简单库存系统的基础设置
最后一段总结了如何完成简单库存系统的基础设置。作者演示了玩家如何通过点击操作将草、土和石头添加到库存中,并展示了物品在世界中被隐藏的效果。玩家可以自由地在这些物品上行走,而不会被它们阻挡。此外,作者提到在后续的教程中将教授如何将库存显示在UI上,以及如何将物品放入其他容器,如箱子。
Mindmap
Keywords
💡库存系统
💡立方体
💡预制体
💡物理材质
💡有限状态机(FSM)
💡射线检测(Raycast)
💡游戏对象变量
💡数组变量
💡玩家交互
💡UI显示
💡容器
Highlights
本视频教程将教授如何制作一个简单的库存系统,让玩家能够存储和查看他们的物品。
使用Unity的第一人称玩家模板,包括第一人称视角模板、玩家移动模板和简单的玩家交互模板。
创建一个空的父对象命名为'world',用于存放场景中的物体。
制作一个箱子和箱子盖,将它们重命名为'chest base'和'chest lid'以清晰区分。
创建草、石头和泥土的立方体物品,并为它们设置不同的材质和大小。
将草、石头和泥土的立方体转换为预制体,以便在世界中多次使用。
创建'simple inventory prefabs'和'simple inventory materials'文件夹来组织资源。
为每种物品创建不同的材质,例如棕色的泥土、绿色的草和灰色的石头。
使用Playmaker创建一个名为'item'的FSM模板,用于物品的通用逻辑。
设置物品的精灵图和是否可以放入库存的布尔值。
为每种物品设置默认的'can stash'值为true,并分配相应的精灵图。
为物品添加刚体组件,以便在场景中进行物理交互。
使用射线检测玩家与物品的交互,并触发交互事件。
创建一个库存FSM,用于管理玩家与物品的交互和库存添加逻辑。
实现物品被添加到库存时的逻辑,包括设置物品的可见性和物理交互性。
创建一个'ignore all'层,使库存中的物品不与物理系统交互。
设置物品在被添加到库存时变为不可见,并保持静态以避免物理计算。
通过测试,确保玩家可以拾取物品,并且物品在场景中变为不可交互。
接下来的教程将教授如何在UI中显示库存以及如何将物品放入其他容器。
Transcripts
hello and welcome to the simple
inventory tutorial in this video we're
going to be learning how to make a
simple inventory system a place where
the player stores and views all of their
items let me just take you through the
scene really quick i have this player
here which is a first person player and
i'm just using the setup that we've made
in previous tutorials so this has our
first person look template this player
move template and a simple player
interact template that's all making use
of the character controller it isn't
completely necessary but if you want to
follow along with this you can go check
out the tutorials for that first person
controller links are in the description
okay so i have that and then i have a
couple of cinti assets in here and then
i have a couple of cinti assets in here
i have the floor from cinti's polygon
prototype pack as well as this chest and
the chest and the floor are currently
prefabs but what i want to do is i'm
just going to right click on the floor
create an empty parent and i'm going to
rename this to our
world
okay so it just has
level stuff in it and this chest i'm
gonna right click and i'm gonna unpack
it completely okay so that means it's
detached from the prefab asset we have
now i'm gonna rename this
as our
chest
base
and then rename this as chest lid just
to see things a little more clearly now
to start with an inventory system you
need items to actually put in your
inventory so we're going to start by
making some cubes so i'm gonna go ahead
and make a cube we'll call this one
grass
and we'll call this one
stone
and we'll call this one
dirt
okay now i'm just making
some simple materials here
okay so these are just going to be cubes
i'll change the scale to like
5.5.5 just make them a little smaller
but you can actually use whatever you
want
any type of items we'll do just fine
making these cubes here is just sort of
simplest example that we can work with
maybe you could just as easily be doing
this system with a sword
or
a health pack a gun all the common
things
doesn't matter we're using cubes here
make things easy on ourselves okay so we
have grass stone and dirt now what we
want to do is make each one of these
into a prefab that way there could be
multiple out in our world and we could
slap some fsm templates on them so they
behave accordingly i have a little
folder here for the scene that i'm
currently working in this simple
inventory so in here i'm going to create
a new folder and call this
simple inventory
prefabs and i'm going to create another
folder
call this one
simple
inventory materials
okay so now in the materials i'm going
to create
a dirt material and it's just going to
be
brown
let me turn the smoothness down
okay and i'll pop that on our first one
and then i'm going to duplicate this
hitting ctrl d
renaming this to grass
and i'm going to change this to green
pop it on there
and then duplicate that call this one
stone
and change it to
gray
pop it on there i'm gonna go into the
prefabs folder and drag in our dirt
our stone and our grass then i'm going
to remove them from the scene so now we
can work on them here let's start with
the dirt okay so i'm going to open up
our prefab by double clicking on it and
selecting the asset itself over here in
the hierarchy i can start working on an
fsm for it so moving this over i'm going
to right click add fsm we're going to
call this fsm item
okay because it's going to be a template
and we want to be able to use it on the
other ones for the grass and the stone
as well so we're going to make something
here that is generic but with a few
values that we could change per item
okay so
the first thing that i'm going to want
to do is make it so
so i'm going to right click in here i'm
going to go save template okay so i'm
going to call this a simple inventory
item
okay and use the save template in csm
yes
now the name of the template file is
simple inventory item but the name of
the fsm is item okay and it's an
important distinction to make when we're
referencing the fsm it'll always be
referencing this name item okay so if we
clicked edit template now we're inside
here if i go to the variables
i'm going to create a new sprite
variable called item sprite
and we're going to make it an
input variable now it's exposed over
here in our inspector so even though
this is a reusable template we can set a
unique value for the item sprite and
then i'm going to give it a bool value
we're going to call this
can
stash
in the tooltip i'll just write here true
equals
can stash
into inventory
false
equals
can not stash into inventory
okay so if you have your mouse over here
it tells you a little tool tip
this reminds us what this bull is for
and make this an input as well and that
way for some items you could make it so
you can interact with them in the world
but maybe you don't want them to be able
to get picked up and added to an
inventory say like a chair it would be
sort of weird if you were able to add a
chair to your inventory okay so this can
stash for this dirt should be set to
true and i think
a good way to go about this would be to
make the default input variable as true
since most of the items we'll be making
will be stashable for our inventory okay
so the way you could do that is you
could remove it from the inputs and then
change its value to true
and then make it an input and now
whenever we add this template the
default value for can stash will be true
okay this is looking good so i'm saving
that and i'm going to add the component
rigidbody
that way we can just throw a few of
these in our scene
okay i use gravity has the box collider
already this looking good saving it and
i'm going to hop over to grass
and i'm going to right click to add fsm
use template
and we're going to use our simple
inventory item okay you'll see that over
here our can stash is already set to
true and our item sprite doesn't have
anything yet we'll put something there
in a moment but let's just call this
item okay we have to change that saving
it and hop over to stone do the same
thing use template
and our simple inventory item changing
this to item
and yes we want it to stash okay so that
all looks good to me so i've gone and
prepared these
sprites
okay so this is a single image that just
has
sprite for
our dirt our grass and our stone cubes
okay so this isn't the video that we're
going to learn how to chop up sprites
and all that but just know that what you
could do is you could bring in an image
for your icons right it doesn't have to
be one single image containing all of
them it's also just as easy to bring in
an image for
for just one at a time the only thing
that you really need to know is that
whatever image you bring in you're going
to want to select it and then come over
here to the texture type you want to
select sprite 2d and ui and when you're
done with that you'll hit the supply
button okay but yeah i've gone and
chopped up mine since mine was three in
one and so i have all the individual
sprites here so for me
what i'm gonna do is i'm gonna select
this stone and over here in the item
sprite
i can select the stone one
and drop it in right here
and i've actually just realized that
somewhere in the middle of all this i've
named my grass block stone so what i'm
going to do is actually i'm going to
keep this as stone i'm going to drag and
drop this stone
sprite icon
into this item sprite value okay so it's
using that now but i am going to
change this material okay so that should
be stone
i could save that okay so that's our
stone right it's gray has the item for
stone now i'm gonna go to grass
and i'm gonna drag and drop this grass
icon in there
and just make sure that i put this grass
material on there
saving that then i'm gonna go to dirt
and this last one
i'm going to drag and drop this
dirt sprite icon in here
that's looking good saving it okay
now this has a rigid body the other ones
did not so i'm just going to add a rigid
body on those as well
add component rigid body save it
stone add component rigid body save it
so now they all have their rigid bodies
and their item fsm and they all have
their sprite assigned to them okay so
those are all of our objects dirt grass
and stone next we're going to take the
player and we're going to go over to
this
interact
template okay so our interact template
if we edit it
we can see that what it's doing is it's
waiting for us to click because that's
fire1 we're using unity's input axes but
you can use unity's new input system or
third-party assets like rewired whatever
it is we're just looking for
some player input
to fire off to the next state okay okay
so whatever input system you're using
after you press the button for interact
we go here to get object has this
raycast and what it does is it gets
whatever object you're looking at this
object hit
and does test whether or not you hit
something if you did then it sends this
event here this object hit okay and it
sends an interact event this interact
event is a this interact event is a sort
of pillar of the designs that we use in
a lot of the tutorials here as well as
the templates available on the ecosystem
so if you don't know anything about how
our templates and this interact event
works you should also go check out the
tutorials for that stuff there are links
in the description okay but the gist of
it is yeah we just shoot a raycast out
in the world and whatever thing it hits
we send an event to it okay so this
interact event is what gets sent to it
that means that we can come over to our
items and if we select this template we
can come over to the first state and
we'll just name this
init
that's just a little state where we can
do some initialization stuff but i'm
going to add a transition to it and send
off to another state okay so this one's
just for later if we want to add stuff
to this but this new state that it
transitions off to this is where it's
standby for interaction
and on this date we're going to add a
transition
custom events
interact event okay so this item is
going to be waiting here for an interact
event to be sent so that's when the
player interacts with it this event gets
sent and the transition fires off to the
next state where it's actually going to
send an event back to the player so if i
go to the action browser
and i'm just going to go ahead and dock
this right here
i'm going to move my project tab back
here and move that there we go just so
we have some more room so what's going
to happen is in here it's going to
tell player to stash this item
okay so we're going to send
an event to the player the target that
we want to send to we're going to set as
a game object fsm the game object we're
going to specify as a new variable that
we'll call
player and that's because if i save this
really quick and i go back out to the
scene i select the player
go down to our interact fsm edit it
you'll see that when we do interact
we're also setting
the variable name player on the object
hit and it's setting it as the player
itself this player value okay which
happens up here in initialization it
gets itself okay that way whenever we
interact with things there's a way for
the thing that we're interacting with to
reference us as well a very easy and
direct way that it could start talking
to the player okay so if i go over to
our item again
okay the player the event that we want
to send is going to an fsm on the player
that we'll call
inventory and the event we want to send
is a new global event that we can call
inventory
space space
add
let's just test if this works right now
nothing's really going to happen other
than
sending this inventory add okay so i'm
going to copy this text
the name of this fsm because we haven't
made it yet and i'm gonna go back out
here select our player
i'm gonna add a new component it's an
fsm i'm gonna paste the name in here
inventory and open up our inventory fsm
and in here i'm gonna right click
add global transition custom events
inventory add
okay so what we should see
if i put a cube out here
if i select our player and we just take
a look at our inventory fsm when i run
this right now
press play and i'm going to click on the
cube
okay there we go you see that on our
inventory it fired off at and if you
select the dirt you'll see that indeed
it did this action right here where it
tells the player to stash this item okay
now let's add a little bit of
functionality to that so i'm going to
remove the dirt and we're going to go
ahead and
add
in our simple inventory item template
something here in the initialization
state we're going to do a get
owner
and the owner is
this item
okay now down here in the tell player to
item we can do is set fsm game object
okay
here's where we could tell the player
which item they're adding to their
inventory so i'm going to specify the
game object the player
the fsm name is inventory and the
variable name we'll call it
item to add
okay and we're going to set the value as
this item
so i'm going to take this item to add
text i'm going to copy it
saving this i'm going to go back out to
my scene and player
the variables we're going to make a new
game object variable called item to add
okay so back here in the state in item
to add what we could do is
do an array add
okay now this array add we're going to
have we're going to make a new variable
it's going to be an array variable we'll
call this our
inventory with the capital i
and the array type
is game object
so now if i go over here i could select
our inventory array and the value that
we want to add
is item to add this should be the very
beginnings of our inventory system i
should be able to come over here and
grab our
dirt
our grass and our stone
okay i'm just going to grab all these
and scooch them up
and let's select our player
and go to variables inventory i'm going
to make this an output variable and that
way down here we could see the inventory
populating so we could see it changing
over here in real time so hit play
you'll also be able to see over here
okay now
i'm gonna go ahead i'm gonna click this
grass cube
you can see that our inventory added the
grass cube you can see it down here
okay it's still just sitting there okay
so i can come over to the dirt cube
click that now we have grass and dirt
come over to our stone click that now we
have grass dirt and stone great so this
is looking pretty good
stop this so the next step would be to
sort of remove these cubes
from the world in a way or at least make
their presence sort of unknown
we don't want to delete them
because we want them to stay on our
inventory but the inventory array on the
player is more of just an idea it's just
sort of an organizational thing it's not
an actual place in the 3d space of our
scene so they're not going anywhere
that's why they just sat there but they
populated in this list the way you build
the illusion that something has been
added to your inventory
is by changing what actually happens
when you do this little inventory ad so
we're going to say
add to array
i'm going to add a finish transition and
send down here
where you
hide item from world
okay and then here is where you could
put something like set visibility
okay and you could set your
item to ads visibility to false
okay so having this box unchecked means
that your item will become invisible
reset on exit i'm going to uncheck that
we want it to stay invisible until we
tell it explicitly to become visible
again another thing we want
is to avoid being able to like bump into
them or raycasts to hit them or any sort
of physics interactions
in any sort of way we don't want these
getting in the way they're like just
some invisible ghost item so what you
could do then
is set layer
okay and let's just assume that all of
our items are on the default layer to
begin with right like most of these like
they are right now if i select these you
can see that they're on their default
layer
but we could change it
and you might be tempted to use
something like ignore raycast or
something like that and that could work
but i think to be safe what we want to
do is come over here to layer
and we're going to add a layer and we're
going to call this one
ignore all now this ignore all layer if
we come over to our project settings and
come over to our physics section here
you could change your collision matrix
in a way that ignore all
doesn't interact
with anything so this ignore all right
here we want to uncheck everything that
it's on and this bottom one right here
we only need to uncheck that one so
basically this means that everything on
the ignore all layer is untouchable by
the entire physics system for every
other layer okay that means we can't
bump into it that means raycasts can't
hit it all that sort of good stuff okay
so
if i select the player
and i'm just going to scooch this down
again so we can see the inspector
we've set the visibility to invisible
we've set the layer to ignore all
oop and not get owner we actually want
it to be our item tab okay so
save that give it a little test spin
okay so i'm going to click on that it
goes away not like bumping into anything
there right i could totally move freely
so you can see if i walk over here
my player capsule
jumps over them right it kind of the
step height takes care of itself and
it ends up walking over them
but right here there's nothing if i
pause this
it's like
select our graphs
you can see that what happened was
because it was ignoring everything it
actually fell through the world so it's
actually i make this bigger if i look
down
see it's
it's just falling continuously down
there
maybe a good thing to do too would be to
tell it to
put on a
set is kinematic okay so we're going to
specify our
item to add and we'll say is kinematic
and what that'll do is it'll just make
sure that the object stays put
doesn't matter too much but it at least
prevents
unity from having to do this extra work
of
simulating the physics of it falling
when it really doesn't need to so
setting to his kinematic relieves some
of that work
okay so i'm going to press play and if i
click it
and i pause it
and grab my grass
you can see that it's still sitting here
nicely
okay still there in the world and
should be able to move
yep and you can see that in
this screen
i'm totally walking right through it
but as you can see in the game screen
nothing is stopping me from walking
through it this is exactly what we want
so the first part of our inventory
system
is
ready to go okay so that's how you can
set up the beginnings of our simple
inventory system in the next tutorials
we're going to be learning how to
display this in an inventory ui as well
as putting items in other containers
like this chest here be sure to check
out our other videos to learn all the
various features of playmaker links to
more learning resources are in the
description
浏览更多相关视频
Unreal Engine 5 RPG Tutorial Series - #14: Equipment System
Unreal Engine 5 RPG Tutorial Series - #4: Assassinations
Unreal Engine 5 RPG Tutorial Series - #5: Player Stats - Health, Stamina and XP
How to make a DOOM CLONE in Unity || E1M4 Player Health and Armor
Unreal Engine 5 RPG Tutorial Series - #20: AI Behavior Trees Patrolling
How to make a DOOM CLONE in Unity || E1M1 First Person Player
5.0 / 5 (0 votes)