How to make a DOOM CLONE in Unity || E1M6 Doors and Keys

SpawnCampGames
18 Feb 202115:55

Summary

TLDR本视频教程介绍了如何在Doom克隆游戏中添加一个带有钥匙和敌人生成系统的门。首先创建一个空的游戏对象作为门的基础,并添加门框和门作为子对象。使用Unity的标准立方体或ProBuilder工具来构建门的模型,并确保每个部分都有碰撞器。接着,通过动画窗口创建门的关闭和开启动画,并在Animator窗口中设置动画状态和转换。为了检测玩家,添加一个触发器和脚本,当玩家触发时,根据玩家是否拥有相应的钥匙来打开门或激活敌人生成区域。最后,创建钥匙拾取脚本,并将其与门和钥匙拾取对象关联,以实现游戏逻辑。视频还展示了如何将门和钥匙拾取设置为预制件,以便在构建游戏关卡时重复使用。

Takeaways

  • 🚪 在视频中,将向Doom克隆游戏中添加一个包含钥匙和敌人生成系统的门。
  • 🛠️ 门将分为三部分构建:门基座、门框和门本身,使用Unity的空游戏对象和标准立方体或ProBuilder来建模。
  • 🎨 为门添加材料,包括基本颜色以及红、蓝、绿三种颜色,以区分不同类型的门。
  • 🔑 制作门动画,包括门的关闭状态(idle)和开启状态(open),并使用Unity的动画窗口来创建动画剪辑。
  • 📂 动画剪辑将保存在动画剪辑文件夹中,并通过动画器窗口设置从默认状态到开门状态的转换。
  • 👤 为检测玩家,将在门对象上添加一个盒子碰撞器,并设置为触发器,同时添加一个脚本来控制门的逻辑。
  • 🔒 门脚本将检测玩家是否进入触发器,并在动画器中设置触发器来打开门。
  • 👾 门还将负责在开门后激活区域内的敌人,通过设置一个公共游戏对象来控制敌人的生成。
  • 🔄 创建三种不同颜色的门变体,每种门需要特定的钥匙才能打开。
  • 🗝️ 玩家将有一个库存系统来存储钥匙,通过脚本控制哪些钥匙被收集。
  • 🔄 制作钥匙拾取器,当玩家触发时,将更新玩家库存中的钥匙状态,并销毁钥匙对象。

Q & A

  • 在视频中,如何为Doom克隆游戏添加门系统?

    -在视频中,首先创建一个新的空GameObject作为门的基础,然后将其变换归零。接着,作为子对象添加门框和门的空GameObject,并将门的各个部分放入相应的空GameObject中。

  • 使用Unity标准立方体构建门框和门有哪些步骤?

    -使用Unity标准立方体构建门框和门的步骤包括:使用多个标准Unity立方体将它们拼接在一起形成门框和门,只要保持结构不变,任何制作方式都可以。

  • 为什么选择使用ProBuilder来构建门?

    -选择使用ProBuilder是因为它是一个强大的工具,可以在Unity中进行模型构建,类似于在Unity内部建模。

  • 如何为门添加动画?

    -为门添加动画需要打开动画窗口,选择门的空GameObject,创建新的动画剪辑,命名为'door idle',保存在动画剪辑文件夹中。然后创建另一个动画剪辑'door open',记录开门动作的关键帧。

  • 如何设置门的动画状态?

    -在Animator窗口中设置门的动画状态,创建一个触发器'open door',并在默认剪辑和'door open'剪辑之间创建过渡。

  • 如何检测玩家是否进入门的触发区域?

    -在门的主GameObject上创建一个BoxCollider,并设置为'Is Trigger'。然后添加一个名为'door'的脚本,该脚本包含逻辑以检测玩家是否进入触发区域,并在Animator中设置触发器。

  • 如何让门在玩家触发后打开?

    -在'door'脚本中,使用'void OnTriggerEnter'函数来检测玩家是否触发了门。如果是玩家,使用'doorAnim.SetTrigger'方法和字符串'open door'来打开门。

  • 如何防止门在打开动画结束后循环播放?

    -在项目窗口中找到'door open'动画,然后在Inspector中关闭'Loop Time'选项,这样门在打开后就会保持打开状态,而不会循环播放。

  • 如何让门在打开时同时激活区域内的敌人?

    -创建一个公共GameObject,命名为'area to spawn'。在门脚本中,在打开门之后,使用'areaToSpawn.SetActive(true)'来激活这个GameObject,从而激活区域内的敌人。

  • 如何为不同颜色的门设置不同的钥匙要求?

    -在门脚本中,为每种颜色的门设置一个公共布尔值(例如'requireRed', 'requireBlue', 'requireGreen'),并在脚本中检查玩家是否拥有相应的钥匙。如果玩家拥有正确的钥匙,门就会打开。

  • 如何创建钥匙拾取物并将其添加到玩家的库存中?

    -创建一个空GameObject作为钥匙拾取物的根,然后添加一个Quad作为图形,并设置其Collider为'Is Trigger'。添加一个'key pickup'脚本,并在脚本中设置相应的布尔值来表示不同类型的钥匙。当玩家触发拾取物时,将相应的布尔值设置为true,并销毁拾取物GameObject。

  • 如何将门和钥匙拾取物转换为预制件?

    -将门和钥匙拾取物居中后,拖入到Prefabs文件夹中创建预制件。创建预制件后,可以从场景中删除它们,以便在构建关卡时重复使用。

