How to make a DOOM CLONE in Unity || E1M3 Enemy AI and NavMesh Part 1

SpawnCampGames
7 Dec 202016:38

Summary

TLDR本视频脚本介绍了在Unity中为游戏敌人添加视觉效果和行为反应的步骤。首先创建了击中效果的粒子系统,并将其设置为不循环播放,持续时间为半秒。接着,为敌人添加了音频效果,并在玩家射击时触发。然后,通过改变敌人材质来表示其警觉状态,并使用触发器和脚本来检测玩家的接近。最后,设置了基本的AI行为,使敌人在意识到玩家后能够追逐玩家。这个过程包括了设置导航网格、调整敌人的NavMesh Agent组件,并编写脚本来控制敌人的移动。视频还提到了在开发过程中遇到的一些技术问题以及解决方法。

Takeaways

  • 🎮 在游戏中添加击中效果:创建新的粒子效果,设置持续时间为半秒,不循环,初始速度为10,颜色为深红色,使用重力1.5,模拟空间为世界空间,并通过触发器发射15个粒子。
  • 🛠️ 将击中效果转换为预制体:创建预制体文件夹,将击中效果拖入文件夹,然后从层级结构中删除击中效果实例。
  • 👾 敌人控制击中效果的生成:在敌人脚本中添加公共变量,用于生成击中效果,并在受到伤害时实例化该效果。
  • 🔊 为枪添加临时音效:创建音频文件夹,添加枪声效果,调整音量,并在枪脚本中播放和停止音效。
  • 🎨 敌人感知玩家时改变材质:创建新材质,添加触发器组件,当玩家进入触发器时,改变敌人的材质以显示其已经感知到玩家。
  • 🔍 通过触发器和标签检测玩家:使用`CompareTag`方法和玩家标签来检测玩家是否进入敌人的触发区域。
  • 🔧 玩家射击时使敌人感知:使用`OverlapSphere`方法检测射击范围内的敌人,并设置他们的感知状态。
  • 🏃 设置导航网格代理:为敌人添加导航网格代理组件,设置速度,并在导航窗口中烘焙导航网格。
  • 🤖 创建基础AI脚本:编写脚本使敌人在感知玩家后追逐玩家,使用`SetDestination`方法移动敌人至玩家位置。
  • 🛑 解决敌人检测问题:修复由于敌人之间距离过近导致的检测错误,通过距离检查而非触发器来检测玩家。
  • 🔄 持续迭代和改进:脚本和效果是临时的,将在后续进行重构和改进,以增强游戏的AI和测试体验。

Q & A

  • 如何在游戏中为敌人添加命中效果?

    -首先创建一个新的粒子效果,设置其持续时间为0.5秒,不循环播放。设置起始速度为10,起始颜色为深红色,并添加重力1.5。在发射器设置中,使用时间间隔为0的发射率,改为添加15个粒子的爆发。然后将这个粒子效果转为预制体,并放入预制体文件夹中。

  • 为什么需要在敌人脚本中添加枪击效果?

    -在敌人脚本中添加枪击效果是为了当敌人受到伤害时,能够在游戏中显示相应的视觉效果,增强玩家的游戏体验。

  • 如何在游戏中实现枪声效果?

    -首先创建一个新的音频文件夹并添加枪声效果文件。然后在玩家控制的角色上添加一个音频源组件,并设置音频剪辑为枪声效果。在枪的脚本中,使用GetComponent<AudioSource>()来获取音频源组件,并在射击方法中调用Play()函数播放枪声。

  • 为什么需要在敌人模型上添加敌意材料?

    -添加敌意材料是为了在敌人意识到玩家存在时,通过改变颜色来给玩家一个视觉提示,表明敌人已经进入警戒状态。

  • 如何使敌人在玩家进入触发器时改变材料?

    -在敌人脚本中使用OnTriggerEnter方法检测玩家是否进入触发器。如果是玩家,则获取敌人的MeshRenderer组件,并将其material属性设置为敌意材料。

  • 为什么需要为敌人添加一个NavMesh Agent组件?

    -NavMesh Agent组件负责在导航网格上导航,允许敌人根据玩家的位置移动,这是实现AI行为的基础。

  • 如何设置敌人的导航网格?

    -首先需要将地面和可行走的物体标记为Navigation Static,然后在Navigation窗口中进行烘焙,生成导航网格供敌人使用。

  • 如何编写敌人AI脚本来追逐玩家?

    -创建一个敌人AI脚本,在Start方法中初始化敌人意识和玩家位置的引用。在Update方法中,如果敌人意识到玩家,则使用NavMesh Agent的setDestination方法将目的地设置为玩家的位置。

  • 为什么需要修改敌人检测玩家的方法?

    -由于敌人之间的重叠和射线检测的问题,需要修改敌人检测玩家的方法以避免错误。可以通过距离检测和设置感知半径来实现。

  • 如何在游戏中测试敌人的AI行为?

    -在Unity编辑器中设置好所有脚本和组件后,可以通过播放测试来观察敌人是否能够根据玩家的位置和行为做出相应的反应,例如改变材料或追逐玩家。

  • 为什么在测试中会出现控制台错误?

    -控制台错误可能是因为射线检测时敌人的位置不正确导致的空引用错误。需要检查敌人的位置和射线检测的逻辑,确保敌人在正确的位置被检测到。

  • 如何修复敌人之间的重叠问题?

    -可以通过调整敌人的位置或大小,确保它们的碰撞体不会重叠。此外,也可以修改敌人检测玩家的逻辑,避免因为重叠而导致的错误。

