Simple Inventory (PART 1: Adding Items to Player Inventory)

Hutong Games
20 Sept 202220:47

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

00:00

🎮 制作简单的库存系统

本段介绍了如何创建一个简单的库存系统,用于玩家存储和查看所有物品。视频展示了一个第一人称玩家角色,使用之前教程中创建的模板和组件。接着,作者创建了一个空的父对象作为世界层,并展示了如何将箱子和地板从预制体中解包,然后重命名为'chest base'和'chest lid'。为了构建库存系统,首先创建了三种立方体物品:草、石头和土,并将它们设置为预制体。此外,还为这些物品创建了简单的材料,并在新的文件夹中管理这些预制体和材料。

05:00

🛠 为物品创建FSM模板

这一段详细说明了如何为物品创建一个可复用的有限状态机(FSM)模板。首先,创建了一个名为'item'的FSM,并将其保存为'simple inventory item'模板。模板中定义了两个输入变量:'item sprite'和'can stash',分别用于设置物品的精灵图标和决定物品是否可以存入库存。作者还展示了如何将模板应用到草、石头和土这三种物品上,并为它们设置了相应的精灵图标和材料。

10:00

🔗 物品与玩家交互的FSM设置

本段讲述了如何设置物品的FSM以响应玩家的交互。首先,为物品创建了一个'init'状态用于初始化操作,并设置了一个过渡到'standby for interaction'状态,该状态等待接收'interact'事件。当玩家与物品交互时,物品会发送一个事件回给玩家的FSM,通知玩家将该物品存入库存。作者还介绍了如何设置玩家的FSM以响应'inventory add'事件,并在库存中添加物品。

15:01

📦 将物品添加到库存并从世界中隐藏

在这一段中,作者展示了如何将物品添加到玩家的库存中,并从游戏世界中隐藏这些物品。通过设置物品的'visibility'为不可见,并将其移动到一个名为'ignore all'的层,以确保物品不会与游戏世界中的其他物体发生任何物理交互。此外,还设置了物品为'is kinematic',以防止Unity进行不必要的物理模拟。通过这些步骤,玩家可以拾取物品,而物品在世界中的存在则变得透明,不会影响玩家的移动或游戏的物理效果。

20:02

📚 完成简单库存系统的基础设置

最后一段总结了如何完成简单库存系统的基础设置。作者演示了玩家如何通过点击操作将草、土和石头添加到库存中,并展示了物品在世界中被隐藏的效果。玩家可以自由地在这些物品上行走,而不会被它们阻挡。此外,作者提到在后续的教程中将教授如何将库存显示在UI上,以及如何将物品放入其他容器,如箱子。

Mindmap

Keywords

💡库存系统

库存系统是视频中的核心主题,指的是玩家在游戏中存储和查看所有物品的系统。在视频中,通过创建一个简单的库存系统,玩家可以收集草、石头和泥土等物品,并将它们存储在库存中。这个系统是游戏中常见的功能,允许玩家管理他们的资源和装备。

💡立方体

立方体在视频中被用作示例物品,用来展示如何创建和使用库存系统。草、石头和泥土被制作成立方体形状,以便简化教学过程。立方体作为最简单的3D模型之一,适合用来说明如何将物品转化为游戏世界中的实体。

💡预制体

预制体(Prefab)是Unity中的一种资源,允许开发者创建可重复使用的游戏对象实例。在视频中,草、石头和泥土的立方体被转换成预制体,这样它们就可以在游戏世界中多次使用,并且可以统一管理它们的行为和属性。

💡物理材质

物理材质在视频中用于给立方体模型添加不同的视觉和物理属性。例如,草、石头和泥土立方体分别被赋予了绿色、灰色和棕色的材质,以模拟它们在现实世界中的外表。这增强了游戏的视觉效果并帮助玩家识别不同的物品。

💡有限状态机(FSM)

