Unity暑期萌新入門:巡邏敵人篇

作者:Truly
上一篇中我們設(shè)置了不會動的石像鬼,這一篇我們將制作在關(guān)卡中巡邏的幽靈,并且在關(guān)卡中投放一定數(shù)量的敵人。幽靈比石像鬼多了一個巡邏的功能,我們結(jié)合之前在關(guān)卡中設(shè)置的Nav Mesh來實(shí)現(xiàn)這個功能。

一、制作Ghost預(yù)制體(推薦參照GIF或圖片快速操作)
1.新建Ghost預(yù)制體
在Project > Assets > Models > Characters文件夾里找到Ghost的模型,拖動到Hierarchy窗口創(chuàng)建Ghost GameObject,再把Ghost GameObject拖動到Project > Assets > Prefabs文件夾中創(chuàng)建預(yù)制體。

2.設(shè)置動畫
(1)在Assets > Animation > Animators文件夾中新建Animator Controller ,命名為Ghost。

(2)雙擊Ghost(Animator Controller)打開Animator視窗,打開Assets > Animation > Animations文件夾,把Ghost@Walk拖到Animator視窗里。

(3)把設(shè)置好的Ghost(Animator Controller)分配到Ghost?預(yù)制體的Animator中。

3.添加并設(shè)置Collider
(1)進(jìn)入編輯預(yù)制體模式,給Ghost 添加Capsule Collider組件。
(2)設(shè)置其屬性:
Center: (?0,?0.6,?0)
Radius:?0.25
Height:?1.2

4.添加并設(shè)置Rigidbody
因?yàn)镚host幽靈移動的時候不能被John撞離路線,所以我們要添加Rigidbody,并且勾選IsKinematic,使Ghost不受外力影響。

5.添加并設(shè)置視野模擬
在Gargoyle(石像鬼)中我們用PointOfView?GameObject模擬范圍檢測,我們可以把它做成Prefab(預(yù)制體),應(yīng)用到Ghost(幽靈)中。
(1)打開Assets> Prefabs?文件夾并雙擊Gargoyle?Prefab,打開預(yù)制體。
(2)把Hierarchy窗口中的PointOfView?GameObject拖進(jìn)Assets > Prefabs?文件夾,生成PointOfView?Prefabs(預(yù)制體)。
(3)雙擊Ghost Prefab打開預(yù)制體,把PointOfView?Prefabs拖到Ghost 上,使其添加為Ghost的子對象。

(4)在Hierarchy窗口中,選擇PointOfView?,修改其Transform:
Position:(0?,0.75?,0.4?)
Rotation:(0?,0?,0?)

6.添加并設(shè)置Nav Mesh Agent組件
(1)要使Ghost在關(guān)卡中移動,需要兩個步驟:
①給Ghost添加Nav Mesh Agent,使Ghost可以使用NavMesh進(jìn)行導(dǎo)航。
②通過腳本控制Nav Mesh Agent。
(2)在Inspector窗口中添加Nav Mesh Agent組件,并根據(jù)如下設(shè)置:
Speed(移動速度):0.25
Stopping Distance(停止距離):0.2,與目標(biāo)點(diǎn)的距離小于該值就會停止。
Radius(半徑):0.25,模擬Ghost的半徑,用于判斷Ghost是否有足夠的空間在路徑上行走。

Ghost預(yù)制體已經(jīng)完成Nav Mesh Agent組件的設(shè)置,接下來進(jìn)行腳本的編寫,設(shè)置目標(biāo)點(diǎn)。
二、腳本編寫
游戲中Ghost(幽靈)會根據(jù)一系列目標(biāo)點(diǎn)來進(jìn)行巡邏。
1.新建腳本
(1)在Asset > Scripts文件夾里新建腳本,命名為WaypointPatrol。
(2)把WaypointPatrol腳本拖到Ghost的Inspector窗口中,使其添加為Ghost的組件。

2.思路
(1)在Scene視窗中設(shè)置一系列目標(biāo)點(diǎn),然后傳(拖)入腳本。
(2)Ghost順著各自的目標(biāo)點(diǎn)進(jìn)行移動。
(3)根據(jù)Ghost與目標(biāo)點(diǎn)的距離,更新當(dāng)前目標(biāo)點(diǎn),達(dá)到巡邏的效果。
3.腳本
(1)使用Nav Mesh Agent時要添加命名空間using UnityEngine.AI;
(2)我們使用數(shù)組來儲存一系列目標(biāo)點(diǎn),暫時可以理解為用一個容器把這些目標(biāo)點(diǎn)“裝”起來,這些目標(biāo)點(diǎn)都會被賦予一個下標(biāo)(編號),下標(biāo)從0開始遞增。我們將通過更換下標(biāo)的方式來更新當(dāng)前目標(biāo)點(diǎn)。
通過以下公式計算更換當(dāng)前點(diǎn)的下標(biāo),下標(biāo)會從起點(diǎn)(0)到終點(diǎn)一直循環(huán):
m_CurrentWaypointIndex = (m_CurrentWaypointIndex + 1) % waypoints.Length;
m_CurrentWaypointInde:當(dāng)前目標(biāo)點(diǎn)下標(biāo);
waypoints.Length:數(shù)組長度(元素個數(shù));
%:取余符號。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
?
public class WaypointPatrol : MonoBehaviour
{
??? public NavMeshAgent navMeshAgent;?? //每個幽靈對應(yīng)的navMeshAgent組件
??? public Transform[] waypoints;?????? //存儲一系列目標(biāo)點(diǎn)的數(shù)組(目標(biāo)點(diǎn)從外部拖入)
?
??? int m_CurrentWaypointIndex;???????? //當(dāng)前目標(biāo)點(diǎn)的下標(biāo)
?
??? void Start()
??? {
??????? navMeshAgent.SetDestination(waypoints[0].position); //設(shè)置第一個目標(biāo)點(diǎn)(下標(biāo)為0)
??? }
?
??? void Update()
??? {
??????? //如果離目標(biāo)點(diǎn)剩余的距離小于設(shè)定的停止距離
??????? if (navMeshAgent.remainingDistance < navMeshAgent.stoppingDistance)????????????
??????? {
??????????? //計算下一個目標(biāo)點(diǎn)。(當(dāng)前下標(biāo)+1)與數(shù)組長度取余數(shù)
??????????? m_CurrentWaypointIndex = (m_CurrentWaypointIndex + 1) % waypoints.Length;??
???????? ???navMeshAgent.SetDestination(waypoints[m_CurrentWaypointIndex].position);??? //更換目標(biāo)點(diǎn)
??????? }
?
???????
??? }??
}
?
4.給Waypoint Patrol腳本分配引用(編輯預(yù)制體模式)
回到Ghost預(yù)制體編輯窗口,選中Ghost GameObject,把Nav Mesh Agent組件拖到Waypoint Patrol腳本的Nav Mesh Agent字段中,保存預(yù)制體。