Outlines

00:00

🎯 添加击中效果

我们从添加一个新的粒子效果开始,为敌人击中时添加视觉提示。创建一个新的粒子效果,命名为gun hit,设置持续时间为0.5秒,不循环,使用深红色。添加重力并设置为1.5。然后,在敌人的脚本中添加一个公用GameObject变量,将粒子效果拖入Inspector中。当敌人受到伤害时实例化该粒子效果。尽管这不是最优方法,但目前可以使用。之后,我们还将为枪添加音效。创建一个音频文件夹,将音效拖入,添加Audio Source组件,并在fire方法中播放音效。

05:02

🔊 增强敌人AI的感知能力

接着我们增强敌人的AI,使其能感知玩家。创建一个新的材质,并在敌人感知玩家时改变材质颜色。编写一个新的脚本Enemy Awareness,定义材质变量,并在OnTriggerEnter方法中检查是否为玩家,如果是则更改材质。然后,添加一个球形触发器,使敌人在玩家进入时变得警觉。接着在枪的脚本中,使用Physics.OverlapSphere方法模拟枪声半径,并使该范围内的敌人变得警觉。

10:03

🚶 设置敌人导航

设置敌人的导航行为。首先配置导航网格,使地面和大立方体可行走。然后在敌人上添加NavMesh Agent组件,调整速度。在Navigation窗口中烘焙导航网格,使NavMesh Agent可以在蓝色区域内导航。编写脚本使敌人追踪玩家,在start方法中获取Enemy Awareness组件和玩家的Transform。在Update方法中,若敌人感知到玩家,则设置目标位置为玩家位置。

15:05

📏 重新配置玩家检测方法

修正玩家检测方法以避免重叠的敌人检测错误。移除敌人的球形触发器,使用距离检查替代。在Enemy Awareness脚本中,定义一个玩家Transform变量,并在Start方法中初始化。使用Vector3.Distance方法计算敌人和玩家之间的距离,如果距离小于感知半径,则设置敌人为警觉状态。删除原有的OnTrigger方法并测试新方法。最终确认所有系统协同工作良好。

Mindmap

Keywords

💡粒子效果

粒子效果是一种在视频游戏中用来增强视觉效果的技术,它通过创建和控制大量小粒子来模拟如火焰、烟雾、爆炸等自然现象。在视频中,制作者提到创建一个新的粒子效果来模拟枪击时的视觉效果,命名为'gun hit',这是为了让玩家对游戏中的打击反馈有更直观的感受。

💡Prefab

Prefab是Unity中预制件的意思,它允许开发者将游戏对象和其组件保存为模板,以便在游戏的不同部分重复使用。在脚本中,制作者将创建的粒子效果'gun hit'转换为Prefab,并将其放入Prefab文件夹中,这样做可以方便地在游戏中多次使用同一效果。