有限状态机是视频中用来控制游戏对象行为的工具。通过为物品创建一个FSM模板,可以定义物品的行为,如是否可以被存储在库存中。FSM允许开发者根据玩家的互动来改变游戏对象的状态,从而实现复杂的游戏逻辑。

💡射线检测(Raycast)

射线检测是一种在Unity中常用的技术,用于检测玩家视线中的物体。在视频中,通过射线检测实现玩家与物品的交互,当玩家点击时,射线会检测到被点击的物品,然后触发相应的事件,如将物品添加到库存中。

💡游戏对象变量

游戏对象变量是Unity中用于存储对游戏对象引用的变量。在视频中,创建了一个名为'item to add'的游戏对象变量,用于在玩家与物品交互时指定要添加到库存中的物品。这种变量的使用简化了游戏对象之间的引用和交互。

💡数组变量

数组变量是一种数据结构,用于存储一系列元素。在视频中,创建了一个名为'inventory'的数组变量,用于存储玩家库存中的所有物品。通过向数组中添加或移除元素,可以管理玩家的库存内容。

💡玩家交互

玩家交互是游戏中玩家与游戏世界进行交流的行为。在视频中,通过玩家点击操作与立方体物品进行交互,触发物品被添加到库存的事件。这是玩家控制游戏流程和影响游戏世界状态的一种方式。

💡UI显示

UI显示指的是游戏中用户界面的呈现,用于向玩家展示信息和状态。虽然在视频中没有直接展示,但提到了在未来的教程中会学习如何将库存系统中的物品显示在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

play00:02

hello and welcome to the simple

play00:04

inventory tutorial in this video we're

play00:06

going to be learning how to make a

play00:07

simple inventory system a place where

play00:09

the player stores and views all of their

play00:11

items let me just take you through the

play00:13

scene really quick i have this player

play00:15

here which is a first person player and

play00:18

i'm just using the setup that we've made

play00:20

in previous tutorials so this has our

play00:22

first person look template this player

play00:25

move template and a simple player

play00:27

interact template that's all making use

play00:29

of the character controller it isn't

play00:30

completely necessary but if you want to

play00:32

follow along with this you can go check

play00:33

out the tutorials for that first person

play00:35

controller links are in the description

play00:37

okay so i have that and then i have a

play00:39

couple of cinti assets in here and then

play00:41

i have a couple of cinti assets in here

play00:42

i have the floor from cinti's polygon

play00:44

prototype pack as well as this chest and

play00:47

the chest and the floor are currently

play00:48

prefabs but what i want to do is i'm

play00:51

just going to right click on the floor

play00:52

create an empty parent and i'm going to

play00:54

rename this to our

play00:56

world

play00:57

okay so it just has

play00:59

level stuff in it and this chest i'm

play01:01

gonna right click and i'm gonna unpack

play01:03

it completely okay so that means it's

play01:05

detached from the prefab asset we have

play01:08

now i'm gonna rename this

play01:10

as our

play01:11

chest

play01:12

base

play01:13

and then rename this as chest lid just

play01:16

to see things a little more clearly now

play01:18

to start with an inventory system you

play01:19

need items to actually put in your

play01:21

inventory so we're going to start by

play01:23

making some cubes so i'm gonna go ahead

play01:25

and make a cube we'll call this one

play01:27

grass

play01:30

and we'll call this one

play01:32

stone

play01:33

and we'll call this one

play01:35

dirt

play01:36

okay now i'm just making

play01:38

some simple materials here

play01:41

okay so these are just going to be cubes

play01:44

i'll change the scale to like

play01:46

5.5.5 just make them a little smaller

play01:48

but you can actually use whatever you

play01:51

want

play01:52

any type of items we'll do just fine

play01:54

making these cubes here is just sort of

play01:57

simplest example that we can work with

play01:59

maybe you could just as easily be doing

play02:01

this system with a sword

play02:03

or

play02:04

a health pack a gun all the common

play02:06

things

play02:07

doesn't matter we're using cubes here

play02:09

make things easy on ourselves okay so we