Outlines

00:00

🚪 制作带有钥匙和敌人生成系统的门

本段视频教程介绍了如何在Doom克隆游戏中添加一个包含钥匙和另一侧生成敌人的门系统。首先,创建一个空的游戏对象作为门的基础,并将其变换设置为零。接着,作为子对象添加门框和门的空游戏对象,并在其中放置门的各个部分。可以使用Unity的标准立方体构建门,或者使用ProBuilder工具进行建模。确保每个部分都有碰撞器。然后,创建一个名为'door'的材料文件夹,并准备基本的门颜色以及红色、蓝色和绿色的材料。接下来,通过动画窗口为门创建'door idle'和'door open'两个动画剪辑,分别代表门的关闭和开启状态。最后,设置Animator窗口中的动画状态和转换,为门添加触发器和条件,以实现玩家接近时门的自动开启。

05:05

🔓 门的逻辑和敌人生成

在本段中,继续完善门的逻辑,使其能够检测玩家并触发动画。首先,为门添加一个Box Collider组件,并设置为'Is Trigger'。然后,创建一个名为'door'的脚本,用于处理门的逻辑,包括检测玩家进入触发器区域并设置Animator的触发器。此外,为了解决门动画循环的问题,需要在Unity编辑器中禁用'Loop Time'选项。接下来,介绍了如何通过创建一个名为'area to spawn'的公共游戏对象来控制敌人的生成,确保敌人在门打开后正确生成。最后,展示了如何使用基础门的设置来创建三种不同颜色的门,并为每种门设置不同的钥匙要求。

10:06

🗝️ 玩家钥匙系统和门的设置

本段视频教程讲解了如何为玩家设置钥匙系统以及如何为不同颜色的门配置钥匙要求。首先,为玩家添加一个名为'player inventory'的脚本,用于跟踪玩家拥有的钥匙。然后,为每种颜色的门添加一个布尔值,以确定是否需要特定的钥匙才能打开门。在'door'脚本中,通过比较标签和布尔值检查来实现门的开启逻辑。如果门需要钥匙并且玩家拥有相应的钥匙,则门会打开。最后,展示了如何将门和钥匙拾取器设置为预制件,以便在构建关卡时可以重复使用。

15:06

🔑 创建钥匙拾取器

在最后一段中,介绍了如何创建钥匙拾取器。首先,创建一个空的游戏对象作为拾取器的父对象,并为其子对象添加一个四边形作为图形。然后,为拾取器添加一个Box Collider,并设置为触发器。接着,创建一个名为'key pickup'的脚本,用于处理玩家拾取钥匙的逻辑。脚本中,使用'OnTriggerEnter'函数检测玩家并根据拾取器的颜色更新玩家库存中的相应布尔值。此外,为了视觉效果,为钥匙图形添加了'Sprite'和'Sprite Rotator'脚本,并设置了材质。最后,一旦玩家拾取了钥匙,拾取器将被销毁,以确保玩家只能拾取一次。