💡脚本编程

脚本编程是游戏开发中的一个重要环节,它允许开发者通过编写代码来控制游戏对象的行为。视频中提到了多个脚本的编写,如'enemy script'和'enemy awareness',这些脚本定义了敌人的行为,比如在受到攻击时播放粒子效果和声音,以及如何响应玩家的接近。

💡触发器(Trigger)

在Unity中,触发器是一种特殊的Collider,它用于检测游戏对象是否进入或离开某个区域,而不会对游戏对象产生物理影响。视频中,敌人使用触发器来检测玩家是否进入其范围,并在玩家接近时改变材料,表示敌人已经注意到了玩家。

💡材质(Material)

在Unity中,材质是定义游戏对象外观的属性集合,包括颜色、纹理和光泽等。视频中,制作者创建了一个新的材质'enemy aggro'来表示敌人进入警觉状态,通过改变敌人的颜色来给玩家视觉上的反馈。

💡音频效果

音频效果是游戏中用来增强玩家沉浸感的声音元素,如枪声、环境声等。制作者在视频中提到为枪添加了音频效果,使用了'doom gun sound'来模拟枪击的声音,并通过脚本控制播放,以增强射击的真实感。

💡导航网格(Navmesh)

导航网格是Unity中用于AI路径寻找的一种技术,它允许AI角色在游戏世界中智能地移动。视频中,制作者设置了导航网格,使敌人能够在玩家射击后,通过Navmesh Agent组件,智能地沿着网格追逐玩家。

💡AI行为

AI行为指的是游戏中非玩家角色(NPC)的行为模式,如追逐、逃避、攻击等。视频中,制作者通过编写'enemy AI'脚本来实现敌人的基本行为,如在意识到玩家后追逐玩家,这是游戏AI设计中的核心部分。

💡射线检测(Raycasting)

射线检测是一种在Unity中常用的技术,通过发射一条射线来检测它是否与场景中的其他对象相交。在视频中,制作者使用射线检测来实现枪击效果,通过检测射线与敌人的交点来确定是否击中目标。

💡层级(Layer)

在Unity中,层级是指游戏对象的组织结构,允许开发者将对象分组并设置特定的属性。视频中提到了创建一个新的层'enemy',这样在射线检测和导航网格中可以专门针对这一层的对象进行操作,提高了游戏开发的效率和准确性。

Highlights

创建新的粒子效果来增强敌人受击时的视觉效果

设置粒子效果的持续时间、循环和生命周期

调整粒子效果的起始速度和颜色以符合游戏风格

使用重力和世界空间模拟粒子效果

创建预制体来简化游戏对象的复用

编写脚本控制敌人在受击时生成粒子效果

为枪械添加临时的音效效果

使用AudioSource组件来播放枪声

在脚本中控制音频的播放和停止

通过改变敌人材质来表示其警觉状态

创建敌人警觉性的新脚本和方法

使用触发器和标签来检测玩家接近

调整敌人AI使其能够追踪并追逐玩家

设置NavMesh以实现敌人的路径导航

编写AI脚本来控制敌人在警觉状态下追逐玩家

解决脚本中的空引用错误并优化敌人检测

使用距离检查代替触发器来检测玩家

完成基本AI并计划在未来进行扩展

Transcripts

play00:02

we left off with this bland enemy it

play00:04

takes damage and dies but there's little

play00:06

indication

play00:07

so let's fix that before we move forward

play00:08

we're going to start by adding a hit

play00:10

effect

play00:11

so we'll create a new particle effect

play00:13

we're going to zero out its position

play00:14

and we'll name this something like gun

play00:16

hit we'll make the duration like half a

play00:19

second

play00:21

we'll untick looping

play00:24

the lifetime will also be half a second

play00:27

for start speed we'll go with 10

play00:32

for start color we'll go with the dark

play00:34

red

play00:37

we want gravity so we'll use 1.5

play00:40

simulation space is world then we'll

play00:43

expand emission

play00:45

we'll set its rate over time to zero and

play00:48

instead we'll add a burst

play00:50

of 15 particles or so

play00:58

that all looks good we're going to turn

play00:59

