How to make a DOOM CLONE in Unity || E1M6 Doors and Keys
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
🚪 制作带有钥匙和敌人生成系统的门
本段视频教程介绍了如何在Doom克隆游戏中添加一个包含钥匙和另一侧生成敌人的门系统。首先,创建一个空的游戏对象作为门的基础,并将其变换设置为零。接着,作为子对象添加门框和门的空游戏对象,并在其中放置门的各个部分。可以使用Unity的标准立方体构建门,或者使用ProBuilder工具进行建模。确保每个部分都有碰撞器。然后,创建一个名为'door'的材料文件夹,并准备基本的门颜色以及红色、蓝色和绿色的材料。接下来,通过动画窗口为门创建'door idle'和'door open'两个动画剪辑,分别代表门的关闭和开启状态。最后,设置Animator窗口中的动画状态和转换,为门添加触发器和条件,以实现玩家接近时门的自动开启。
🔓 门的逻辑和敌人生成
在本段中,继续完善门的逻辑,使其能够检测玩家并触发动画。首先,为门添加一个Box Collider组件,并设置为'Is Trigger'。然后,创建一个名为'door'的脚本,用于处理门的逻辑,包括检测玩家进入触发器区域并设置Animator的触发器。此外,为了解决门动画循环的问题,需要在Unity编辑器中禁用'Loop Time'选项。接下来,介绍了如何通过创建一个名为'area to spawn'的公共游戏对象来控制敌人的生成,确保敌人在门打开后正确生成。最后,展示了如何使用基础门的设置来创建三种不同颜色的门,并为每种门设置不同的钥匙要求。
🗝️ 玩家钥匙系统和门的设置
本段视频教程讲解了如何为玩家设置钥匙系统以及如何为不同颜色的门配置钥匙要求。首先,为玩家添加一个名为'player inventory'的脚本,用于跟踪玩家拥有的钥匙。然后,为每种颜色的门添加一个布尔值,以确定是否需要特定的钥匙才能打开门。在'door'脚本中,通过比较标签和布尔值检查来实现门的开启逻辑。如果门需要钥匙并且玩家拥有相应的钥匙,则门会打开。最后,展示了如何将门和钥匙拾取器设置为预制件,以便在构建关卡时可以重复使用。
🔑 创建钥匙拾取器
在最后一段中,介绍了如何创建钥匙拾取器。首先,创建一个空的游戏对象作为拾取器的父对象,并为其子对象添加一个四边形作为图形。然后,为拾取器添加一个Box Collider,并设置为触发器。接着,创建一个名为'key pickup'的脚本,用于处理玩家拾取钥匙的逻辑。脚本中,使用'OnTriggerEnter'函数检测玩家并根据拾取器的颜色更新玩家库存中的相应布尔值。此外,为了视觉效果,为钥匙图形添加了'Sprite'和'Sprite Rotator'脚本,并设置了材质。最后,一旦玩家拾取了钥匙,拾取器将被销毁,以确保玩家只能拾取一次。
Mindmap
Keywords
💡Unity
💡门系统
💡动画
💡敌人生成
💡空游戏对象
💡碰撞器
💡Animator
💡脚本
💡预制件
💡玩家库存
💡颜色钥匙
Highlights
在视频中,将向Doom克隆游戏中添加一个带有钥匙和敌人生成系统的门。
构建门分为三部分:门基座、门框和门本身。
使用Unity标准立方体或ProBuilder工具来构建门框和门。
确保每个门部件都具有碰撞器,以便正常工作。
使用材质为门添加颜色,包括基本颜色和红、蓝、绿色。
通过动画窗口创建门的关闭和打开动画。
在Animator窗口中设置动画状态和转换。
创建一个脚本来检测玩家进入门的触发器并控制动画。
设置一个触发器来检测玩家并激活敌人生成区域。
创建三种不同颜色的门,每种门需要不同颜色的钥匙。
为玩家添加一个库存脚本来跟踪他们拥有的钥匙。
为每种颜色的门设置需要相应颜色钥匙的逻辑。
将门和钥匙拾取器转换为预制件,以便在构建关卡时重复使用。
创建钥匙拾取器,当玩家触碰时更新其库存并销毁拾取器对象。
为每种颜色的钥匙创建拾取器,并设置相应的颜色和逻辑。
完成的门系统可以根据需要在关卡构建中选择使用。
Transcripts
in this video we'll be adding a door
system to our doom clone
that comes complete with keys and a
system to spawn enemies on the other
side
we'll build each door in three pieces
we'll create a new empty game object
that will be
our door base and zero out the transform
next as children will have a door frame
empty game object
and also a door empty game object
we'll put our door pieces in each empty
accordingly
now there's a couple of ways we can
build our door the easiest way
is use a bunch of standard unity cubes
and piece them together for the door
frame
and then also for the door
as long as you keep this structure any
way you go about making them will work
for us
i'm going to opt out for use in pro
builder
it can be found in windows package
manager
and it's a powerful tool that
practically lets you model within unity
if you'd like a tutorial on how to use
it let me know in the comments
now we still have the same structure and
as long as our pieces
have a collider just as our default cube
did then we're good
importing your own models will also work
fine
now we're almost ready to animate the
door so we'll move it out in front of us
a little bit
but before we do that let's give it a
splash of color
so i made a folder inside my materials
called door
and i added a few materials so we'll
need a basic door color as well as a red
blue and a green
now we're ready to animate so we'll need
our animation window
if yours is missing open it with window
animation and animation now let's click
our door empty game object of our base
door
we'll create a new animation clip called
door idle and we want to save it in our
animation clips folder
this will be our closed state of the
door and it only needs one key frame so
we'll click the record button and we'll
just move the door up a bit and back to
where it was
and then we'll click record button again
to stop and you'll see
one keyframe next we'll need the door
open animation so we'll create a new
animation clip called door open
for this one we'll hit record we'll move
this slider to
0.3 or the half a second mark
and we'll just pull our door to the
position we want when it's open
now you can click record to stop and we
have our animation clips
now we need to go to the animator window
this can also be found in windows and
animation
so the orange clip is our default
starting clip
we need a transition to the door open
clip so
first let's go up here and select
parameters and click the plus
symbol we're going to use a trigger and
we'll name it open door
right click the default clip click make
transition
drag it over to the open door clip and
left click
now we'll select the transition by
clicking the arrow and over in the
inspector let's
disable has exit time we'll also go down
here and click the plus
add a condition and since we only have
one it's going to go ahead set it up for
us
and that's it for the animation
now we need something to detect the
players so we'll go to our main base
door object
and create a box collider if you press
this button here it'll show you the
bounds of the box
and you can edit it by dragging these
little dots
and we want to make it big enough for
the player to enter it from
either side and when we're finished
we'll make sure to set this to
is triggered now we'll add another
component and this will be a script
called door and it will hold all of our
logic
we'll wait for a second for it to
compile and then we'll double click it
to open it in visual studio
we want our door to detect whether the
player has entered the trigger and then
set our trigger in our animator
so we need a public animator reference
called
door anim
and then we could save it and go to
unity real quick and we can just drag in
the door object with the animator into
the door and um slot now back in the
script we can ko the start and update
function because all we're going to need
is a void trigger
enter function
now in the function when it's triggered
will detect if it's the player with
if other dot compare tag in this player
so if this is the player we'll open with
our animator
with dooran
dot set trigger
[Music]
and this string needs to be identical
so pascal case open door
so it now detects the player and opens
we have a little looping problem at the
end
and we can fix this by finding our door
open animation
in the project window and then turning
off loop time
in the inspector
now the door will open and it will stay
open our base door is almost finished
but we need to add one more system
you'll see here that if our enemies are
beyond the locked door we can actually
trigger them
too early instead for now we're just
going to tell the door to also spawn in
any enemies that we need
in that area
to do this i create a simple public game
object
called area to spawn
and right after we open the door we'll
set this game object to be active with
area to spawn dot set active and true
you can now see in the hierarchy that we
have an empty game object in this case
called room one
and it's just a bunch of enemies as
children
we'll keep the enemies active and just
deactivate the object
and then we'll drag this in our door
script
now let's use this base door to make our
three variations
first i'll clean up my scene a little
i'll delete this pedestal that the enemy
is on and drag him
out of the way
let's fix up this dab mesh that i hacked
up for this tutorial
in our window ai navigation will
navigate to our bake tap
making sure my gizmos are on you see
that i've broke it we'll just hit
bake and now it's ready when we need it
back on our door we have everything
ready except it's missing area to spawn
but that's fine
we'll sign this when we're ready when we
go to place the doors
so let's duplicate the entire door
object three times
and this is our green door red door and
blue door
currently they all open for our players
so we'll need to know
what keys are
first let's jump back to our player
below the player health we'll add a new
script called player
inventory and we'll open it up
so here we're just going to use three
public booleans public so we can access
them from the door script and our door
pickup
so this is all that we need so let's
save and go back to unity
now go to any one of the doors and
double click the door script to open it
once more
now we need a public boolean we'll call
this requires key
and in our compare tag we can do an if
check
so if requires key
then we want to do more logic
else requires key is not true then we
want the door to open instead
back at the top we're gonna need three
public booleans just like our player
inventory
this time we're going to use commas and
write it all on one line
so we'll have require red comma require
blue
comma and require green
and let's go back and change our player
inventory script the same way
so we're consistent
so back in the door script now inside
the if statement of requires key
we'll do three checks so we'll say if
required red
so this is the red door and
so we'll grab the player inventory
script off the player when he triggers
this
so we'll say other dot git component and
then we're going to say
player inventory and we'll check if it
has the red key by accessing its boolean
dot has red so if it's a red door and
the player has a red key
we'll open and the same for the other
two doors we'll check against blue
and then we'll also shake against green
and we can copy this door open logic and
paste it in all three of our checks
we can save and go back into unity so we
have our player with three booleans
and then our doors with three matching
booleans and an extra for our base store
our base door will have everything is
false
so set up the other doors by checking
the required key
and then what color for each door now
only the base door will open
but if we were to manually change the
player inventory to have a key
then that door will also open
so now we can turn these into prefabs so
we'll grab
one and center it and then we'll open
our prefabs folder
and make a new one called doors and then
we're going to drag this door in
after it's a prefab we can delete it out
of the scene and then we'll do the same
for the others
center it make it a prefab and delete it
out of the scene
so the doors are finished and it's time
to make the key pickups
this is the exact same process from the
previous video that i have
on pickups and billboards we're going to
need to drag in our sprites that we plan
on using
select them and set their texture type
to sprott
2d and ui we'll apply these settings
we'll create an empty game object and
we're going to start with green pickup
and as a child we'll create a new quad
for our graphics
we'll zero out their positions
and i'm going to drag the pickup game
object above the ground
and forward in front of us a little bit
so next click our quad
or our graphics and make sure its
collider is turned off
back on the root pickup object we'll add
a boss collider
we'll make it a bit bigger than our
graphics like we did in the last video
make sure that this collider is set to
is trigger
next add a new script called key pickup
after we open this we'll need those
three booleans once more
we'll call this is red key is blue key
and
is green key
we can delete the update and start
function we'll use
once more the on trigger enter function
and
also again we'll compare save as a
player so if other
dot compare tag player then we'll check
what key pick up this is
and give the player inventory the
correct key
so we'll say if is red key
other dot get component player inventory
dot has red is equal to true
and the same for the other two so if
it's blue set the blue boolean to true
and likewise for the green
we'll save and in unity we'll set this
to
is green and we'll set up the graphics
let's call the quad key graphic
and we'll just drag our sprite straight
onto it
and then in the inspector we will need
to set shader
do sprite and default
then finally we'll add the sprite
rotator script that we have
a minor adjustment to the graphic and
everything works but once we run across
it then we want it to be destroyed so
back in the script we'll just add
destroy
game object at the end of our if
statement
and save
now in unity you can see in the
inspector that our inventory is changed
to include the key
and then it disappears so now make two
more pickups for blue and green
using the same steps and changing its
settings to match its color
finally we can center these and drag
them into our pickups prefab folder
so now we should have a working door
system that we can pick and choose from
when we go to build our level
so until our next video spawn camp out
浏览更多相关视频
Unreal Engine 5 RPG Tutorial Series - #14: Equipment System
How to make a DOOM CLONE in Unity || E1M1 First Person Player
How to make a DOOM CLONE in Unity || E1M3 Enemy AI and NavMesh Part 1
How to make a DOOM CLONE in Unity || E1M5 Pick-ups and Billboards
Unreal Engine 5 RPG Tutorial Series - #20: AI Behavior Trees Patrolling
How to make a DOOM CLONE in Unity || E1M2 Enemy Hit Detection
5.0 / 5 (0 votes)