play02:11

have grass stone and dirt now what we

play02:13

want to do is make each one of these

play02:15

into a prefab that way there could be

play02:17

multiple out in our world and we could

play02:19

slap some fsm templates on them so they

play02:22

behave accordingly i have a little

play02:23

folder here for the scene that i'm

play02:25

currently working in this simple

play02:26

inventory so in here i'm going to create

play02:28

a new folder and call this

play02:31

simple inventory

play02:34

prefabs and i'm going to create another

play02:37

folder

play02:38

call this one

play02:40

simple

play02:41

inventory materials

play02:44

okay so now in the materials i'm going

play02:46

to create

play02:48

a dirt material and it's just going to

play02:51

be

play02:55

brown

play02:57

let me turn the smoothness down

play02:59

okay and i'll pop that on our first one

play03:02

and then i'm going to duplicate this

play03:03

hitting ctrl d

play03:05

renaming this to grass

play03:10

and i'm going to change this to green

play03:13

pop it on there

play03:15

and then duplicate that call this one

play03:19

stone

play03:21

and change it to

play03:22

gray

play03:25

pop it on there i'm gonna go into the

play03:27

prefabs folder and drag in our dirt

play03:30

our stone and our grass then i'm going

play03:33

to remove them from the scene so now we

play03:35

can work on them here let's start with

play03:37

the dirt okay so i'm going to open up

play03:38

our prefab by double clicking on it and

play03:41

selecting the asset itself over here in

play03:43

the hierarchy i can start working on an

play03:46

fsm for it so moving this over i'm going

play03:48

to right click add fsm we're going to

play03:50

call this fsm item

play03:52

okay because it's going to be a template

play03:53

and we want to be able to use it on the

play03:55

other ones for the grass and the stone

play03:57

as well so we're going to make something

play03:58

here that is generic but with a few

play04:01

values that we could change per item

play04:03

okay so

play04:04

the first thing that i'm going to want

play04:06

to do is make it so

play04:08

so i'm going to right click in here i'm

play04:10

going to go save template okay so i'm

play04:11

going to call this a simple inventory

play04:16

item

play04:17

okay and use the save template in csm

play04:20

yes

play04:20

now the name of the template file is

play04:23

simple inventory item but the name of

play04:25

the fsm is item okay and it's an

play04:28

important distinction to make when we're

play04:30

referencing the fsm it'll always be

play04:31

referencing this name item okay so if we

play04:34

clicked edit template now we're inside

play04:36

here if i go to the variables

play04:37

i'm going to create a new sprite

play04:40

variable called item sprite

play04:44

and we're going to make it an

play04:45

input variable now it's exposed over

play04:47

here in our inspector so even though

play04:50

this is a reusable template we can set a

play04:52

unique value for the item sprite and

play04:54

then i'm going to give it a bool value

play04:58

we're going to call this

play05:00

can

play05:01

stash

play05:02

in the tooltip i'll just write here true

play05:05

equals

play05:06

can stash

play05:08

into inventory

play05:11

false

play05:12

equals

play05:14

can not stash into inventory

play05:21

okay so if you have your mouse over here

play05:23

it tells you a little tool tip

play05:25

this reminds us what this bull is for

play05:26

and make this an input as well and that

play05:28

way for some items you could make it so

play05:31

you can interact with them in the world

play05:32

but maybe you don't want them to be able

play05:34

to get picked up and added to an

play05:37

inventory say like a chair it would be

play05:39

sort of weird if you were able to add a

play05:40

chair to your inventory okay so this can

play05:43

stash for this dirt should be set to

play05:45

true and i think

play05:46

a good way to go about this would be to

play05:49

make the default input variable as true

play05:52

since most of the items we'll be making

play05:53

will be stashable for our inventory okay

play05:56

so the way you could do that is you

play05:57

could remove it from the inputs and then

play05:59

change its value to true

play06:01

and then make it an input and now

play06:03

whenever we add this template the