5.給Observer腳本分配引用(普通模式)
退出編輯預(yù)制體模式,在Hierarchy窗口里選中Ghost的子對象PointOfView?:
把JohnLemon GameObject拖進(jìn)Observer腳本的Player字段;
把GameEnding GameObject拖進(jìn)Observer腳本的GameEnding字段。

三、布置幽靈及石像鬼
我們已經(jīng)制作好一只Ghost和一只Gargoyle,接下來就是復(fù)制怪物,把他們布置在關(guān)卡中。
(1)布置Ghost
①在Hierarchy窗口里選中Ghost,Ctrl+D復(fù)制3只Ghost。
②修改每只Ghost 的Transform組件的Positon:
Gost Postion: (?-5.3,?0,?-3.1)
Gost(1)?Postion: (?1.5,?0,?4)
Gost(2)?Postion: (?3.2,?0,?6.5)
Gost(3)?Postion: (?7.4,?0,?-3)
(2)設(shè)置路線目標(biāo)點(diǎn)
我們用一些空的GameObject來制作路線的目標(biāo)點(diǎn)。
①在Hierarchy窗口中右鍵 > Create Empty,重命名為Waypoint.;
②Ctrl+D再復(fù)制出9個Waypoint,下邊給每只幽靈分配并設(shè)置各自的巡邏目標(biāo)點(diǎn);
③把Waypoint和Waypoint(1)拖進(jìn)Ghost 的Waypoint Patrol腳本并修改Positon;
Waypoint的Positon: (?-5.3,?0,?6.7)。
Waypoint(1)的Positon:(?-5.5,?0,?-4.5)。

④把Waypoint(2)和Waypoint(3)拖進(jìn)Ghost(1)的Waypoint Patrol腳本并修改Positon;
Waypoint(2)的Positon: (?1.2,?0,?7.7?)。
Waypoint(3)的Positon: (?0.9,?0,?-3.5?)。
⑤把Waypoint(4)、Waypoint(5)、Waypoint(6)和Waypoint(7)拖進(jìn)Ghost(2)的Waypoint Patrol腳本并修改Positon;
Waypoint(4)的Positon: (?3.2,0,5.6?)。
Waypoint(5)的Positon: (?3.2,0,12.3?)。
Waypoint(6)的Positon: (?6.5,0,12.3?)。
Waypoint(7)的Positon: (?6.5,0,5.6?)。
⑥把Waypoint(8)和Waypoint(9)拖進(jìn)Ghost(3)的Waypoint Patrol腳本并修改Positon;
Waypoint(8)的Positon: (?3.2,0,-5?)。
Waypoint(9)的Positon: (?7.4,0,-2?)。
(3)布置Gargoyle
①再復(fù)制2只Gargoyle。
②修改Gargoyle (1)的Transform:
Positon:(-2.6,0,-8.5);
Rotation:(0, 30,0)。
③修改Gargoyle (2)的Transform:
Positon:(-4.8, 0, 10.6)。
四、整理Hierarchy
經(jīng)過一輪的設(shè)置之后,我們的Hierarchy窗口顯得比較凌亂,我們可以對它們進(jìn)行分類,并且用新建相應(yīng)的空GameObject作為父節(jié)點(diǎn)把它們“裝”起來。
(1)新建一個空的GameObject,名為Enemies,Positon設(shè)為(0,0,0),把所有Ghost和Gargoyle拖進(jìn)去;
(2)新建一個空的GameObject,名為Waypoints,Positon設(shè)為(0,0,0),把所有Waypoint拖進(jìn)去。

設(shè)置完成后記得保存場景哦。
結(jié)語:本篇我們制作了可以巡邏的幽靈,并且在關(guān)卡中布置了數(shù)只怪物,游戲的大部分設(shè)置算是都基本完成了,接下來最后一章我們將為游戲添加音效,進(jìn)一步烘托恐怖的氣氛。
迫不及待想自行開始制作的小伙伴,可以瀏覽John Lemon's Haunted Jaunt官方教程:https://learn.unity.com/project/john-lemon-s-haunted-jaunt-3d-beginner
咱們的游戲開發(fā)交流群也歡迎強(qiáng)勢插入:869551769
希望參與線下游戲開發(fā)學(xué)習(xí)的,歡~~~~~~迎訪問:http://levelpp.com/