Mindmap

Keywords

💡Unity

Unity是一个跨平台的游戏引擎,用于创建视频游戏。它提供了一套工具和功能,使得开发者能够设计和构建各种类型的游戏。在视频中,Unity被用来创建一个“毁灭战士”风格的游戏,包括制作门、钥匙系统和敌人生成机制。

💡门系统

门系统是游戏中的一个机制,允许玩家通过特定的动作或物品(如钥匙)来开启或关闭门。在视频中,门系统被设计为由三部分组成:门框、门本身以及动画效果,以增强游戏的互动性和沉浸感。

💡动画

动画在视频中指的是门的开闭动作,这是通过Unity的动画窗口来实现的。动画是游戏中增强视觉效果和玩家体验的重要元素,例如,门的开启和关闭动画可以给玩家提供视觉反馈,表明门的状态。

💡敌人生成

敌人生成指的是游戏中在特定条件下生成敌人的机制。在视频教程中,当玩家打开门时,系统会在门的另一侧生成敌人,为游戏增加挑战和动态性。

💡空游戏对象

空游戏对象在Unity中是一个没有具体模型或纹理,但可以包含脚本和组件的容器。在视频中,空游戏对象被用作门的基座,以及门框和门的父对象,以组织场景中的元素。

💡碰撞器

碰撞器是Unity中用于检测物体间碰撞的组件。在视频中,为了检测玩家是否到达门的位置,门对象被赋予了一个盒子碰撞器,并设置为“is triggered”,以便在玩家触发时执行特定的动作,如开门。

💡Animator

Animator是Unity中的一个组件,用于控制游戏对象的动画状态和转换。在视频中,Animator组件用于管理门的开启和关闭动画,通过设置触发器和条件来响应玩家的互动。

💡脚本

脚本是Unity中用于编写游戏逻辑的代码。在视频中,脚本被用来实现门的逻辑,如检测玩家是否拥有正确的钥匙来开启门,以及控制敌人生成的逻辑。

💡预制件

预制件是Unity中预先配置好的对象,可以重复使用。在视频中,完成的门和钥匙拾取对象被转换为预制件,以便在游戏的不同部分快速部署和使用。

💡玩家库存

玩家库存是游戏中用于跟踪玩家拥有的物品或资源的系统。在视频教程中,玩家库存通过脚本实现,用于跟踪玩家是否拥有开启特定颜色门的钥匙。

💡颜色钥匙

颜色钥匙是游戏中的一种机制,用于区分不同类型的门和钥匙。在视频中,有红色、蓝色和绿色的门和钥匙,每种颜色的钥匙对应相同颜色的门,增加了游戏的策略性和多样性。

Highlights

在视频中,将向Doom克隆游戏中添加一个带有钥匙和敌人生成系统的门。

构建门分为三部分:门基座、门框和门本身。

使用Unity标准立方体或ProBuilder工具来构建门框和门。

确保每个门部件都具有碰撞器,以便正常工作。

使用材质为门添加颜色,包括基本颜色和红、蓝、绿色。

通过动画窗口创建门的关闭和打开动画。

在Animator窗口中设置动画状态和转换。

创建一个脚本来检测玩家进入门的触发器并控制动画。

设置一个触发器来检测玩家并激活敌人生成区域。

创建三种不同颜色的门,每种门需要不同颜色的钥匙。

为玩家添加一个库存脚本来跟踪他们拥有的钥匙。

为每种颜色的门设置需要相应颜色钥匙的逻辑。

将门和钥匙拾取器转换为预制件,以便在构建关卡时重复使用。

创建钥匙拾取器,当玩家触碰时更新其库存并销毁拾取器对象。

为每种颜色的钥匙创建拾取器,并设置相应的颜色和逻辑。

完成的门系统可以根据需要在关卡构建中选择使用。

Transcripts

play00:02

in this video we'll be adding a door

play00:03

system to our doom clone

play00:05

that comes complete with keys and a

play00:08

system to spawn enemies on the other

play00:10

side

play00:14

we'll build each door in three pieces

play00:16

