文章

創建敵人與角色的追逐行為

在 Unity 中,創建敵人與角色的追逐行為涉及到設置敵人 AI 以識別並追蹤玩家。這通常需要結合使用 NavMeshAgent 進行導航、以及一些邏輯來實現追蹤和攻擊。以下是詳細步驟,讓你可以設置這種行為:

1. 準備場景

步驟 1:設置角色和敵人

  1. 創建角色
    • Hierarchy 視窗中創建一個角色物件,例如使用 3D Object > Capsule
  2. 創建敵人
    • Hierarchy 視窗中創建一個敵人物件,例如使用 3D Object > Capsule
  3. 設置 NavMesh
    • 確保場景中已經設置了 NavMesh,這樣敵人才可以在場景中移動。

2. 設置敵人 AI

步驟 1:創建 AI 腳本

  1. 創建新腳本
    • Assets 資料夾中,右鍵選擇 Create > C# Script,命名為 EnemyAI.
  2. 編寫 AI 腳本
    • 打開 EnemyAI 腳本,添加以下代碼以實現追逐行為:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    using UnityEngine;
    using UnityEngine.AI;
    
    public class EnemyAI : MonoBehaviour
    {
        public Transform player; // 玩家角色
        public float detectionRange = 10f; // 偵測範圍
        public float attackRange = 2f; // 攻擊範圍
        private NavMeshAgent agent;
        private bool isPlayerDetected = false;
    
        void Start()
        {
            agent = GetComponent<NavMeshAgent>();
        }
    
        void Update()
        {
            float distanceToPlayer = Vector3.Distance(transform.position, player.position);
    
            if (distanceToPlayer < detectionRange)
            {
                isPlayerDetected = true;
                agent.SetDestination(player.position); // 追蹤玩家
                   
                if (distanceToPlayer < attackRange)
                {
                    // 在此處添加攻擊邏輯
                    Debug.Log("Attack Player!");
                }
            }
            else
            {
                isPlayerDetected = false;
                // 可以在這裡添加其他邏輯,如返回巡邏等
                // 例如:agent.SetDestination(patrolPoint.position);
            }
        }
    }
    

步驟 2:配置腳本

  1. 附加腳本
    • Hierarchy 視窗中選擇你的敵人物件,然後在 Inspector 視窗中將 EnemyAI 腳本拖放到敵人物件上。
  2. 設置參數
    • Inspector 中,將玩家角色拖放到 player 欄位中。
    • 根據需要調整 detectionRange(偵測範圍)和 attackRange(攻擊範圍)的值。

3. 添加攻擊行為

步驟 1:創建攻擊腳本

  1. 創建新腳本
    • Assets 資料夾中,右鍵選擇 Create > C# Script,命名為 Attack
  2. 編輯攻擊腳本
    • 打開 Attack 腳本,添加以下代碼以實現簡單的攻擊邏輯:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    using UnityEngine;
    
    public class Attack : MonoBehaviour
    {
        public float attackDelay = 1f; // 攻擊延遲
        private bool isAttacking = false;
    
        public void PerformAttack()
        {
            if (!isAttacking)
            {
                isAttacking = true;
                // 執行攻擊動畫或效果
                Debug.Log("Attacking Player!");
    
                // 重置攻擊狀態
                Invoke("ResetAttack", attackDelay);
            }
        }
    
        void ResetAttack()
        {
            isAttacking = false;
        }
    }
    

步驟 2:將攻擊邏輯整合到 AI 腳本中

  1. 修改 EnemyAI 腳本
    • 添加對 Attack 腳本的引用並呼叫攻擊方法:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    using UnityEngine;
    using UnityEngine.AI;
    
    public class EnemyAI : MonoBehaviour
    {
        public Transform player;
        public float detectionRange = 10f;
        public float attackRange = 2f;
        private NavMeshAgent agent;
        private Attack attackScript;
    
        void Start()
        {
            agent = GetComponent<NavMeshAgent>();
            attackScript = GetComponent<Attack>();
        }
    
        void Update()
        {
            float distanceToPlayer = Vector3.Distance(transform.position, player.position);
    
            if (distanceToPlayer < detectionRange)
            {
                agent.SetDestination(player.position);
    
                if (distanceToPlayer < attackRange)
                {
                    attackScript.PerformAttack(); // 執行攻擊
                }
            }
        }
    }
    

4. 測試和調整

  1. 測試 AI 行為
    • 點擊 Play 按鈕來測試敵人是否能夠正確追蹤並攻擊玩家。
  2. 調整參數
    • 根據測試結果,調整 detectionRangeattackRange 的值,以確保追蹤和攻擊行為的合理性。
  3. 增加動畫
    • 可以根據需要添加動畫,例如攻擊動畫,並在 Attack 腳本中觸發動畫。

5. 小結

通過以上步驟,你可以在 Unity 中為敵人添加基本的追逐行為,並設置簡單的攻擊邏輯。這些步驟包括設置敵人的導航網格、編寫基本的 AI 腳本以實現追蹤玩家,並添加攻擊行為。你可以根據需要擴展和優化這些功能,以創建更複雜的敵人行為和更豐富的遊戲體驗。

本文章以 CC BY 4.0 授權