How to make a DOOM CLONE in Unity || E1M1 First Person Player

SpawnCampGames
20 Nov 202019:05

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

00:00

🎮 Unity中创建基础场景与玩家移动

本段介绍了在Unity中创建一个名为'doom clone'的新项目,并使用标准3D模板来设置基本场景。首先更改场景名称,调整摄像机的清除标志为深灰色,并设置为16:9的宽高比。接着创建两个新文件夹分别用于存放材料和脚本。在材料文件夹中创建了三种材料:基础颜色、次要颜色和玩家颜色。然后设置环境,创建了一个平面作为地板,并添加了几个立方体作为参考点。最后,创建了玩家对象,添加了摄像机和角色控制器组件,并为玩家添加了一个胶囊形状的模型,用于表示玩家的身体。

05:01

📝 编写玩家移动脚本

在Unity中创建玩家移动脚本,首先定义了玩家速度和获取角色控制器组件的变量。在Start函数中初始化角色控制器组件。Update函数中,创建了input和movePlayer函数来处理玩家的输入和移动。使用Vector3来组合玩家的水平和垂直移动,并添加重力影响。通过TransformDirection函数确保玩家面向移动方向。最后,将输入向量与玩家速度和重力向量结合,并通过CharacterController的move函数实现玩家移动,确保移动与帧速率无关。

10:03

🐭 添加鼠标视角控制

为了添加鼠标视角控制,创建了MouseLook脚本,定义了x鼠标位置、平滑鼠标位置、灵敏度和平滑变量。使用lerp函数对鼠标输入进行平滑处理,并通过Transform的localRotation和Quaternion的angleAxis函数实现玩家的视角旋转。在Start方法中锁定光标并隐藏它,以提供沉浸式体验。调整了脚本中的灵敏度和平滑值,并通过Unity的Animator窗口为摄像机添加了idle和walk两种动画状态,以实现头部晃动效果。

15:04

🏃 实现行走动画与动画状态控制

为了根据玩家的移动状态播放相应的动画,定义了一个Animator组件变量camAnim,并在脚本中添加了一个布尔变量isWalking来跟踪玩家是否在移动。通过检测角色控制器的velocity的magnitude来判断玩家是否在行走,并设置Animator的布尔参数'isWalking'来控制动画播放。在Animator窗口中设置了从默认动画到cam walk的过渡,并在isWalking为false时返回到cam idle状态。最后,将主摄像机拖拽到camAnim字段中,完成行走动画和动画状态控制的设置。

Mindmap

Keywords

💡Unity

Unity是一个跨平台的游戏开发引擎,广泛用于创建二维和三维游戏。在视频中,Unity被用来从头开始制作一个'Doom'克隆游戏,涉及到玩家移动、场景设置、效果添加等多个方面。

💡Doom克隆

Doom克隆指的是模仿经典游戏《Doom》的游戏。在本视频中,制作者计划创建一个类似的游戏,包括基本的场景设置和玩家控制,这是视频教学的核心内容。

💡玩家移动

玩家移动是游戏中的一个基本功能,允许玩家在游戏世界中导航。视频中,制作者通过设置摄像机、添加角色控制器组件以及编写脚本来实现玩家在基本场景中的移动。

💡摄像机

摄像机在Unity中用于确定游戏世界中玩家的视角。视频中,制作者将默认摄像机的清除标志设置为纯色,并调整了摄像机的位置,以模拟玩家头部的位置。

💡材质

在Unity中,材质用于给游戏对象表面添加颜色、纹理和视觉效果。视频中提到了创建不同的材质,如基础颜色、次要颜色和玩家颜色,以区分游戏中的不同元素。

💡环境设置

环境设置是游戏开发中用于构建游戏世界的过程。视频脚本中描述了如何创建一个平面作为地板,并使用材质来装饰它,以及如何散布立方体作为参考点。

💡角色控制器

角色控制器是Unity中的一个组件,它提供了一种简单的方式来控制玩家角色的移动和交互。在视频中,角色控制器被添加到玩家对象上,以实现基本的移动功能。

💡脚本