this into a prefab but first we'll make

play01:01

a folder for a prefabs

play01:04

then we'll just drag this gun hit inside

play01:06

the prefab folder

play01:08

and then we can go back to our hierarchy

play01:09

and delete the gun hit so for now our

play01:11

enemy is going to be in charge of

play01:13

spawning the gun hit particle so we'll

play01:15

go into our enemy script

play01:17

and at the top we'll say public game

play01:18

object

play01:21

gun hit effect

play01:28

and we're going to drag this in the

play01:29

inspector onto our enemy script

play01:34

and then down in here where we take

play01:35

damage

play01:38

we can say instantiate and this is going

play01:40

to spawn it and what we want

play01:41

spawn is our gun hit effect and we want

play01:43

it to spawn at our

play01:45

transform position

play01:48

and quaternion.identity is just a fancy

play01:51

way of saying no rotation

play01:54

this is not a good method but it's going

play01:55

to work for now you can see our hit

play01:57

effects keep piling up in the hierarchy

play01:59

and we'll be using a pulling system

play02:00

later but for now this is fine

play02:02

now we want to add audio to the gun and

play02:04

just like the hit effect this is only

play02:05

temporary and we'll be doing this a

play02:07

better way later

play02:08

so first we're going to make a new audio

play02:10

folder and i found the actual doom gun

play02:12

sound effect but any gun sound effect

play02:14

will work so just drag it in

play02:18

and then we'll go to our player

play02:22

and then the gun and we'll add a new

play02:24

component and this component is going to

play02:26

be an

play02:26

audio source once that's up here on the

play02:28

audio clip we're going to click this

play02:30

circle and we're going to select our

play02:32

doom gun sound

play02:33

so the only thing we need to do this is

play02:35

untick

play02:36

play on awake and reduce the audio

play02:38

volume a bit

play02:40

i usually use 0.1 to start

play02:44

so we're going to go back into our gun

play02:46

script and inside the fire method is

play02:48

where we're going to play this audio

play02:50

[Music]

play02:53

and we're going to be lazy we're not

play02:54

even going to cache these references

play02:55

we're going to use

play02:56

git components and we need a git

play02:59

component of audio source

play03:03

and we'll call the play function

play03:08

and then right above this we're going to

play03:10

call the stop function just to make sure

play03:12

audios don't overlap

play03:13

so now that we have these makeshift

play03:15

effects it should make building out the

play03:16

ai and testing more enjoyable

play03:18

and don't worry if you chose to skip

play03:19

this for now because we're going to be

play03:21

reworking all this and the code still

play03:22

works the same

play03:24

we're going to start the enemy ai simple

play03:25

by making it aware of the player

play03:27

and since we just added some

play03:28

visualizations we'll continue with that

play03:30

tradition

play03:31

and we're gonna make the enemy swap out

play03:32

its materials when it becomes aware

play03:34

so to do that first we'll start by

play03:35

making a new material we'll just

play03:36

duplicate the enemy material

play03:38

we'll call this something like enemy

play03:40

aggro we'll just change the color of it

play03:42

so we can see it changing once we've

play03:46

created a new material we can just go to

play03:47

our enemy and we're going to add a new

play03:49

component

play03:51

and it's going to be a new script called

play03:53

enemy awareness

play03:59

so we'll just double click that to open

play04:01

it and then we'll clean it up

play04:03

and up at the top we'll define that

play04:04

material we'll call it public material

play04:09

and we'll call it aggro matte for aggro

play04:12

material

play04:15

then we'll need a ontriggerenter method

play04:19

so this will return a collider called

play04:21

other and then inside here we're just

play04:23

going to check

play04:23

if other dot transform dot compare tag

play04:27

and this takes in a string and we're

play04:29

just gonna call it player

play04:33

and then if it is the player we want to

play04:35

get its mesh renderer

play04:41

and then we're going to access the dot

play04:43

material property

play04:44

and we're going to set that material to

play04:45

our aggro material

play04:47

now we can save and head into unity and

play04:49

then on the enemy script

play04:51

we can just drag in our new material

play04:54

so we're using an on-trigger enter so

play04:56

our enemy also needs a trigger

play04:58