we'll create a new empty game object

play00:18

that will be

play00:18

our door base and zero out the transform

play00:26

next as children will have a door frame

play00:28

empty game object

play00:30

and also a door empty game object

play00:34

we'll put our door pieces in each empty

play00:36

accordingly

play00:38

now there's a couple of ways we can

play00:39

build our door the easiest way

play00:42

is use a bunch of standard unity cubes

play00:44

and piece them together for the door

play00:46

frame

play00:48

and then also for the door

play00:52

as long as you keep this structure any

play00:54

way you go about making them will work

play00:56

for us

play00:58

i'm going to opt out for use in pro

play01:00

builder

play01:01

it can be found in windows package

play01:04

manager

play01:04

and it's a powerful tool that

play01:06

practically lets you model within unity

play01:10

if you'd like a tutorial on how to use

play01:12

it let me know in the comments

play01:15

now we still have the same structure and

play01:17

as long as our pieces

play01:19

have a collider just as our default cube

play01:21

did then we're good

play01:22

importing your own models will also work

play01:25

fine

play01:26

now we're almost ready to animate the

play01:27

door so we'll move it out in front of us

play01:29

a little bit

play01:30

but before we do that let's give it a

play01:32

splash of color

play01:34

so i made a folder inside my materials

play01:36

called door

play01:37

and i added a few materials so we'll

play01:39

need a basic door color as well as a red

play01:42

blue and a green

play01:46

now we're ready to animate so we'll need

play01:48

our animation window

play01:50

if yours is missing open it with window

play01:53

animation and animation now let's click

play01:56

our door empty game object of our base

play01:58

door

play02:04

we'll create a new animation clip called

play02:06

door idle and we want to save it in our

play02:09

animation clips folder

play02:13

this will be our closed state of the

play02:14

door and it only needs one key frame so

play02:16

we'll click the record button and we'll

play02:18

just move the door up a bit and back to

play02:20

where it was

play02:26

and then we'll click record button again

play02:28

to stop and you'll see

play02:29

one keyframe next we'll need the door

play02:33

open animation so we'll create a new

play02:35

animation clip called door open

play02:38

for this one we'll hit record we'll move

play02:40

this slider to

play02:41

0.3 or the half a second mark

play02:45

and we'll just pull our door to the

play02:47

position we want when it's open

play02:52

now you can click record to stop and we

play02:54

have our animation clips

play02:56

now we need to go to the animator window

play02:59

this can also be found in windows and

play03:02

animation

play03:04

so the orange clip is our default

play03:06

starting clip

play03:07

we need a transition to the door open

play03:10

clip so

play03:11

first let's go up here and select

play03:13

parameters and click the plus

play03:15

symbol we're going to use a trigger and

play03:17

we'll name it open door

play03:19

right click the default clip click make

play03:21

transition

play03:22

drag it over to the open door clip and

play03:24

left click

play03:25

now we'll select the transition by

play03:27

clicking the arrow and over in the

play03:28

inspector let's

play03:29

disable has exit time we'll also go down

play03:33

here and click the plus

play03:34

add a condition and since we only have

play03:36

one it's going to go ahead set it up for

play03:38

us

play03:39

and that's it for the animation

play03:46

now we need something to detect the

play03:48

players so we'll go to our main base

play03:50

door object

play03:50

and create a box collider if you press

play03:53

this button here it'll show you the

play03:54

bounds of the box

play03:55

and you can edit it by dragging these

play03:58

little dots

play03:59

and we want to make it big enough for

play04:01

the player to enter it from

play04:02

either side and when we're finished

play04:04

we'll make sure to set this to

play04:06

is triggered now we'll add another

play04:08

component and this will be a script

play04:10

called door and it will hold all of our

play04:12

logic

play04:13

we'll wait for a second for it to

play04:14

compile and then we'll double click it

play04:16

to open it in visual studio

play04:18

we want our door to detect whether the

play04:20

player has entered the trigger and then

play04:21

set our trigger in our animator

play04:23

so we need a public animator reference

play04:26

called

play04:27

door anim

play04:30

and then we could save it and go to

play04:32

unity real quick and we can just drag in

play04:34

