How to make a DOOM CLONE in Unity || E1M1 First Person Player
Summary
TLDR本视频教程介绍了如何从零开始在Unity中创建一个毁灭战士(Doom)风格的克隆游戏。内容涵盖了从玩家移动到添加特效和打磨的各个方面。首先设置基础场景,实现基本玩家移动。接着创建新项目,使用Unity的标准3D模板,并进行场景设置,包括调整摄像机、光源、创建材质和脚本文件夹。然后创建环境、玩家模型,并添加摄像机和角色控制器组件。随后,通过编写脚本来控制玩家的移动和视角旋转,实现玩家的键盘操作和鼠标平滑转动。最后,添加头部晃动动画和动画控制器,以增强玩家的游戏体验。视频以展示完成的可操作角色和环境结束,鼓励观众继续关注后续教程。
Takeaways
- 🎮 视频教程将从零开始在Unity中创建一个Doom克隆游戏。
- 🏗️ 教程将涵盖从玩家移动到添加效果和润色的所有内容。
- 📁 创建一个名为'doom clone'的新项目,并使用Unity内置的标准3D模板。
- 🖼️ 场景设置包括更改清除标志为深灰色,调整摄像机和定向光的设置。
- 🗂️ 创建了'materials'和'scripts'两个文件夹,用于存放游戏的材质和脚本。
- 🔨 制作了基础色、次要色和玩家色三种材质,其中玩家色使用醒目的霓虹色。
- 🏰 环境设置包括创建平面作为地板,并使用基础色材质。
- 🧩 创建了环境、玩家和玩家模型等空物体,并设置了它们的位置和属性。
- 👾 玩家模型添加了摄像机和角色控制器组件,用于控制玩家的视角和移动。
- ⌨️ 脚本编写包括玩家移动和鼠标视角控制,使用Input.GetAxisRaw获取键盘输入。
- 🔧 鼠标视角控制脚本中使用了平滑值和灵敏度设置,并通过Transform进行旋转。
- 🎨 为玩家添加了动画效果,包括静止和行走时的头部晃动。
- 🔄 动画设置包括创建动画窗口和Animator窗口,以及设置动画状态机。
Q & A
这个系列视频是关于什么的?
-这个系列视频是关于如何在Unity中从零开始制作一个Doom克隆游戏。
制作Doom克隆游戏需要哪些Unity组件?
-需要使用Unity的内置渲染管线、标准3D模板、场景设置、相机、方向光、材料、脚本、环境设置、玩家模型以及摄像机控制等组件。
为什么需要创建两个新文件夹,分别用于存放什么?
-需要创建两个新文件夹,一个用于存放材料(Material),另一个用于存放脚本(Script),以便于管理和组织项目资源。
在Unity中设置场景时,如何改变相机的清除标志?
-在Unity中设置场景时,可以通过选择相机,然后在相机的Clear Flags属性中选择一个固体颜色,例如深灰色。
如何调整Unity中的自由摄像机的宽高比?
-可以通过选择相机,然后在Aspect属性中更改宽高比,例如设置为16x9。
在Unity中创建环境时,如何设置地面?
-可以通过创建一个新的3D对象,选择平面(Plane),然后将其命名为'floor',并在X和Z轴上缩放至40x40,确保位置为0,0,0,并拖动基础颜色到地面上。
如何为玩家添加移动能力?
-可以通过为玩家添加一个Character Controller组件,并编写脚本来控制玩家的移动。
玩家移动脚本中需要哪些变量?
-玩家移动脚本中需要一个float类型的player speed变量来控制玩家移动速度,还需要一个Character Controller组件的引用。
如何实现玩家的旋转移动?
-可以通过添加一个Mouse Look组件来实现玩家的旋转移动,并通过脚本来平滑处理鼠标输入。
如何锁定鼠标并隐藏光标?
-可以通过设置Cursor.lockState为CursorLockMode.Locked,并设置Cursor.visible为false来锁定鼠标并隐藏光标。
如何为玩家添加头部晃动(head bop)动画?
-可以通过创建动画窗口和Animator窗口,为相机创建'cam idle'和'cam walk'两个动画状态,并在脚本中根据玩家是否移动来切换动画状态。
如何将动画状态与玩家移动状态同步?
-可以通过在玩家移动脚本中添加一个Animator组件的引用,并使用布尔值来跟踪玩家是否在移动,然后通过Animator组件的SetBool方法来控制动画状态的切换。
Outlines
🎮 Unity中创建基础场景与玩家移动
本段介绍了在Unity中创建一个名为'doom clone'的新项目,并使用标准3D模板来设置基本场景。首先更改场景名称,调整摄像机的清除标志为深灰色,并设置为16:9的宽高比。接着创建两个新文件夹分别用于存放材料和脚本。在材料文件夹中创建了三种材料:基础颜色、次要颜色和玩家颜色。然后设置环境,创建了一个平面作为地板,并添加了几个立方体作为参考点。最后,创建了玩家对象,添加了摄像机和角色控制器组件,并为玩家添加了一个胶囊形状的模型,用于表示玩家的身体。
📝 编写玩家移动脚本
在Unity中创建玩家移动脚本,首先定义了玩家速度和获取角色控制器组件的变量。在Start函数中初始化角色控制器组件。Update函数中,创建了input和movePlayer函数来处理玩家的输入和移动。使用Vector3来组合玩家的水平和垂直移动,并添加重力影响。通过TransformDirection函数确保玩家面向移动方向。最后,将输入向量与玩家速度和重力向量结合,并通过CharacterController的move函数实现玩家移动,确保移动与帧速率无关。
🐭 添加鼠标视角控制
为了添加鼠标视角控制,创建了MouseLook脚本,定义了x鼠标位置、平滑鼠标位置、灵敏度和平滑变量。使用lerp函数对鼠标输入进行平滑处理,并通过Transform的localRotation和Quaternion的angleAxis函数实现玩家的视角旋转。在Start方法中锁定光标并隐藏它,以提供沉浸式体验。调整了脚本中的灵敏度和平滑值,并通过Unity的Animator窗口为摄像机添加了idle和walk两种动画状态,以实现头部晃动效果。
🏃 实现行走动画与动画状态控制
为了根据玩家的移动状态播放相应的动画,定义了一个Animator组件变量camAnim,并在脚本中添加了一个布尔变量isWalking来跟踪玩家是否在移动。通过检测角色控制器的velocity的magnitude来判断玩家是否在行走,并设置Animator的布尔参数'isWalking'来控制动画播放。在Animator窗口中设置了从默认动画到cam walk的过渡,并在isWalking为false时返回到cam idle状态。最后,将主摄像机拖拽到camAnim字段中,完成行走动画和动画状态控制的设置。
Mindmap
Keywords
💡Unity
💡Doom克隆
💡玩家移动
💡摄像机
💡材质
💡环境设置
💡角色控制器
💡脚本
💡动画
💡Animator
Highlights
创建Doom克隆游戏系列教程,涵盖从玩家移动到添加特效和打磨的各个方面。
设置基本场景,实现基础玩家移动。
使用Unity内置的标准3D模板创建新项目。
更改场景名称,设置摄像机清除标志为深灰色。
调整摄像机宽高比为16x9,修改定向光颜色为白色并降低强度。
创建materials和scripts文件夹,制作基础色、次要色和玩家色材质。
设置环境,创建平面作为地板,调整位置和缩放。
创建立方体作为参考点,确保不阻碍玩家移动。
创建玩家对象,添加摄像机和角色控制器组件。
为玩家添加胶囊形状的模型,并分配玩家材质。
编写玩家移动脚本,实现键盘控制的横向移动和鼠标控制的旋转移动。
使用TransformDirection函数修正玩家面向移动方向的问题。
添加MouseLook脚本,实现平滑的鼠标视角控制。
锁定鼠标光标并隐藏,以增强沉浸感。
为玩家添加头部晃动动画,提升游戏真实感。
使用Animator窗口创建和配置玩家行走和静止时的动画。
在PlayerMove脚本中添加逻辑,根据玩家是否移动来切换动画状态。
将摄像机的Animator组件链接到脚本,实现动画控制。
完成Doom克隆游戏的基础构建,并提供了后续开发的方向。
Transcripts
in this series we're going to be making
a doom clone in unity from scratch
we'll be covering a little bit of
everything from player movement to
polishing and adding effects
in this video we're going to set up a
basic scene and have a basic player move
around it we're going to start with a
new project named doom clone and for
this project we'll be using the built-in
render pipeline just by selecting the
standard 3d template
so we're going to start with a fresh
project and this has the default layout
the first thing we want to do is go into
our scene
folder and rename our scene
so let's do some basic scene setup we're
going to expand our scene
we're going to go to our main camera
first let's change the clear flags to a
solid color
and here we're just going to use a dark
gray
now we can go over here and drag our
game window down here to the corner
right
rearrange my windows a bit on the free
aspect we want to change that to 16x9
go back to our directional light we're
going to change
this from the pale yellow to a white and
we're going to drop the intensity just a
bit so it's not so blinding
so back in the project window we're
going to go to our asset root folder
and we're going to create two new
folders we're going to make one for
materials and we're going to make one
for scripts
so straight to our material folder we're
going to make one new material
called base color we're going to
duplicate this
and hit f2 to rename the others we're
going to have a secondary color and
we're going to have
a player color we're going to keep the
tone and for the base and secondary
colors we're going to use
a gray shade and for our player we want
him to stand out so let's use something
like a
neon color so now we need to set up our
environment
so to do this we can go back to our
hierarchy we're going to create a new
empty
we can rename this guy to environment so
after we do that we need to go and make
sure our position on our transform is
set to zero zero zero
so now we can go back to the root we're
going to create a new 3d object
we're going to make a plane and we're
going to call this floor
and we're going to scale it to 40 by 40
on the x and z
make sure that our position is still 0 0
0 and then we'll just drag our base
color
onto the floor
what i like to do is to create a few
cubes we're going to
move this cube above the floor we're
going to scatter this cube around a
little bit to give us some reference
points and then make sure to drag it
forward on the z so it's not in the way
of our player
so finally it's time to make our player
in the hierarchy we're going to create
another
empty game object we'll call this one
player and we'll check once more that
his position is zeroed out
now our player needs the ability to see
so let's grab the main camera
drag that straight onto the player let's
zero that camera out
let's go back on the root of the player
and in the inspector we're going to add
a character controller component
so now that we have something to look at
let's hover over the scene window and
hit
f to focus and we'll see that we need to
pull this guy out of the ground a bit
you'll also see that the camera is not
positioned where the player's head would
be so let's select it
and drag to where we think it should be
we can make this easier by giving our
player a body
that we can see so go to the root again
we're going to create a new capsule
we'll rename this guy player model and
then we're going to give it our player
material
so let's make sure to remove its mesh
collider since the character controller
already has one
and we should be ready to start
scripting
before i hop into code i like to
brainstorm so on this character we're
going to need two types of movement
we're going to need a lateral movement
for our x and z
and that's going to be controlled by our
keyboard and then we want a rotational
movement
that's going to be controlled by our
mouse so i think the first script we're
going to tackle is player movement
so let's go to our player we're going to
add a component
we're going to call this player move
we're going to hit
enter twice let it compile and then
we'll double click it to open it in your
text editor of choice
so we'll clean this up a bit by getting
rid of these comments
let's make some room here at the top so
two variables i know i need
is a float the player speed for us to be
able to control the player and we're
going to default that to 20 because it's
a good value
and we also need the character
controller component
so we'll type in private character
controller and then we'll just call this
my character controller so in the start
we're going to say my cc equals and this
is the component that we're grabbing
and we're going to say get component and
it's just going to be character
controller
so now we can go into the update and
we're going to lay out the script a
little bit
so we're going to create a few calls for
some functions
we're going to say we need to get input
and then we need to
move player by using that input
so we can go down below the update
method and actually make these functions
so we can just say void input and void
move player
so now let's think about this input
earlier i was saying we only need two
coordinates the x and the y
but for this we're going to use some
vector three so we're going to take
the player movement with no y
we're going to make a new vector for
gravity with only a y
we'll combine those and pass the final
vector to our character controller
so let's start by making a few of these
vectors we're going to need
one for our input vector and then we
need another one
to pass into our player controller this
is going to be called movement vector
and then we need a float for our gravity
and we're going to set this guy to
negative 10.
all right i think that's all the
variables that we need
so now let's go into our first function
and start using these variables so for
the first thing
we want to take our first vector our
input vector
and we're going to set this to a new
vector 3 and that's because we need to
assemble this vector we need the
x to be input dot get axis raw
so we want this to be between zero and
one
this axis is going to be the horizontal
axis
so if we go into unity and go to our
project settings
and open the input manager we can see
that there's
multiple axes one is horizontal
there's also a vertical and that's what
we're going to use for our z
component so we're just going to copy
this
paste under our z and we're going to
call this vertical
and for our y-coordinate this is where
we're going to add gravity so we're
going to leave this at
0 for now on the next line we need to
compensate for diagonal movement
player player is going forward and left
at the same time now if he was only
moving one direction
he'd be fine but you can see if you
combine the two inputs players actually
farther than one unit to fix this we can
normalize these values to return
something in this circle so the speed is
constant to do this we'll just take the
variable and we'll add dot
normalize to the end if we were to run
the code like this you'll see that we
have a problem where our player
doesn't actually move the direction that
it's facing
to fix that issue we can pass it through
a
unity function called transform
direction
now that we have our input vector set up
and built the way we want it
it's time to take that input vector
and we're going to modify it and put it
in a variable called movement vector
so to begin we're just going to say our
input vector
times our player speed and that'll give
us our first vector
then we're going to combine this with a
gravity vector that we make so we're
just going to say
vector 3 dot up which is unity's y-axis
we're going to multiply this times my
gravity which was negative 10.
now we're done with our input so we can
go on to our
player move and we can say mycc
which is referenced in the character
controller it has a function
called move and it takes in a vector 3.
so we're just going to pass in this
final vector 3 that we made
and we're going to multiply it by time
dot delta time to make it frame
independent so we're not running faster
on faster pcs
so here's what we got so far it's
looking pretty good
but you can see that we don't have any
mouse movement so let's go
back into our project and fix that so
now we're going to add another component
to our player
called mouse look
once this opens we can clean it up a bit
by deleting the comments
and we'll also delete the start function
that i definitely need later
so on this script we're going to be
using floats for our variables since we
only have one
axis to deal with so our first
input is going to be our x mouse
position
then we're going to take this x mouse
input and we're going to smooth it so we
need another float called smooth mouse
position
and since we know we're going to be
modifying it let's go up here
we're going to create two floats that we
can modify in the inspector
one's going to be called sensitivity and
we're going to set that to
[Music]
1.5
and the other float is going to be
called smoothing
and we're going to set that to 10
and lastly we need a variable to
actually rotate our player
and we're going to calculate that by
adding our rotation onto
a variable called current look position
so let's lay out our script a little bit
in our update we're going to need a few
functions to call
get input we're going to modify input
and rotate player then we'll go below
the update and we're going to create
these
so firstly we're going to store our
input in x mouse position
and we're going to use another get axis
raw because we want to apply our own
smoothing
and this time we're going to use our
mouse x
axis
[Music]
so now we go straight to modifying our
input
first thing we're going to do is we're
going to add sensitivity
to our new input we're going to do that
simply by saying
times equals sensitivity and we also
want smoothing to affect this
so next we want our main smoothing to be
applied
so we're going to say smooth mouth
position and we're going to use
a lerp which just literally interpolates
one value to another
for our a value we're going to use
smooth mouse position
our b value we'll use x mouse position
and then for the time we use smoothing
but since this takes a value
between 0 and 1 we're just gonna put one
over smoothing we've modified our input
now it's time to actually rotate our
player
here's where we need to use our current
looking position to store our rotation
and then every frame we're going to add
on to it our smooth mouse position so
we'll say
plus equals now it's time for our actual
rotation
we're going to say transform dot local
rotation because we want this rotate
in local space then we're going to set
this equal to a quaternion and we're
going to use this handy function of
quaternion
called angle axis it's going to rotate
us around a certain axis current looking
position
as our rotation and then the axis that
we want to rotate around
is the transform dot up or our y axis
so it's almost time to save and test but
before we do that we need to lock and
remove our cursors from the screen
so let's resurrect the start method that
i killed earlier
and inside here we're going to lock
these cursors and it's pretty simple
the first thing we need to do is say
cursor dot lock state
and we're going to use cursor lock mode
dot
locked
and then we need to actually hide the
cursor by saying
cursor.visible equals false
back in unity i know the smoothing is
set to 10. let's change that in the
inspector to 1.5 and also go into the
script and change it
here now we can quickly save and go back
in play mode you can see now we have a
decent character controller
walking and moving around our scene one
final thing we can do to spice this up
is to add head bop the way our players
set up
is the camera is inside the player so
the player moves and the camera follows
we can add animation to the camera that
animation will take place within the
parent
to begin we're gonna need two windows
we're gonna need
a animation window
i like to dock it down by the console
and then we also need an animator window
so after we have those open we can go to
our main camera
and we're going to go down and create a
new animation
we're going to need two animations the
first animation is just going to be
static
so we're going to call this cam idle and
we only need one keyframe
so i'm just going to hit the record
button and since we're animating the
y-axis we'll change it
and all you simply need to do is delete
the value
and re-paste it so we can stop the
recording
we'll go here and we're going to create
the next animation
and it's going to be called cam walk
we'll hit the record button once again
and we're going to go about
half a second and we're going to move
the camera
up above the player this creates a
keyframe at half a second but also one
at the beginning so we'll go down here
and highlight these keyframes hit ctrl c
to copy
and then we'll go over to about one and
paste to make this a loop
so before we stop recording we can check
a preview
so we hit this little play button and
it'll give us a preview
and we can see that it's a little too
slow
half a sec it's a little too fast so
we'll go three quarters of a second
and then we can stop the recording and
stop the preview
now we go back to the root of our player
and open our
player move script once more now we need
another variable
and this one's going to be a animator
and we'll just call this cam anim
and we'll also make it public so we can
assign it in the inspector
so now we need a boolean so this script
can keep track of whether we're moving
we're going to call this boolean is
walking
figure this out we're going to check for
head bob
we're going to do that at the very
bottom of our update
so we can go below our move player and
create this function
and the character controller in unity
has a
default variable called velocity
we'll use that velocity to determine
whether our player is actually moving or
not
so we'll say my cc dot velocity
and then we're going to get the
magnitude or the length of that vector
then we can just wrap this up in an if
statement and we're basically just going
to say
if our magnitude is greater than a very
small number
[Music]
then we're walking
[Music]
and if we're not
then that means we must be standing
still
so now that our script has a bull that
tells us whether or not we're walking
let's use that to get our animator to
play the animation that we want
we're going to say canada.set bool
and the first one is a string for the
boolean of the animator
we haven't made that yet but we're going
to call it is walking
and the second parameter is just a true
or false
and we're going to pass in our bull that
we just created here
to set the animator's bull now we say we
go back to unity
so now we need to go to the main camera
or whatever object that our animator is
on
we need to select the animator window
we're going to tidy this up a bit
and now the top left we need to go and
make a parameter
and this is the bull that we just
referenced in our script
and when you make these remember that
the spelling must be exact
to the string that you referenced let's
make a transition from our default
animation by
right clicking over to cam walk we're
going to select that transition
we're going to untick exit time and down
in conditions we're going to
add our boolean set this to true so
when we are walking our cam walk will
play
now we can do the same and make a
transition back to cam
idle as exit time is unticked
and this transition will occur when is
walking is false
in our final step we'll go into our
player and drag our main camera
into the new cam anim field
so now we can hit ctrl s to save or hit
play
and we can see our finished product
and that was it on our first episode i
hope you join us on the next ones
and help me build this into a fleshed
out doom clone spawn camp
out
関連動画をさらに表示
How to make a DOOM CLONE in Unity || E1M6 Doors and Keys
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 || E1M3 Enemy AI and NavMesh Part 1
Unreal Engine 5 RPG Tutorial Series - #14: Equipment System
Unreal Engine 5 RPG Tutorial Series - #9: Combat
5.0 / 5 (0 votes)