文章

為角色添加簡單的AI行為

為角色添加簡單的 AI 行為可以使遊戲中的角色顯得更智能和有趣。以下是使用 Unity 的 NavMesh 和 C# 腳本來實現基本 AI 行為的步驟:

1. 準備工作

步驟 1:創建角色和目標

  1. 角色物件
    • Hierarchy 視窗中創建一個角色物件(如 3D Object > Capsule),並為其添加 NavMeshAgent 組件。
  2. 目標物件
    • 創建一個目標物件(如 3D Object > Sphere),作為角色的目標。

2. 設置 NavMesh

  1. 設置地形和障礙物
    • 確保場景中的地形和障礙物已經設置了 NavMesh,並且 NavMeshAgent 組件的設置也已經調整完畢。

3. 編寫 AI 腳本

步驟 1:創建新腳本

  1. 創建 AI 腳本
    • Assets 資料夾中,右鍵選擇 Create > C# Script,創建一個新的 C# 腳本,命名為 SimpleAI.
  2. 編輯 AI 腳本
    • 打開 SimpleAI 腳本,並添加以下代碼來實現簡單的跟隨目標行為:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    using UnityEngine;
    using UnityEngine.AI;
    
    public class SimpleAI : MonoBehaviour
    {
        public Transform target; // 目標位置
        private NavMeshAgent agent;
    
        void Start()
        {
            agent = GetComponent<NavMeshAgent>();
        }
    
        void Update()
        {
            if (target != null)
            {
                agent.SetDestination(target.position); // 設置目標位置
            }
        }
    }
    

步驟 2:配置腳本

  1. 附加腳本
    • Hierarchy 視窗中,選擇你的角色物件,並在 Inspector 視窗中將 SimpleAI 腳本拖放到角色物件上。
  2. 設置目標
    • Inspector 中,將目標物件拖放到 SimpleAI 腳本的 Target 欄位中。

4. 添加更複雜的 AI 行為(可選)

你可以擴展 AI 腳本,讓角色具備更多的行為,例如:

1. 基本巡邏行為

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 PatrolAI : MonoBehaviour
{
    public Transform[] waypoints; // 巡邏點
    private int currentWaypointIndex = 0;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        GoToNextWaypoint();
    }

    void Update()
    {
        if (!agent.pathPending && agent.remainingDistance < 0.5f)
        {
            GoToNextWaypoint();
        }
    }

    void GoToNextWaypoint()
    {
        if (waypoints.Length == 0)
            return;

        agent.destination = waypoints[currentWaypointIndex].position;
        currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
    }
}
  • 設置巡邏點
    • Inspector 中,將巡邏點對象拖放到 waypoints 陣列中。

2. 簡單的尋找與攻擊行為

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
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
    public Transform player; // 玩家角色
    public float detectionRange = 10f; // 偵測範圍
    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); // 追蹤玩家
        }
        else
        {
            isPlayerDetected = false;
            // 可以在這裡添加回到巡邏點或其他行為的邏輯
        }
    }
}
  • 設置玩家角色
    • Inspector 中,將玩家角色拖放到 player 欄位中。

5. 測試和調整

  1. 測試 AI 行為
    • 點擊 Play 按鈕來測試角色的 AI 行為,確保角色按照預期跟隨目標或執行其他行為。
  2. 調整參數
    • 根據需要調整 NavMeshAgent、AI 腳本中的參數(如速度、範圍等),以實現最佳效果。

6. 小結

以上步驟介紹了如何在 Unity 中為角色添加簡單的 AI 行為。通過使用 NavMeshAgent 和 C# 腳本,你可以實現角色的基本導航、巡邏、追蹤等功能。根據遊戲需求,你可以進一步擴展 AI 行為,例如添加更多複雜的狀態機、感知系統和行為樹等,以提升遊戲的智能性和挑戰性。

本文章以 CC BY 4.0 授權