the door object with the animator into

play04:36

the door and um slot now back in the

play04:42

script we can ko the start and update

play04:44

function because all we're going to need

play04:46

is a void trigger

play04:47

enter function

play04:52

now in the function when it's triggered

play04:54

will detect if it's the player with

play04:56

if other dot compare tag in this player

play05:04

so if this is the player we'll open with

play05:06

our animator

play05:08

with dooran

play05:12

dot set trigger

play05:14

[Music]

play05:18

and this string needs to be identical

play05:23

so pascal case open door

play05:31

so it now detects the player and opens

play05:33

we have a little looping problem at the

play05:35

end

play05:35

and we can fix this by finding our door

play05:38

open animation

play05:39

in the project window and then turning

play05:42

off loop time

play05:43

in the inspector

play05:48

now the door will open and it will stay

play05:50

open our base door is almost finished

play05:53

but we need to add one more system

play05:54

you'll see here that if our enemies are

play05:56

beyond the locked door we can actually

play05:58

trigger them

play05:59

too early instead for now we're just

play06:01

going to tell the door to also spawn in

play06:03

any enemies that we need

play06:05

in that area

play06:10

to do this i create a simple public game

play06:13

object

play06:13

called area to spawn

play06:22

and right after we open the door we'll

play06:24

set this game object to be active with

play06:26

area to spawn dot set active and true

play06:37

you can now see in the hierarchy that we

play06:39

have an empty game object in this case

play06:41

called room one

play06:42

and it's just a bunch of enemies as

play06:44

children

play06:46

we'll keep the enemies active and just

play06:48

deactivate the object

play06:50

and then we'll drag this in our door

play06:52

script

play07:00

now let's use this base door to make our

play07:02

three variations

play07:03

first i'll clean up my scene a little

play07:09

i'll delete this pedestal that the enemy

play07:11

is on and drag him

play07:13

out of the way

play07:18

let's fix up this dab mesh that i hacked

play07:20

up for this tutorial

play07:21

in our window ai navigation will

play07:24

navigate to our bake tap

play07:26

making sure my gizmos are on you see

play07:28

that i've broke it we'll just hit

play07:30

bake and now it's ready when we need it

play07:33

back on our door we have everything

play07:34

ready except it's missing area to spawn

play07:37

but that's fine

play07:38

we'll sign this when we're ready when we

play07:40

go to place the doors

play07:43

so let's duplicate the entire door

play07:45

object three times

play07:46

and this is our green door red door and

play07:48

blue door

play07:54

currently they all open for our players

play07:55

so we'll need to know

play07:57

what keys are

play08:00

first let's jump back to our player

play08:03

below the player health we'll add a new

play08:04

script called player

play08:06

inventory and we'll open it up

play08:10

so here we're just going to use three

play08:12

public booleans public so we can access

play08:15

them from the door script and our door

play08:16

pickup

play08:19

so this is all that we need so let's

play08:22

save and go back to unity

play08:24

now go to any one of the doors and

play08:27

double click the door script to open it

play08:28

once more

play08:34

now we need a public boolean we'll call

play08:37

this requires key

play08:39

and in our compare tag we can do an if

play08:42

check

play08:43

so if requires key

play08:50

then we want to do more logic

play08:53

else requires key is not true then we

play08:56

want the door to open instead

play09:08

back at the top we're gonna need three

play09:10

public booleans just like our player

play09:12

inventory

play09:13

this time we're going to use commas and

play09:15

write it all on one line

play09:17

so we'll have require red comma require

play09:20

blue

play09:21

comma and require green

play09:28

and let's go back and change our player

play09:29

inventory script the same way

play09:31

so we're consistent

play09:36

so back in the door script now inside

play09:38

the if statement of requires key

play09:40

we'll do three checks so we'll say if

play09:43

required red

play09:44

so this is the red door and

play09:48

so we'll grab the player inventory

play09:49

script off the player when he triggers

play09:51

this

play09:52

so we'll say other dot git component and

play09:55

then we're going to say

play09:56

player inventory and we'll check if it

play09:58

has the red key by accessing its boolean

play10:01

dot has red so if it's a red door and

play10:04

the player has a red key

play10:05