we're going to use a sphere collider for

play04:59

our trigger and we'll do that by

play05:01

checking that is triggered so whenever a

play05:05

player enters this trigger the enemy is

play05:06

going to become alert

play05:08

so something around eight for a radius

play05:10

seems fine for now

play05:12

so we'll collapse this for now and then

play05:14

we're going to drag it up here below the

play05:15

rigid body with the other components

play05:17

and then one last thing we need to go to

play05:19

our player and make sure that the tag

play05:22

is tagged as player because that's what

play05:24

we're referencing in our enemy awareness

play05:26

script and if we save and test

play05:27

our enemies should change colors when we

play05:29

get too close

play05:30

and it does so let's add a little bit

play05:32

more functionality to this

play05:36

so we'll go back into our enemy

play05:38

awareness script and we still wanted to

play05:40

change material so we're just going to

play05:41

cut this real quick

play05:43

and instead we're going to say is aggro

play05:45

equals true

play05:46

so up at the top we'll define this bull

play05:51

and it's going to be a public bull

play05:53

because other scripts are going to need

play05:54

reference to this

play05:59

and then we're going to make an update

play06:00

loop and then

play06:02

instead of just setting our material

play06:04

whenever the trigger gets triggered

play06:06

we have this bull being set instead so

play06:08

we'll just say

play06:09

if is aggro then we want to

play06:13

change our material and we'll just paste

play06:14

this line

play06:16

so it's still detecting the player like

play06:18

before but now there's a public bull

play06:19

that we can access

play06:21

or modify so now let's make the player's

play06:24

gunshot

play06:24

make the enemy aware so we have a player

play06:27

and we have our enemy

play06:28

the enemy knows where the player is

play06:30

because of a sphere clutter trigger

play06:32

so similar to how our gun can shoot a

play06:34

raycast and return what it hits

play06:36

it can also do something called a

play06:38

overlap sphere

play06:40

which will return an array of all the

play06:42

colliders inside the radius

play06:44

so if these are all enemy colliders then

play06:46

we can know that they have

play06:47

a enemy aware script we can loop through

play06:50

these and enable the public bull

play06:52

setting the enemies inside the radius to

play06:53

become aware or aggro

play06:55

so back in the gun script if you go down

play06:57

here in our fire method

play06:59

and look this for each statement here is

play07:01

where our enemy takes damage

play07:03

and possibly could get killed and that

play07:05

might cause some errors if we try to

play07:07

alert it after the fact

play07:08

so at the very beginning of our fire

play07:09

statement we're going to simulate the

play07:11

gunshot radius

play07:12

and alert any enemy in earshot

play07:16

so at the top i'll start by creating a

play07:18

float for the radius

play07:21

we'll call it gunshot radius now i'll

play07:24

default it to the same

play07:25

as range at 20 and then i'll fix these

play07:29

variables up a bit by setting the fire

play07:31

rate to 1 which it is in the inspector

play07:34

and we'll move it down with next time to

play07:36

fire then i'll move

play07:38

gunshot radius up with the other gun

play07:40

variables

play07:41

and the layer mask is fine and the box

play07:43

collider and enemy manager are both fine

play07:45

down there

play07:46

so

play07:51

so back to the fire method we want to

play07:53

use physics dot overlap sphere

play07:56

this is going to take a origin that

play07:57

we're going to use transform.position

play07:59

for

play08:02

it's going to take a radius that we're

play08:03

going to use gunshot radius for

play08:05

and then we're going to add a layer mass

play08:07

to make sure

play08:08

these are only enemies

play08:16

so we'll need to go to the top and we're

play08:18

going to create another layer mask like

play08:20

we did before

play08:21

only this time it's going to be called

play08:22

enemy layer mask

play08:24

okay so the physics overlap sphere takes

play08:26

a array of colliders so we're gonna

play08:30

say we want a clotter array and we're

play08:32

gonna call this

play08:33

enemy colliders

play08:38

and back here we'll say enemy colliders

play08:41

equals

play08:45

what we had already topped out

play08:49

so now that we got a collection of enemy

play08:51

colliders we can loop through each one

play08:52

and alert the ones that are inside the

play08:54

overlap sphere