脚本在Unity中用于编写代码,控制游戏对象的行为。视频中,制作者编写了'Player Move'和'Mouse Look'脚本来控制玩家的移动和视角旋转。

💡动画

动画是游戏中用于创建动态效果的技术。视频中,制作者为摄像机创建了'cam idle'和'cam walk'两种动画状态,以模拟玩家移动时头部的上下摆动。

💡Animator

Animator是Unity中的一个组件,用于控制和播放动画。在视频中,Animator组件被用来根据玩家的移动状态在'cam idle'和'cam walk'动画之间切换。

Highlights

创建Doom克隆游戏系列教程,涵盖从玩家移动到添加特效和打磨的各个方面。

设置基本场景,实现基础玩家移动。

使用Unity内置的标准3D模板创建新项目。

更改场景名称,设置摄像机清除标志为深灰色。

调整摄像机宽高比为16x9,修改定向光颜色为白色并降低强度。

创建materials和scripts文件夹,制作基础色、次要色和玩家色材质。

设置环境,创建平面作为地板,调整位置和缩放。

创建立方体作为参考点,确保不阻碍玩家移动。

创建玩家对象,添加摄像机和角色控制器组件。

为玩家添加胶囊形状的模型,并分配玩家材质。

编写玩家移动脚本,实现键盘控制的横向移动和鼠标控制的旋转移动。

使用TransformDirection函数修正玩家面向移动方向的问题。

添加MouseLook脚本,实现平滑的鼠标视角控制。

锁定鼠标光标并隐藏,以增强沉浸感。

为玩家添加头部晃动动画,提升游戏真实感。

使用Animator窗口创建和配置玩家行走和静止时的动画。

在PlayerMove脚本中添加逻辑,根据玩家是否移动来切换动画状态。

将摄像机的Animator组件链接到脚本,实现动画控制。

完成Doom克隆游戏的基础构建,并提供了后续开发的方向。

Transcripts

play00:03

in this series we're going to be making

play00:04

a doom clone in unity from scratch

play00:06

we'll be covering a little bit of

play00:08

everything from player movement to

play00:09

polishing and adding effects

play00:11

in this video we're going to set up a

play00:12

basic scene and have a basic player move

play00:14

around it we're going to start with a

play00:16

new project named doom clone and for

play00:18

this project we'll be using the built-in

play00:19

render pipeline just by selecting the

play00:21

standard 3d template

play00:23

so we're going to start with a fresh

play00:24

project and this has the default layout

play00:27

the first thing we want to do is go into

play00:29

our scene

play00:30

folder and rename our scene

play00:34

so let's do some basic scene setup we're

play00:36

going to expand our scene

play00:38

we're going to go to our main camera

play00:40

first let's change the clear flags to a

play00:42

solid color

play00:44

and here we're just going to use a dark

play00:46

gray

play00:48

now we can go over here and drag our

play00:50

game window down here to the corner

play00:52

right

play00:53

rearrange my windows a bit on the free

play00:57

aspect we want to change that to 16x9

play01:00

go back to our directional light we're

play01:03

going to change

play01:04

this from the pale yellow to a white and

play01:06

we're going to drop the intensity just a

play01:08

bit so it's not so blinding

play01:10

so back in the project window we're

play01:12

going to go to our asset root folder

play01:13

and we're going to create two new

play01:15

folders we're going to make one for

play01:16

materials and we're going to make one

play01:18

for scripts

play01:19

so straight to our material folder we're

play01:21

going to make one new material

play01:23

called base color we're going to

play01:26

duplicate this

play01:27

and hit f2 to rename the others we're

play01:29

going to have a secondary color and

play01:31

we're going to have

play01:31

a player color we're going to keep the

play01:34

tone and for the base and secondary

play01:36

colors we're going to use

play01:37

a gray shade and for our player we want

play01:40

him to stand out so let's use something

play01:42

like a

play01:43

neon color so now we need to set up our

play01:45

environment

play01:46

so to do this we can go back to our

play01:47

hierarchy we're going to create a new

play01:49

empty

play01:50

we can rename this guy to environment so

play01:53

after we do that we need to go and make

play01:55

sure our position on our transform is

play01:57

set to zero zero zero

play01:58