play06:05

default value for can stash will be true

play06:07

okay this is looking good so i'm saving

play06:09

that and i'm going to add the component

play06:12

rigidbody

play06:14

that way we can just throw a few of

play06:15

these in our scene

play06:16

okay i use gravity has the box collider

play06:18

already this looking good saving it and

play06:20

i'm going to hop over to grass

play06:23

and i'm going to right click to add fsm

play06:25

use template

play06:27

and we're going to use our simple

play06:29

inventory item okay you'll see that over

play06:31

here our can stash is already set to

play06:33

true and our item sprite doesn't have

play06:34

anything yet we'll put something there

play06:36

in a moment but let's just call this

play06:38

item okay we have to change that saving

play06:40

it and hop over to stone do the same

play06:42

thing use template

play06:45

and our simple inventory item changing

play06:47

this to item

play06:49

and yes we want it to stash okay so that

play06:51

all looks good to me so i've gone and

play06:53

prepared these

play06:54

sprites

play06:56

okay so this is a single image that just

play06:57

has

play06:58

sprite for

play06:59

our dirt our grass and our stone cubes

play07:02

okay so this isn't the video that we're

play07:03

going to learn how to chop up sprites

play07:05

and all that but just know that what you

play07:06

could do is you could bring in an image

play07:08

for your icons right it doesn't have to

play07:10

be one single image containing all of

play07:12

them it's also just as easy to bring in

play07:15

an image for

play07:16

for just one at a time the only thing

play07:18

that you really need to know is that

play07:19

whatever image you bring in you're going

play07:20

to want to select it and then come over

play07:21

here to the texture type you want to

play07:23

select sprite 2d and ui and when you're

play07:26

done with that you'll hit the supply

play07:27

button okay but yeah i've gone and

play07:30

chopped up mine since mine was three in

play07:32

one and so i have all the individual

play07:34

sprites here so for me

play07:36

what i'm gonna do is i'm gonna select

play07:37

this stone and over here in the item

play07:39

sprite

play07:41

i can select the stone one

play07:44

and drop it in right here

play07:46

and i've actually just realized that

play07:47

somewhere in the middle of all this i've

play07:49

named my grass block stone so what i'm

play07:52

going to do is actually i'm going to

play07:53

keep this as stone i'm going to drag and

play07:55

drop this stone

play07:57

sprite icon

play07:58

into this item sprite value okay so it's

play08:00

using that now but i am going to

play08:04

change this material okay so that should

play08:06

be stone

play08:07

i could save that okay so that's our

play08:10

stone right it's gray has the item for

play08:13

stone now i'm gonna go to grass

play08:15

and i'm gonna drag and drop this grass

play08:17

icon in there

play08:20

and just make sure that i put this grass

play08:21

material on there

play08:23

saving that then i'm gonna go to dirt

play08:27

and this last one

play08:29

i'm going to drag and drop this

play08:32

dirt sprite icon in here

play08:35

that's looking good saving it okay

play08:37

now this has a rigid body the other ones

play08:39

did not so i'm just going to add a rigid

play08:41

body on those as well

play08:43

add component rigid body save it

play08:46

stone add component rigid body save it

play08:49

so now they all have their rigid bodies

play08:51

and their item fsm and they all have

play08:54

their sprite assigned to them okay so

play08:56

those are all of our objects dirt grass

play08:59

and stone next we're going to take the

play09:02

player and we're going to go over to

play09:04

this

play09:05

interact

play09:06

template okay so our interact template

play09:08

if we edit it

play09:10

we can see that what it's doing is it's

play09:12

waiting for us to click because that's

play09:14

fire1 we're using unity's input axes but

play09:17

you can use unity's new input system or

play09:19

third-party assets like rewired whatever

play09:21

it is we're just looking for

play09:23

some player input

play09:25

to fire off to the next state okay okay

play09:27

so whatever input system you're using

play09:29

after you press the button for interact

play09:31

we go here to get object has this

play09:33