we'll open and the same for the other

play10:07

two doors we'll check against blue

play10:09

and then we'll also shake against green

play10:24

and we can copy this door open logic and

play10:26

paste it in all three of our checks

play10:35

we can save and go back into unity so we

play10:38

have our player with three booleans

play10:42

and then our doors with three matching

play10:44

booleans and an extra for our base store

play10:47

our base door will have everything is

play10:49

false

play10:50

so set up the other doors by checking

play10:53

the required key

play10:55

and then what color for each door now

play10:58

only the base door will open

play11:00

but if we were to manually change the

play11:02

player inventory to have a key

play11:04

then that door will also open

play11:10

so now we can turn these into prefabs so

play11:13

we'll grab

play11:14

one and center it and then we'll open

play11:16

our prefabs folder

play11:18

and make a new one called doors and then

play11:20

we're going to drag this door in

play11:32

after it's a prefab we can delete it out

play11:34

of the scene and then we'll do the same

play11:36

for the others

play11:37

center it make it a prefab and delete it

play11:40

out of the scene

play11:47

so the doors are finished and it's time

play11:49

to make the key pickups

play11:50

this is the exact same process from the

play11:52

previous video that i have

play11:54

on pickups and billboards we're going to

play11:56

need to drag in our sprites that we plan

play11:58

on using

play11:59

select them and set their texture type

play12:01

to sprott

play12:02

2d and ui we'll apply these settings

play12:09

we'll create an empty game object and

play12:11

we're going to start with green pickup

play12:21

and as a child we'll create a new quad

play12:23

for our graphics

play12:24

we'll zero out their positions

play12:30

and i'm going to drag the pickup game

play12:32

object above the ground

play12:33

and forward in front of us a little bit

play12:36

so next click our quad

play12:38

or our graphics and make sure its

play12:40

collider is turned off

play12:42

back on the root pickup object we'll add

play12:44

a boss collider

play12:51

we'll make it a bit bigger than our

play12:53

graphics like we did in the last video

play12:56

make sure that this collider is set to

play12:58

is trigger

play13:00

next add a new script called key pickup

play13:09

after we open this we'll need those

play13:11

three booleans once more

play13:13

we'll call this is red key is blue key

play13:16

and

play13:16

is green key

play13:24

we can delete the update and start

play13:26

function we'll use

play13:27

once more the on trigger enter function

play13:30

and

play13:30

also again we'll compare save as a

play13:32

player so if other

play13:34

dot compare tag player then we'll check

play13:37

what key pick up this is

play13:38

and give the player inventory the

play13:40

correct key

play13:44

so we'll say if is red key

play13:52

other dot get component player inventory

play13:55

dot has red is equal to true

play14:03

and the same for the other two so if

play14:05

it's blue set the blue boolean to true

play14:12

and likewise for the green

play14:18

we'll save and in unity we'll set this

play14:21

to

play14:21

is green and we'll set up the graphics

play14:25

let's call the quad key graphic

play14:33

and we'll just drag our sprite straight

play14:35

onto it

play14:36

and then in the inspector we will need

play14:38

to set shader

play14:43

do sprite and default

play14:47

then finally we'll add the sprite

play14:49

rotator script that we have

play14:55

a minor adjustment to the graphic and

play14:58

everything works but once we run across

play15:00

it then we want it to be destroyed so

play15:02

back in the script we'll just add

play15:04

destroy

play15:06

game object at the end of our if

play15:10

statement

play15:13

and save

play15:17

now in unity you can see in the

play15:19

inspector that our inventory is changed

play15:21

to include the key

play15:22

and then it disappears so now make two

play15:25

more pickups for blue and green

play15:27

using the same steps and changing its

play15:29

settings to match its color

play15:31

finally we can center these and drag

play15:34

them into our pickups prefab folder

play15:41

so now we should have a working door

play15:43

system that we can pick and choose from

play15:45

when we go to build our level

play15:50

so until our next video spawn camp out

Rate This

5.0 / 5 (0 votes)

Related Tags
游戏开发Unity3D动画制作Doom克隆门系统钥匙系统敌人生成教程视频编程逻辑Unity建模
Do you need a summary in English?