How to make a DOOM CLONE in Unity || E1M4 Player Health and Armor
Summary
TLDR在这个视频教程中,Spawn Camp继续开发一个游戏项目,专注于为玩家角色添加健康和装甲系统。首先,他创建了一个'Player Health'脚本来设置最大生命值和装甲值,并在Unity编辑器中通过脚本控制玩家的生命值。接着,他实现了一个'damage player'函数来模拟玩家受到伤害,包括装甲吸收伤害的逻辑。最后,他添加了当玩家生命值降至零时重置场景的功能,使用Unity的Scene Management。视频以对下一集内容的预告结束,承诺将介绍基本的库存系统和触发器。
Takeaways
- 📂 开始前清理项目窗口,将脚本放入脚本文件夹。
- 🕹️ 为玩家添加一个名为'Player Health'的新脚本组件。
- 🔢 在脚本中定义公共变量'maxHealth'和私有变量'health',使用整数类型。
- 🖊️ 在Unity的检查器中设置'maxHealth'的值为100。
- 🛡️ 为玩家添加护甲系统,定义'maxArmor'和'armor'变量。
- 🛑 在'DamagePlayer'函数中添加逻辑,先扣除护甲再扣健康值。
- 🔧 测试时使用键盘右键Shift键来模拟玩家受到伤害。
- 💥 当玩家健康值降到0或以下时,通过Debug.Log输出玩家死亡信息。
- 🔄 玩家死亡后,使用场景管理器重置当前场景。
- 🎮 视频教程接下来将设置基本的库存系统和触发器,包括健康和护甲的拾取以及对玩家造成伤害的环境。
- 📢 视频结束时提醒观众如果喜欢这个系列,请点赞和订阅。
Q & A
视频教程中提到的'spawn camp'是什么意思?
-在视频教程中,'spawn camp'可能是指开发者在游戏开发过程中的一个阶段,即创建敌人并设置它们在游戏世界中生成(spawn)并停留在特定地点(camp)的行为。
为什么要在视频中暂停一段时间?
-视频中的制作者提到因为假期而暂停了一段时间,这表明制作者可能需要休息或处理个人事务,这是很常见的情况。
视频中提到'doom guy wannabe'是什么意思?
-'doom guy wannabe'在这里指的是玩家控制的角色,类似于经典游戏《毁灭战士》(Doom)中的主角。
为什么在Unity中添加脚本组件后,脚本会出现在根资产文件夹而不是脚本文件夹?
-这是因为Unity的默认行为是通过'添加组件'按钮添加新组件时,Unity会将脚本文件放在根资产文件夹中。如果想要脚本出现在特定的文件夹,需要手动将其拖放到该文件夹中。
在Unity脚本中,为什么使用'public int maxHealth'而不是直接使用'public int health'?
-使用'public int maxHealth'是为了区分最大生命值和当前生命值。这样可以在不改变最大生命值的情况下,通过脚本或游戏逻辑来修改当前生命值。
为什么在脚本中将'health'变量设置为私有(private)?
-将'health'变量设置为私有是为了封装,防止外部直接修改这个变量,确保只有脚本内部可以修改它,从而维护数据的一致性和安全性。
在Unity中,如何测试玩家受到伤害的功能?
-在Unity中,可以通过在Update函数中添加测试代码来模拟玩家受到伤害。例如,使用Input.GetKeyDown检测特定按键(如右Shift)被按下,并调用damagePlayer函数来模拟伤害。
为什么在处理玩家受到伤害时要考虑装甲(armor)?
-考虑装甲是为了增加游戏的复杂性和策略性。装甲可以在玩家受到伤害时吸收一部分伤害,保护玩家的生命值,使游戏玩法更加丰富。
如何在游戏中实现装甲的逻辑,使其在受到伤害时先于生命值被消耗?
-在damagePlayer函数中,首先检查玩家是否拥有装甲。如果有,根据装甲的值和受到的伤害来决定是消耗装甲还是将剩余的伤害转移到生命值上。如果装甲值大于或等于伤害,则只消耗装甲;如果装甲值小于伤害,则装甲被完全消耗,剩余的伤害转移到生命值上。
在Unity中,如何实现玩家死亡后重置场景的功能?
-在Unity中,可以通过SceneManagement命名空间中的SceneManager类来加载当前场景。在玩家生命值降到零时,使用SceneManager.LoadScene方法加载当前场景的索引,从而实现场景重置。
Outlines
🔧 玩家健康系统设置
视频脚本介绍了如何在游戏开发中为玩家角色设置健康系统。首先,开发者在Unity项目中为玩家角色添加了一个新的脚本组件,命名为'Player Health'。在脚本中定义了最大生命值(max health)和当前生命值(health),其中最大生命值在Unity的Inspector面板中设置,而当前生命值在脚本中初始化为最大生命值。接着,脚本中添加了一个'damage player'函数,用于减少玩家的生命值。通过在Update函数中使用Input.GetKeyDown来测试伤害,每当按下右Shift键时,玩家将受到固定的伤害,并在控制台中打印出伤害信息。此外,还提到了Doom游戏中的装甲系统,并计划在接下来的开发中实现。
🛡️ 玩家装甲与伤害处理逻辑
在玩家健康系统的脚本中,开发者进一步增加了装甲系统,并实现了伤害处理逻辑。首先,定义了最大装甲值(max armor)和当前装甲值(armor),并在游戏开始时将装甲值设置为最大装甲值。然后,开发者在'damage player'函数中加入了判断逻辑,以确定伤害是先作用于装甲还是直接作用于玩家的生命值。具体逻辑为:如果装甲值大于伤害值,则只减少装甲;如果装甲值小于伤害值,则装甲被完全消耗,剩余的伤害再作用于玩家的生命值。通过这种方式,玩家的装甲可以吸收部分伤害,保护玩家的生命值。最后,脚本中还添加了当玩家生命值降至零或以下时的死亡逻辑,通过Debug.Log输出玩家死亡信息,并使用SceneManager重置当前场景,以模拟玩家死亡后的场景重载。
Mindmap
Keywords
💡敌人检测
💡玩家健康
💡装甲
💡Unity
💡脚本组件
💡伤害
💡Debug模式
💡场景管理
💡测试
💡玩家死亡
Highlights
视频作者在假期后回归,希望观众新年伊始过得好。
之前的系列视频已经建立了能够检测玩家并朝玩家移动的敌人。
接下来的步骤是优化代码,让敌人能够实际攻击玩家。
视频将暂时从敌人设置中抽离,开始为玩家设置健康值。
通过Unity编辑器为玩家添加名为'Player Health'的新脚本组件。
脚本通过两种方式添加:直接添加组件或拖拽脚本到玩家对象。
使用Visual Studio编辑脚本,设置最大生命值和当前生命值。
在Unity检查器中设置最大生命值为100。
通过Debug模式在检查器中查看私有的健康变量。
创建'damage player'函数来模拟玩家受到伤害。
使用Input.GetKeyDown在测试中模拟伤害。
实现基本的装甲系统,类似于Doom游戏中的装甲。
在'damage player'函数中添加逻辑以优先消耗装甲。
如果装甲足以吸收全部伤害,则不会影响玩家生命值。
如果装甲不足以吸收全部伤害,则剩余伤害会转移到玩家生命值。
在玩家生命值降至零或以下时,重置场景并显示玩家已死亡。
使用SceneManager加载当前场景以重置游戏。
视频结束时,预告下一集将设置基本的库存系统和触发器。
Transcripts
hey spawn camp here sorry for the hiatus
but i took a break for the holidays
i hope you all had a great holiday and
hopefully started 2021 off well
we left off this series just haven't
built these enemies they can detect the
player and has temporary code to move
toward the player
our next step is to refine this code and
have them actually be able to attack the
player
but first in this video we're gonna step
back from the enemy for a second and
we're gonna set up our doom guy wannabe
aka player to have some health so we can
actually damage him
like always we'll start by opening our
project and for the first thing i'll
clean up this project window since i
forgot to last video
and i'll put my scripts in the script
folder
now we can work on our players so we'll
click on the player root game object
and we'll go under these scripts and
we'll add a new component
and it's just going to be a basic script
that will name player health
as you can see here the script was added
into the root asset folder
this is the result of adding a component
through this add component button here
alternatively you can go into the
scripts folder and create a new c
sharp file and name it player health and
then you can just drag this script onto
the player object
and the results are basically the same
this way doesn't require you to tidy up
your projects so often
but the ad component is my go-to so it's
up to you how you do this
but anywho let's double click here to
bring up our script inside our editor
i was using writer but you'll see now
that i'm back on visual studio
but they work practically the same all
right let's jump right into it
at the top here above our start function
we're going to say
public int max health
and we're going to use private int
for our health and these are ants
because we're only going to use whole
numbers
and the max health is public because
we're going to set it in the inspector
our health is a private variable because
it'll get modified
in the script like here in the start
function we're just going to say our
health
equals our max health so we're going to
control s to save and then if we go back
into unity and check out our inspector
you'll see that we have a max health
variable now
we're going to keep it basic and we're
going to say our max health is 100. to
see our private variable we can go up
here to these three dots on the
inspector
and we can change this to debug mode and
you'll see now that we have
our private health variable and if we
just go to play you'll see that our
script just sets our
health to our max health now let's have
our player take some basic damage
under our update we're going to say
public void
damage player and since our health is an
integer we're also going to pass in an
integer in this function and we're going
to name it damage and our logic in this
function is going to start off really
easy
and we're just going to say our health
is minus equal damage
and the reason this function is public
is because it's going to be accessed
from outside the script but for now
we're just going to test it so we're
going to make
a test function up inside of our void
update
call and we're going to say if
input.getkeydown and for test purposes i
like to use the right side of my
keyboard so we're going to use the right
shift
and then to test the damage we're just
going to say damage player
and here it's going to take our integer
and we're just going to say 30 and then
we're going to log
a message so we can see it actually
happening
so we're going to say debug.log player
damaged
[Music]
then if you save and go into unity and
play test you'll see
that our health does go down by 30 every
time we hit shift
and it even goes into negatives but
we'll fix that later
so we have our basic health but doom
also has basic armor
so let's do that
so back in our player health script
we're going to do the exact same thing
up here at the top we're going to use a
public int
max armor and we're going to use a
private int for armor
and in the start we're gonna set our
armor
to equal our max armor
and this is just for our testing
purposes because
usually you'd want your player to start
with no armor and have to pick it up as
they go
so down the road we will have this where
it sets a zero from the start
so we're going to need a bit more logic
because now we need the damage to affect
the armor before it affects the player's
health
we'll include all this logic inside the
damaged player function that we already
have
so the gist of it is if the player has
armor damage at first
but there needs to be deeper logic than
this because if the player has enough
armor to absorb
all the damage then we only want to
damage the armor
and if the player only has enough armor
to absorb some of the damage then we
want the armor to be damaged first
and then the rest of the damage to go to
the player
so using these comments will flesh out
this function
so first we'll find out if the player
has any armor at all
we'll use an if statement and say if
armor is greater than zero
we want to damage the armor else if we
don't have any armor then we just
damaged the player
like normal so we'll grab this line we
already have and move it into the else
condition of the if statement
that takes care of our first comment
here so next we'll take care of the
other two
both require the player to have some
armor so inside the if statement where
we check if we have armor
then we'll have another check to figure
out how much we have and how we'll
handle the damage
so if our armor is greater than or equal
to the damage
our player's health remains the same and
will issue all the damage to the armor
by saying armor minus equals damage
now we'll say else if armor is less than
damage
and will need to figure out how much the
armor can take and give the remaining
damage to the player
so to do this we'll use an int called
remaining damage
and this will be a local variable
because it's not needed outside of this
function
so now we'll just say remaining damage
equals
damage minus armor that'll set the
remaining damage to whatever is left
that the armor can't handle
so now that the armor is depleted we'll
just set the armor to zero
and then we'll say health minus equals
remaining damage and that should divide
the damage between the armor
and the health and that'll knock out the
last to-do comment so we'll clean this
up and we'll get rid of the comments and
we can save
and test in unity you'll see at the
bottom right that we're still in debug
mode so we see our values changed in the
inspector
so at the start you'll see how our
health and armor are set to the max
so now if we press the shift key it
damages the armor first so we went from
having 50 to 20.
so now if we damage him again it's going
to take all the remaining 20 from the
armor
and give the other 10 to our players and
our players health is at 90
and if we continue to spam the damage
key eventually we go into negative
numbers
so as our last step we'll add in
something to kill off the player
so let's go back into our player health
script
so we could put a check in our update
something like
if health is less than or equal to zero
then the player is dead
and if this is a smaller game that's
probably fine but since we know that the
player's health
only changes when we get damaged then we
can go inside of our damaged player
function
and check there and that way the check
is not happening every frame and just
bogging our performance down
so let's remove that and down in our
damaged player underneath all this logic
then we're gonna check if we're dead so
we're gonna say
if our health is less than equal to zero
then the player is dead
an easy way for this to be displayed to
us is just to use a debug.log
so we can say debug.log player has died
but imma go one farther and i'm going to
set it where our scene resets
when we die to reset our scene we'll
need our scene manager
so at the very very top we're going to
add a using statement
and it's going to be used in unity
engine dot scene management
then back in the very bottom below our
debug.log
we can simply say scene current scene
equals scene manager dot get active
scene
that's going to return this scene and
then we're going to say
scene manager dot load scene and this
takes in an integer so we're going to
say our current scene that we just set
and we're going to say dot build index
so now we can save
and go into unity for one final test
so we're just going to move over here so
we can see when our scene resets
we're just gonna spam our damaged test
key
and when our health reaches zero
the scene resets and you can see here in
our console
that the player has indeed died so
that's everything working
and before i forget let's go back into
the inspector here on these three dots
and we'll set this
back to normal mode that's about it for
this video we have enemies and we have a
player with armor and health
the next episode will probably set up a
basic inventory and also mess around
with triggers
some will give the player health and
armor such as pickups and others will
damage the player such as lava or toxic
waste
if you're enjoying the series please
like and subscribe so you don't miss the
next one
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 || E1M6 Doors and Keys
How to make a DOOM CLONE in Unity || E1M3 Enemy AI and NavMesh Part 1
Simple Inventory (PART 1: Adding Items to Player Inventory)
How to make a DOOM CLONE in Unity || E1M2 Enemy Hit Detection
5.0 / 5 (0 votes)