Unityでクエストシステムを作成する

クエストは多くのゲームの基本部分であり、プレイヤーに目標と報酬を提供します。このチュートリアルでは、Unity でシンプルなクエスト システムを作成する方法を学習します。クエストの作成、追跡、完了について説明します。

プロジェクトの設定

コーディングを始める前に、簡単な Unity プロジェクトを設定しましょう。

  1. 新しい Unity プロジェクトを作成します。
  2. スクリプトを整理するために、Scripts という名前の新しいフォルダーを作成します。
  3. クエスト データを保存するために、Resources という名前の別のフォルダーを作成します。

クエストクラスの作成

最初のステップは、タイトル、説明、完了ステータスなどのクエスト情報を保持する Quest クラスを定義することです。

using UnityEngine;

[System.Serializable]
public class Quest
{
    public string title;
    public string description;
    public bool isCompleted;

    public Quest(string title, string description)
    {
        this.title = title;
        this.description = description;
        this.isCompleted = false;
    }

    public void CompleteQuest()
    {
        isCompleted = true;
        Debug.Log("Quest Completed: " + title);
    }
}

クエストマネージャーの作成

次に、クエストを処理するマネージャーが必要です。 QuestManager クラスはアクティブなクエストを保存および管理します。

using System.Collections.Generic;
using UnityEngine;

public class QuestManager : MonoBehaviour
{
    public List<Quest> quests = new List<Quest>();

    void Start()
    {
        // Example quests
        quests.Add(new Quest("Find the Key", "Find the key to unlock the door."));
        quests.Add(new Quest("Defeat the Dragon", "Defeat the dragon in the cave."));
    }

    public void CompleteQuest(string title)
    {
        Quest quest = quests.Find(q => q.title == title);
        if (quest != null && !quest.isCompleted)
        {
            quest.CompleteQuest();
        }
    }

    public List<Quest> GetActiveQuests()
    {
        return quests.FindAll(q => !q.isCompleted);
    }
}

UI でクエストを表示する

プレイヤーにクエストを表示するには、シンプルな UI が必要です。シーン内に Canvas と Text 要素を作成して、クエスト リストを表示します。

using UnityEngine;
using UnityEngine.UI;

public class QuestUI : MonoBehaviour
{
    public Text questListText;
    private QuestManager questManager;

    void Start()
    {
        questManager = FindObjectOfType<QuestManager>();
        UpdateQuestList();
    }

    void UpdateQuestList()
    {
        questListText.text = "Quests:\n";
        foreach (Quest quest in questManager.GetActiveQuests())
        {
            questListText.text += "- " + quest.title + ": " + quest.description + "\n";
        }
    }
}

クエストとのやりとり

クエストを操作するための機能をいくつか追加してみましょう。たとえば、クエストを完了するためのボタンを追加できます。

using UnityEngine;

public class QuestGiver : MonoBehaviour
{
    public string questTitle;

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            QuestManager questManager = FindObjectOfType<QuestManager>();
            questManager.CompleteQuest(questTitle);
        }
    }
}

クエストシステムのテスト

クエスト システムをテストするには、シーンに QuestManagerQuestUI を追加します。 QuestGiver スクリプトが添付された単純なトリガー ゾーンを作成し、完了するクエスト タイトルを割り当てます。

結論

Unity でクエスト システムを作成するための基本について説明しました。クエストの作成方法、管理方法、UI での表示方法、およびクエストとの対話方法を学びました。これらの概念を拡張して、Unity プロジェクトでより複雑なクエスト システムを作成できます。