so now we can go back to the root we're

play02:00

going to create a new 3d object

play02:02

we're going to make a plane and we're

play02:04

going to call this floor

play02:06

and we're going to scale it to 40 by 40

play02:09

on the x and z

play02:11

make sure that our position is still 0 0

play02:13

0 and then we'll just drag our base

play02:15

color

play02:16

onto the floor

play02:20

what i like to do is to create a few

play02:22

cubes we're going to

play02:23

move this cube above the floor we're

play02:26

going to scatter this cube around a

play02:28

little bit to give us some reference

play02:29

points and then make sure to drag it

play02:31

forward on the z so it's not in the way

play02:32

of our player

play02:33

so finally it's time to make our player

play02:36

in the hierarchy we're going to create

play02:38

another

play02:38

empty game object we'll call this one

play02:40

player and we'll check once more that

play02:42

his position is zeroed out

play02:47

now our player needs the ability to see

play02:49

so let's grab the main camera

play02:50

drag that straight onto the player let's

play02:52

zero that camera out

play02:56

let's go back on the root of the player

play02:58

and in the inspector we're going to add

play03:00

a character controller component

play03:02

so now that we have something to look at

play03:04

let's hover over the scene window and

play03:06

hit

play03:06

f to focus and we'll see that we need to

play03:08

pull this guy out of the ground a bit

play03:13

you'll also see that the camera is not

play03:15

positioned where the player's head would

play03:16

be so let's select it

play03:18

and drag to where we think it should be

play03:20

we can make this easier by giving our

play03:22

player a body

play03:23

that we can see so go to the root again

play03:26

we're going to create a new capsule

play03:27

we'll rename this guy player model and

play03:30

then we're going to give it our player

play03:32

material

play03:36

so let's make sure to remove its mesh

play03:38

collider since the character controller

play03:39

already has one

play03:41

and we should be ready to start

play03:44

scripting

play03:46

before i hop into code i like to

play03:47

brainstorm so on this character we're

play03:49

going to need two types of movement

play03:50

we're going to need a lateral movement

play03:51

for our x and z

play03:53

and that's going to be controlled by our

play03:54

keyboard and then we want a rotational

play03:57

movement

play03:57

that's going to be controlled by our

play03:59

mouse so i think the first script we're

play04:01

going to tackle is player movement

play04:02

so let's go to our player we're going to

play04:04

add a component

play04:05

we're going to call this player move

play04:08

we're going to hit

play04:08

enter twice let it compile and then

play04:11

we'll double click it to open it in your

play04:13

text editor of choice

play04:15

so we'll clean this up a bit by getting

play04:16

rid of these comments

play04:18

let's make some room here at the top so

play04:20

two variables i know i need

play04:22

is a float the player speed for us to be

play04:25

able to control the player and we're

play04:28

going to default that to 20 because it's

play04:29

a good value

play04:31

and we also need the character

play04:33

controller component

play04:34

so we'll type in private character

play04:36

controller and then we'll just call this

play04:39

my character controller so in the start

play04:42

we're going to say my cc equals and this

play04:44

is the component that we're grabbing

play04:46

and we're going to say get component and

play04:49

it's just going to be character

play04:54

controller

play04:56

so now we can go into the update and

play04:58

we're going to lay out the script a

play04:59

little bit

play05:00

so we're going to create a few calls for

play05:02

some functions

play05:03

we're going to say we need to get input

play05:06

and then we need to

play05:07

move player by using that input

play05:10

so we can go down below the update

play05:12

method and actually make these functions

play05:15

so we can just say void input and void

play05:17

move player

play05:18

so now let's think about this input

play05:20

earlier i was saying we only need two

play05:22

coordinates the x and the y

play05:24

but for this we're going to use some

play05:25

vector three so we're going to take

play05:27

the player movement with no y

play05:30

we're going to make a new vector for

play05:32

gravity with only a y

play05:33

we'll combine those and pass the final

play05:35

vector to our character controller

play05:39

so let's start by making a few of these

play05:41

vectors we're going to need

play05:42

one for our input vector and then we

play05:46

need another one

play05:47

to pass into our player controller this

play05:50

