3D遊戲編程與設計作業07
智能巡邏兵
遊戲設計要求
- 創建一個地圖和若干巡邏兵(使用動畫)
- 每個巡邏兵走一個3-5個邊的凸多邊形,位置數據是相對地址。即每次確定下一個目標位置,用自己當前位置爲原點計算
- 巡邏兵碰撞到障礙物,則會自動選下一個點爲目標
- 巡邏兵在設定範圍內感知到玩家 ,會自動追擊玩家
- 失去玩家目標後,繼續巡邏
- 計分:玩家每次甩掉一個巡邏兵計一分,與巡邏兵碰撞遊戲結束
程序設計
- 使用MVC架構(前面的實驗博客中已有詳細說明,此處不再贅述)
- 使用訂閱與發佈模式傳消息:
CoinCollide
和SoompiCollide
將“碰撞”消息傳遞給GameEventManager
和Controller
,由GameEventManager
和Controller
通知其他模型 - 工廠模式產生巡邏兵和物品
場景製作
地圖繪製
- 下載並導入資源
Wispy Sky
和Winter Zone Mini
- Skybox設置參考我之前的博客
- 創建地形Terrain:菜單欄GameObject->3D Object->Terrain
- 在Inspector面板上設置Terrain的Material爲
SnowAndRock_1
Animation&Animator
巡邏兵動畫製作TODO
- 下載並導入資源
ToonyTinyPeople
- 創建Animator:菜單欄Window->Animation->Animator
- 具體動畫製作可參考博客
- 我的Animator最終制作結果爲:
玩家動畫製作
- 下載並導入資源
ToonyTinyPeople
- 我的Animator最終制作結果爲:
金幣動畫製作
- 下載並導入資源
RPG Pack
- 可直接使用資源庫中提供的Animator
攝像機跟隨CameraFollow
-
創建
CameraFollow.cs
腳本:using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { public GameObject follow; //跟隨的物體 public float smothing = 5f; //相機跟隨的速度 Vector3 offset; //相機與物體相對偏移位置 void Start() { offset = transform.position - follow.transform.position; } void FixedUpdate() { Vector3 target = follow.transform.position + offset; //攝像機自身位置到目標位置平滑過渡 transform.position = Vector3.Lerp(transform.position, target, smothing * Time.deltaTime); } }
-
將該腳本掛載在主攝像機下
-
將
Main Camera
拖動賦值給Controller
的main_camera
屬性
玩家移動JoyStick
與之前HitUFO中的JoyStick
實現方式不同,這裏w
和s
鍵控制的移動改爲Z軸
上的移動,a
和d
鍵控制的移動則仍爲X軸
上的移動,另外爲了更好的用戶遊戲體驗,還加入了遊戲對象旋轉的功能——往哪個方向移動則遊戲對象正面朝向該方向。
//獲取方向鍵的偏移量
float translationX = Input.GetAxis("Horizontal");
float translationZ = Input.GetAxis("Vertical");
if (!game_over)
{
//移動和旋轉
wyb.transform.Translate(translationX * wyb_speed * Time.deltaTime, 0, translationZ * wyb_speed * Time.deltaTime, Space.World);
if (translationX < 0)
{
wyb.transform.eulerAngles = new Vector3(0, -90, 0);
}
else if (translationX > 0)
{
wyb.transform.eulerAngles = new Vector3(0, 90, 0);
}
if (translationZ < 0)
{
wyb.transform.eulerAngles = new Vector3(0, 180, 0);
}
else if (translationZ > 0)
{
wyb.transform.eulerAngles = new Vector3(0, 0, 0);
}
}