raycast and what it does is it gets

play09:36

whatever object you're looking at this

play09:37

object hit

play09:39

and does test whether or not you hit

play09:40

something if you did then it sends this

play09:44

event here this object hit okay and it

play09:46

sends an interact event this interact

play09:48

event is a this interact event is a sort

play09:51

of pillar of the designs that we use in

play09:54

a lot of the tutorials here as well as

play09:55

the templates available on the ecosystem

play09:58

so if you don't know anything about how

play10:00

our templates and this interact event

play10:02

works you should also go check out the

play10:03

tutorials for that stuff there are links

play10:05

in the description okay but the gist of

play10:07

it is yeah we just shoot a raycast out

play10:09

in the world and whatever thing it hits

play10:11

we send an event to it okay so this

play10:13

interact event is what gets sent to it

play10:15

that means that we can come over to our

play10:18

items and if we select this template we

play10:20

can come over to the first state and

play10:22

we'll just name this

play10:24

init

play10:25

that's just a little state where we can

play10:26

do some initialization stuff but i'm

play10:28

going to add a transition to it and send

play10:30

off to another state okay so this one's

play10:32

just for later if we want to add stuff

play10:33

to this but this new state that it

play10:35

transitions off to this is where it's

play10:37

standby for interaction

play10:40

and on this date we're going to add a

play10:41

transition

play10:42

custom events

play10:45

interact event okay so this item is

play10:47

going to be waiting here for an interact

play10:49

event to be sent so that's when the

play10:50

player interacts with it this event gets

play10:52

sent and the transition fires off to the

play10:54

next state where it's actually going to

play10:57

send an event back to the player so if i

play10:59

go to the action browser

play11:00

and i'm just going to go ahead and dock

play11:03

this right here

play11:05

i'm going to move my project tab back

play11:06

here and move that there we go just so

play11:08

we have some more room so what's going

play11:10

to happen is in here it's going to

play11:14

tell player to stash this item

play11:18

okay so we're going to send

play11:20

an event to the player the target that

play11:22

we want to send to we're going to set as

play11:24

a game object fsm the game object we're

play11:26

going to specify as a new variable that

play11:29

we'll call

play11:31

player and that's because if i save this

play11:33

really quick and i go back out to the

play11:35

scene i select the player

play11:37

go down to our interact fsm edit it

play11:40

you'll see that when we do interact

play11:42

we're also setting

play11:44

the variable name player on the object

play11:47

hit and it's setting it as the player

play11:49

itself this player value okay which

play11:51

happens up here in initialization it

play11:52

gets itself okay that way whenever we

play11:54

interact with things there's a way for

play11:56

the thing that we're interacting with to

play11:58

reference us as well a very easy and

play12:01

direct way that it could start talking

play12:02

to the player okay so if i go over to

play12:06

our item again

play12:10

okay the player the event that we want

play12:12

to send is going to an fsm on the player

play12:14

that we'll call

play12:16

inventory and the event we want to send

play12:18

is a new global event that we can call

play12:22

inventory

play12:24

space space

play12:26

add

play12:28

let's just test if this works right now

play12:29

nothing's really going to happen other

play12:30

than

play12:31

sending this inventory add okay so i'm

play12:34

going to copy this text

play12:36

the name of this fsm because we haven't

play12:38

made it yet and i'm gonna go back out

play12:39

here select our player

play12:41

i'm gonna add a new component it's an

play12:42

fsm i'm gonna paste the name in here

play12:45

inventory and open up our inventory fsm

play12:48

and in here i'm gonna right click

play12:50

add global transition custom events

play12:53

inventory add

play12:55

okay so what we should see

play12:57

if i put a cube out here

play13:01

if i select our player and we just take

play13:03

a look at our inventory fsm when i run

play13:05

this right now

play13:07

press play and i'm going to click on the

play13:09

cube

play13:10

okay there we go you see that on our

play13:12

inventory it fired off at and if you

play13:15