is going to be called movement vector

play05:55

and then we need a float for our gravity

play06:00

and we're going to set this guy to

play06:02

negative 10.

play06:08

all right i think that's all the

play06:10

variables that we need

play06:12

so now let's go into our first function

play06:15

and start using these variables so for

play06:18

the first thing

play06:20

we want to take our first vector our

play06:22

input vector

play06:23

and we're going to set this to a new

play06:25

vector 3 and that's because we need to

play06:28

assemble this vector we need the

play06:31

x to be input dot get axis raw

play06:35

so we want this to be between zero and

play06:37

one

play06:38

this axis is going to be the horizontal

play06:40

axis

play06:42

so if we go into unity and go to our

play06:44

project settings

play06:45

and open the input manager we can see

play06:48

that there's

play06:48

multiple axes one is horizontal

play06:52

there's also a vertical and that's what

play06:54

we're going to use for our z

play06:55

component so we're just going to copy

play06:57

this

play06:59

paste under our z and we're going to

play07:02

call this vertical

play07:04

and for our y-coordinate this is where

play07:05

we're going to add gravity so we're

play07:07

going to leave this at

play07:08

0 for now on the next line we need to

play07:11

compensate for diagonal movement

play07:12

player player is going forward and left

play07:14

at the same time now if he was only

play07:16

moving one direction

play07:17

he'd be fine but you can see if you

play07:19

combine the two inputs players actually

play07:21

farther than one unit to fix this we can

play07:23

normalize these values to return

play07:24

something in this circle so the speed is

play07:26

constant to do this we'll just take the

play07:28

variable and we'll add dot

play07:29

normalize to the end if we were to run

play07:32

the code like this you'll see that we

play07:33

have a problem where our player

play07:35

doesn't actually move the direction that

play07:37

it's facing

play07:43

to fix that issue we can pass it through

play07:45

a

play07:46

unity function called transform

play07:49

direction

play07:53

now that we have our input vector set up

play07:56

and built the way we want it

play07:57

it's time to take that input vector

play08:00

and we're going to modify it and put it

play08:02

in a variable called movement vector

play08:04

so to begin we're just going to say our

play08:07

input vector

play08:07

times our player speed and that'll give

play08:10

us our first vector

play08:14

then we're going to combine this with a

play08:18

gravity vector that we make so we're

play08:20

just going to say

play08:21

vector 3 dot up which is unity's y-axis

play08:25

we're going to multiply this times my

play08:27

gravity which was negative 10.

play08:30

now we're done with our input so we can

play08:32

go on to our

play08:33

player move and we can say mycc

play08:36

which is referenced in the character

play08:38

controller it has a function

play08:40

called move and it takes in a vector 3.

play08:43

so we're just going to pass in this

play08:44

final vector 3 that we made

play08:46

and we're going to multiply it by time

play08:48

dot delta time to make it frame

play08:49

independent so we're not running faster

play08:51

on faster pcs

play08:53

so here's what we got so far it's

play08:55

looking pretty good

play08:56

but you can see that we don't have any

play08:58

mouse movement so let's go

play08:59

back into our project and fix that so

play09:02

now we're going to add another component

play09:04

to our player

play09:04

called mouse look

play09:08

once this opens we can clean it up a bit

play09:10

by deleting the comments

play09:17

and we'll also delete the start function

play09:19

that i definitely need later

play09:22

so on this script we're going to be

play09:23

using floats for our variables since we

play09:25

only have one

play09:26

axis to deal with so our first

play09:29

input is going to be our x mouse

play09:32

position

play09:34

then we're going to take this x mouse

play09:36

input and we're going to smooth it so we

play09:38

need another float called smooth mouse

play09:40

position

play09:45

and since we know we're going to be

play09:47

modifying it let's go up here

play09:49

we're going to create two floats that we

play09:50

can modify in the inspector

play09:52

one's going to be called sensitivity and

play09:54

we're going to set that to

play09:58

[Music]

play10:02

1.5

play10:04

and the other float is going to be

play10:05

called smoothing

play10:07

and we're going to set that to 10

play10:12

and lastly we need a variable to

play10:14

actually rotate our player