play08:55

to do that we need a for each statement

play08:58

and for the

play08:59

var we're going to say enemy collider so

play09:02

the

play09:02

singular of our collider array

play09:07

in enemy colliders and this is our

play09:09

collider array

play09:10

and then we're going to say for each one

play09:13

we want to go

play09:14

enemy collider dot get component and we

play09:17

want the

play09:17

enemy awareness and then we made that

play09:20

public bull

play09:21

so we'll access it now and we'll say dot

play09:24

is aggro equals true

play09:25

we can save and now in unity we need to

play09:28

go to our enemy object

play09:30

and we're going to add a new layer we're

play09:32

going to call this layer enemy

play09:34

then we'll click off and then back on

play09:35

enemy and assign

play09:37

the layer now back on our gun script we

play09:40

have this

play09:40

enemy layer mask and we're going to

play09:42

select enemy

play09:44

and this also messed up our raycast

play09:46

layer mask before we were checking if it

play09:48

was

play09:48

anything but the gun so default worked

play09:50

for that but now we have a new enemy

play09:52

layer

play09:53

so we need default and

play09:56

enemy selected so that's the new

play09:58

everything

play09:59

butt gun now with those set up we should

play10:01

test to see that the enemy becomes aware

play10:03

when we shoot our gun

play10:04

oops i killed him so let's try it again

play10:06

so let's back out of his personal space

play10:08

and hit the button and yes he becomes

play10:10

aggro

play10:12

so now let's get it where it actually

play10:14

moves to get the enemy to do something

play10:16

we're going to need your basic nav mesh

play10:18

setup

play10:18

first we need to set up where to walk

play10:21

the ground seems like it should be

play10:22

walkable

play10:23

the cubes at least the big one can be

play10:25

walkable

play10:26

we can select the entire environment

play10:28

game object and at the top of the

play10:30

inspector you can tick this arrow next

play10:32

to static

play10:32

and we can define this as navigation

play10:35

static

play10:36

that'll get it all ready to be baked

play10:37

into a nav mesh next we'll go into our

play10:40

enemy and give this a navmesh agent

play10:42

component

play10:43

this is responsible for navigating the

play10:45

mesh

play10:47

it's got a few variables that you can

play10:49

edit here or in script

play10:51

and already know that i want to increase

play10:52

the speed a bit so i'm going to put

play10:54

perhaps 7 here now we can go to the

play10:57

navigation window

play10:59

and if you don't see this you can find

play11:00

it in window ai

play11:03

and navigation so you have this option

play11:06

to bake

play11:07

and also an agents tab detailing what

play11:10

it's baking for

play11:12

i could bake this and it'd be fine but

play11:14

it might take a while because the ground

play11:15

is so massive

play11:18

so i'm going to go to the ground and i'm

play11:20

going to decrease its scale

play11:22

by half and back in the navigation

play11:25

window i can

play11:26

bake now and you'll see this blue area

play11:28

up here

play11:29

and this is the area that the navamesh

play11:31

agents are allowed to traverse

play11:33

and this can only be seen when you have

play11:35

the navigation window open

play11:37

so let's close it and now that we have

play11:39

our basic nav mesh it'll give us

play11:40

something to build our ai on top of

play11:42

so now we'll just make a script to chase

play11:44

the player once it's aware

play11:46

so first i'm going to move this navmesh

play11:48

agent back up here with

play11:49

other components

play11:53

and then i'm going to add a new

play11:54

component and i'm going to call this

play11:57

enemy ai and then we'll double click it

play12:00

to open

play12:02

so i'm going to just clean this up and

play12:03

then at the top we're going to need a

play12:05

enemy awareness variable and it's going

play12:07

to be private because it's on this game

play12:10

object

play12:13

and then we're going to use a private

play12:15

transform for the players transform

play12:17

because that's what we're going to be

play12:18

moving toward

play12:22

let's make a start function so we can

play12:24

assign all these variables

play12:25

first off we can say enemy awareness

play12:28

equals get component

play12:32

enemy awareness and that's going to pull

play12:34

it straight off of this game object

play12:36

and then players transform is going to

play12:38

be a little different we're going to use

play12:40

find object of type and we're going to