select the dirt you'll see that indeed

play13:17

it did this action right here where it

play13:19

tells the player to stash this item okay

play13:20

now let's add a little bit of

play13:21

functionality to that so i'm going to

play13:23

remove the dirt and we're going to go

play13:25

ahead and

play13:26

add

play13:28

in our simple inventory item template

play13:31

something here in the initialization

play13:32

state we're going to do a get

play13:35

owner

play13:37

and the owner is

play13:38

this item

play13:40

okay now down here in the tell player to

play13:43

item we can do is set fsm game object

play13:48

okay

play13:49

here's where we could tell the player

play13:51

which item they're adding to their

play13:52

inventory so i'm going to specify the

play13:55

game object the player

play13:57

the fsm name is inventory and the

play14:00

variable name we'll call it

play14:02

item to add

play14:04

okay and we're going to set the value as

play14:06

this item

play14:08

so i'm going to take this item to add

play14:09

text i'm going to copy it

play14:12

saving this i'm going to go back out to

play14:13

my scene and player

play14:16

the variables we're going to make a new

play14:18

game object variable called item to add

play14:22

okay so back here in the state in item

play14:24

to add what we could do is

play14:26

do an array add

play14:28

okay now this array add we're going to

play14:30

have we're going to make a new variable

play14:34

it's going to be an array variable we'll

play14:36

call this our

play14:38

inventory with the capital i

play14:42

and the array type

play14:44

is game object

play14:46

so now if i go over here i could select

play14:49

our inventory array and the value that

play14:51

we want to add

play14:52

is item to add this should be the very

play14:55

beginnings of our inventory system i

play14:57

should be able to come over here and

play14:59

grab our

play15:00

dirt

play15:01

our grass and our stone

play15:04

okay i'm just going to grab all these

play15:05

and scooch them up

play15:10

and let's select our player

play15:14

and go to variables inventory i'm going

play15:16

to make this an output variable and that

play15:17

way down here we could see the inventory

play15:20

populating so we could see it changing

play15:22

over here in real time so hit play

play15:25

you'll also be able to see over here

play15:27

okay now

play15:28

i'm gonna go ahead i'm gonna click this

play15:30

grass cube

play15:32

you can see that our inventory added the

play15:35

grass cube you can see it down here

play15:37

okay it's still just sitting there okay

play15:39

so i can come over to the dirt cube

play15:41

click that now we have grass and dirt

play15:44

come over to our stone click that now we

play15:46

have grass dirt and stone great so this

play15:49

is looking pretty good

play15:51

stop this so the next step would be to

play15:54

sort of remove these cubes

play15:57

from the world in a way or at least make

play15:59

their presence sort of unknown

play16:02

we don't want to delete them

play16:03

because we want them to stay on our

play16:05

inventory but the inventory array on the

play16:08

player is more of just an idea it's just

play16:11

sort of an organizational thing it's not

play16:13

an actual place in the 3d space of our

play16:17

scene so they're not going anywhere

play16:18

that's why they just sat there but they

play16:20

populated in this list the way you build

play16:22

the illusion that something has been

play16:24

added to your inventory

play16:26

is by changing what actually happens

play16:28

when you do this little inventory ad so

play16:30

we're going to say

play16:31

add to array

play16:34

i'm going to add a finish transition and

play16:35

send down here

play16:37

where you

play16:38

hide item from world

play16:41

okay and then here is where you could

play16:42

put something like set visibility

play16:46

okay and you could set your

play16:48

item to ads visibility to false

play16:52

okay so having this box unchecked means

play16:55

that your item will become invisible

play16:57

reset on exit i'm going to uncheck that

play16:59

we want it to stay invisible until we

play17:02

tell it explicitly to become visible

play17:04

again another thing we want

play17:06

is to avoid being able to like bump into

play17:08

them or raycasts to hit them or any sort

play17:12

of physics interactions

play17:14

in any sort of way we don't want these

play17:16

getting in the way they're like just

play17:17