play10:15

and we're going to calculate that by

play10:17

adding our rotation onto

play10:20

a variable called current look position

play10:23

so let's lay out our script a little bit

play10:25

in our update we're going to need a few

play10:27

functions to call

play10:28

get input we're going to modify input

play10:31

and rotate player then we'll go below

play10:33

the update and we're going to create

play10:34

these

play10:35

so firstly we're going to store our

play10:37

input in x mouse position

play10:39

and we're going to use another get axis

play10:41

raw because we want to apply our own

play10:43

smoothing

play10:44

and this time we're going to use our

play10:46

mouse x

play10:48

axis

play10:50

[Music]

play10:52

so now we go straight to modifying our

play10:54

input

play10:55

first thing we're going to do is we're

play10:56

going to add sensitivity

play10:58

to our new input we're going to do that

play11:02

simply by saying

play11:03

times equals sensitivity and we also

play11:06

want smoothing to affect this

play11:11

so next we want our main smoothing to be

play11:13

applied

play11:14

so we're going to say smooth mouth

play11:16

position and we're going to use

play11:18

a lerp which just literally interpolates

play11:21

one value to another

play11:23

for our a value we're going to use

play11:25

smooth mouse position

play11:28

our b value we'll use x mouse position

play11:31

and then for the time we use smoothing

play11:34

but since this takes a value

play11:35

between 0 and 1 we're just gonna put one

play11:38

over smoothing we've modified our input

play11:41

now it's time to actually rotate our

play11:43

player

play11:43

here's where we need to use our current

play11:45

looking position to store our rotation

play11:48

and then every frame we're going to add

play11:50

on to it our smooth mouse position so

play11:53

we'll say

play11:54

plus equals now it's time for our actual

play11:57

rotation

play11:57

we're going to say transform dot local

play12:00

rotation because we want this rotate

play12:02

in local space then we're going to set

play12:05

this equal to a quaternion and we're

play12:07

going to use this handy function of

play12:09

quaternion

play12:10

called angle axis it's going to rotate

play12:13

us around a certain axis current looking

play12:16

position

play12:17

as our rotation and then the axis that

play12:19

we want to rotate around

play12:21

is the transform dot up or our y axis

play12:24

so it's almost time to save and test but

play12:26

before we do that we need to lock and

play12:28

remove our cursors from the screen

play12:30

so let's resurrect the start method that

play12:32

i killed earlier

play12:34

and inside here we're going to lock

play12:35

these cursors and it's pretty simple

play12:38

the first thing we need to do is say

play12:40

cursor dot lock state

play12:43

and we're going to use cursor lock mode

play12:45

dot

play12:46

locked

play12:50

and then we need to actually hide the

play12:51

cursor by saying

play12:53

cursor.visible equals false

play12:58

back in unity i know the smoothing is

play12:59

set to 10. let's change that in the

play13:02

inspector to 1.5 and also go into the

play13:04

script and change it

play13:06

here now we can quickly save and go back

play13:09

in play mode you can see now we have a

play13:12

decent character controller

play13:13

walking and moving around our scene one

play13:16

final thing we can do to spice this up

play13:18

is to add head bop the way our players

play13:21

set up

play13:22

is the camera is inside the player so

play13:25

the player moves and the camera follows

play13:30

we can add animation to the camera that

play13:32

animation will take place within the

play13:34

parent

play13:35

to begin we're gonna need two windows

play13:37

we're gonna need

play13:39

a animation window

play13:48

i like to dock it down by the console

play13:51

and then we also need an animator window

play13:54

so after we have those open we can go to

play13:57

our main camera

play13:58

and we're going to go down and create a

play14:01

new animation

play14:03

we're going to need two animations the

play14:05

first animation is just going to be

play14:06

static

play14:07

so we're going to call this cam idle and

play14:10

we only need one keyframe

play14:12

so i'm just going to hit the record

play14:14

button and since we're animating the

play14:16

y-axis we'll change it

play14:18

and all you simply need to do is delete

play14:20

the value

play14:21

and re-paste it so we can stop the

play14:24

recording

play14:25

we'll go here and we're going to create

play14:28

the next animation

play14:29