play12:43

pick something that we know only the

play12:45

player has so we know that player move

play12:47

can only be found on the player so we'll

play12:49

set it to that

play12:50

and then we'll grab the transform from

play12:52

it so we know if the enemy is aware

play12:56

and we know how to find our player's

play12:57

position so now to set the enemy to

play12:59

go to the player we need to access its

play13:02

navamesh agent so at the top we'll just

play13:04

say

play13:04

we want another private navmesh agent

play13:07

and we want this called

play13:08

enemy nav mesh agent

play13:14

so in the start we can say enemy nav

play13:16

mesh agent equals

play13:19

get component of type navmesh agent

play13:26

now on the update since we have a

play13:27

reference to this we can call a function

play13:28

from it called

play13:29

set destination

play13:33

and we want to set the destination to

play13:35

the player's transform.position

play13:38

and we only want to do this if the enemy

play13:40

is aware

play13:41

so we're going to wrap this in an if

play13:42

statement with enemyawareness.aggro

play13:46

and if it is then we'll set the

play13:47

destination to players transform

play13:49

position

play13:53

and if it's not then we'll set it to our

play13:56

own position

play13:58

so now it's time to save and test and

play14:00

everything looks like it's going well

play14:02

until i get these console errors i did

play14:05

end up finding the problem and instead

play14:07

of fixing it

play14:08

off camera i figured i would do it now

play14:10

so i kept pointing at a line in my gun

play14:11

script where it checks for the direction

play14:13

from the player to the enemy

play14:15

i know that line of code's fine it's

play14:16

just that the enemy wasn't there so it

play14:18

was a null error

play14:19

i ended up checking my enemy manager and

play14:22

the list had way more enemies than what

play14:24

i was looking at

play14:25

so i started looking around the enemy

play14:26

again and i realized the problem

play14:28

the big old sphere collider that we use

play14:30

to detect the player

play14:31

is being hit with the ray that the

play14:33

enemies are so close together

play14:35

that they overlap so there's conditions

play14:37

where you're inside of a collider

play14:39

shooting at another collider

play14:41

so we're gonna have to fix this by

play14:43

changing the method that we detect our

play14:44

player

play14:45

first off we'll get rid of all the

play14:46

enemies but one and we'll remove the

play14:48

sphere collider

play14:55

so back in our enemy awareness script we

play14:58

know that our ontrigger function is not

play14:59

going to work anymore so we need another

play15:01

way to find our player

play15:03

we're going to do it like we did before

play15:04

and at the top we're going to create a

play15:06

new

play15:07

private transform called players

play15:10

transform

play15:13

and then our start function will say

play15:15

players transform

play15:16

equals find object of type and then once

play15:20

again we know that player move is on our

play15:21

player and then we'll get our transform

play15:23

off of it

play15:24

so we'll do this using a distance check

play15:27

so we'll create a

play15:28

var dist and we'll set this equal to

play15:31

vector3.distance

play15:32

and a is going to be transform position

play15:35

and our b

play15:36

is going to be our player's

play15:37

transform.position

play15:39

and now that we got the distance we can

play15:41

say if

play15:42

our dist is greater than

play15:47

and then we need a new float so at the

play15:48

top we'll say public

play15:50

float and this is going to be awareness

play15:52

radius

play15:57

then back down here we'll say if

play15:58

distance is less than awareness radius

play16:04

then we can say is aggro to true

play16:09

and go down here and delete the

play16:11

ontrigger method and just like that

play16:12

we've created another method to get our

play16:15

status

play16:16

so now we can save and test and see if

play16:18

this method is going to work but first

play16:19

let's default this awareness radius to

play16:21

eight

play16:22

and also make sure that it's set in the

play16:23

inspector so then we can play test

play16:26

and now it looks to me like all our

play16:28

systems are playing well together

play16:30

so this is it for the basic ai and we'll

play16:33

be doing more to it later

play16:34

so i hope to catch you in part two spawn

play16:36

camp out

Rate This

5.0 / 5 (0 votes)

関連タグ
游戏开发AI敌人视觉特效声音效果导航系统Unity3D敌人行为玩家交互脚本编程游戏测试
英語で要約が必要ですか?