some invisible ghost item so what you

play17:19

could do then

play17:21

is set layer

play17:24

okay and let's just assume that all of

play17:27

our items are on the default layer to

play17:29

begin with right like most of these like

play17:31

they are right now if i select these you

play17:33

can see that they're on their default

play17:34

layer

play17:35

but we could change it

play17:37

and you might be tempted to use

play17:38

something like ignore raycast or

play17:39

something like that and that could work

play17:41

but i think to be safe what we want to

play17:43

do is come over here to layer

play17:44

and we're going to add a layer and we're

play17:46

going to call this one

play17:47

ignore all now this ignore all layer if

play17:50

we come over to our project settings and

play17:52

come over to our physics section here

play17:55

you could change your collision matrix

play17:57

in a way that ignore all

play18:00

doesn't interact

play18:02

with anything so this ignore all right

play18:04

here we want to uncheck everything that

play18:06

it's on and this bottom one right here

play18:08

we only need to uncheck that one so

play18:10

basically this means that everything on

play18:11

the ignore all layer is untouchable by

play18:14

the entire physics system for every

play18:16

other layer okay that means we can't

play18:18

bump into it that means raycasts can't

play18:19

hit it all that sort of good stuff okay

play18:22

so

play18:23

if i select the player

play18:25

and i'm just going to scooch this down

play18:26

again so we can see the inspector

play18:29

we've set the visibility to invisible

play18:30

we've set the layer to ignore all

play18:33

oop and not get owner we actually want

play18:35

it to be our item tab okay so

play18:38

save that give it a little test spin

play18:40

okay so i'm going to click on that it

play18:42

goes away not like bumping into anything

play18:44

there right i could totally move freely

play18:46

so you can see if i walk over here

play18:48

my player capsule

play18:50

jumps over them right it kind of the

play18:52

step height takes care of itself and

play18:55

it ends up walking over them

play18:58

but right here there's nothing if i

play19:00

pause this

play19:02

it's like

play19:03

select our graphs

play19:06

you can see that what happened was

play19:07

because it was ignoring everything it

play19:08

actually fell through the world so it's

play19:10

actually i make this bigger if i look

play19:13

down

play19:14

see it's

play19:15

it's just falling continuously down

play19:17

there

play19:18

maybe a good thing to do too would be to

play19:22

tell it to

play19:24

put on a

play19:25

set is kinematic okay so we're going to

play19:27

specify our

play19:29

item to add and we'll say is kinematic

play19:32

and what that'll do is it'll just make

play19:33

sure that the object stays put

play19:35

doesn't matter too much but it at least

play19:37

prevents

play19:38

unity from having to do this extra work

play19:40

of

play19:41

simulating the physics of it falling

play19:43

when it really doesn't need to so

play19:45

setting to his kinematic relieves some

play19:47

of that work

play19:49

okay so i'm going to press play and if i

play19:52

click it

play19:54

and i pause it

play19:56

and grab my grass

play19:58

you can see that it's still sitting here

play20:00

nicely

play20:01

okay still there in the world and

play20:04

should be able to move

play20:06

yep and you can see that in

play20:09

this screen

play20:11

i'm totally walking right through it

play20:15

but as you can see in the game screen

play20:17

nothing is stopping me from walking

play20:19

through it this is exactly what we want

play20:21

so the first part of our inventory

play20:23

system

play20:24

is

play20:26

ready to go okay so that's how you can

play20:27

set up the beginnings of our simple

play20:29

inventory system in the next tutorials

play20:31

we're going to be learning how to

play20:32

display this in an inventory ui as well

play20:35

as putting items in other containers

play20:37

like this chest here be sure to check

play20:39

out our other videos to learn all the

play20:41

various features of playmaker links to

play20:43

more learning resources are in the

play20:44

description

Rate This

5.0 / 5 (0 votes)

相关标签
游戏开发物品系统PlaymakerUnity教程第一人称玩家交互物理引擎UI设计物品拾取