and it's going to be called cam walk

play14:32

we'll hit the record button once again

play14:35

and we're going to go about

play14:36

half a second and we're going to move

play14:39

the camera

play14:40

up above the player this creates a

play14:42

keyframe at half a second but also one

play14:45

at the beginning so we'll go down here

play14:47

and highlight these keyframes hit ctrl c

play14:49

to copy

play14:50

and then we'll go over to about one and

play14:52

paste to make this a loop

play14:55

so before we stop recording we can check

play14:58

a preview

play14:59

so we hit this little play button and

play15:00

it'll give us a preview

play15:02

and we can see that it's a little too

play15:03

slow

play15:06

half a sec it's a little too fast so

play15:08

we'll go three quarters of a second

play15:11

and then we can stop the recording and

play15:13

stop the preview

play15:22

now we go back to the root of our player

play15:24

and open our

play15:26

player move script once more now we need

play15:29

another variable

play15:30

and this one's going to be a animator

play15:36

and we'll just call this cam anim

play15:40

and we'll also make it public so we can

play15:42

assign it in the inspector

play15:48

so now we need a boolean so this script

play15:52

can keep track of whether we're moving

play15:58

we're going to call this boolean is

play16:06

walking

play16:09

figure this out we're going to check for

play16:12

head bob

play16:13

we're going to do that at the very

play16:15

bottom of our update

play16:16

so we can go below our move player and

play16:18

create this function

play16:22

and the character controller in unity

play16:23

has a

play16:25

default variable called velocity

play16:28

we'll use that velocity to determine

play16:30

whether our player is actually moving or

play16:32

not

play16:33

so we'll say my cc dot velocity

play16:36

and then we're going to get the

play16:37

magnitude or the length of that vector

play16:40

then we can just wrap this up in an if

play16:42

statement and we're basically just going

play16:44

to say

play16:45

if our magnitude is greater than a very

play16:48

small number

play16:50

[Music]

play16:51

then we're walking

play16:54

[Music]

play16:59

and if we're not

play17:02

then that means we must be standing

play17:04

still

play17:09

so now that our script has a bull that

play17:11

tells us whether or not we're walking

play17:12

let's use that to get our animator to

play17:15

play the animation that we want

play17:20

we're going to say canada.set bool

play17:28

and the first one is a string for the

play17:31

boolean of the animator

play17:32

we haven't made that yet but we're going

play17:34

to call it is walking

play17:35

and the second parameter is just a true

play17:37

or false

play17:38

and we're going to pass in our bull that

play17:41

we just created here

play17:42

to set the animator's bull now we say we

play17:45

go back to unity

play17:46

so now we need to go to the main camera

play17:48

or whatever object that our animator is

play17:50

on

play17:51

we need to select the animator window

play17:54

we're going to tidy this up a bit

play17:58

and now the top left we need to go and

play18:00

make a parameter

play18:02

and this is the bull that we just

play18:04

referenced in our script

play18:06

and when you make these remember that

play18:07

the spelling must be exact

play18:09

to the string that you referenced let's

play18:11

make a transition from our default

play18:13

animation by

play18:14

right clicking over to cam walk we're

play18:16

going to select that transition

play18:18

we're going to untick exit time and down

play18:22

in conditions we're going to

play18:23

add our boolean set this to true so

play18:27

when we are walking our cam walk will

play18:29

play

play18:30

now we can do the same and make a

play18:31

transition back to cam

play18:33

idle as exit time is unticked

play18:37

and this transition will occur when is

play18:39

walking is false

play18:41

in our final step we'll go into our

play18:42

player and drag our main camera

play18:44

into the new cam anim field

play18:48

so now we can hit ctrl s to save or hit

play18:51

play

play18:52

and we can see our finished product

play18:56

and that was it on our first episode i

play18:58

hope you join us on the next ones

play19:00

and help me build this into a fleshed

play19:01

out doom clone spawn camp

play19:03

out

Rate This

5.0 / 5 (0 votes)

Related Tags
Unity教程游戏开发Doom克隆玩家控制环境设置材质编辑脚本编程动画制作摄像机控制头动效果
Do you need a summary in English?