Create a Quest System in Unity

Quests are a fundamental part of many games, providing players with goals and rewards. In this tutorial, you will learn how to create a simple quest system in Unity. We will cover quest creation, tracking, and completion.

Setting Up the Project

Before we start coding, let's set up a simple Unity project:

  1. Create a new Unity project.
  2. Create a new folder named Scripts to organize our scripts.
  3. Create another folder named Resources to store our quest data.

Creating the Quest Class

The first step is to define a Quest class to hold quest information such as title, description, and completion status.

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);
    }
}

Creating the Quest Manager

Next, we need a manager to handle our quests. The QuestManager class will store and manage the active quests.

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);
    }
}

Displaying Quests in the UI

To display quests to the player, we need a simple UI. Create a Canvas and a Text element in your scene to show the quest list.

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";
        }
    }
}

Interacting with Quests

Let's add some functionality to interact with our quests. For example, we can add a button to complete a quest.

using UnityEngine;

public class QuestGiver : MonoBehaviour
{
    public string questTitle;

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

Testing the Quest System

To test the quest system, add a QuestManager and QuestUI to your scene. Create a simple trigger zone with a QuestGiver script attached, and assign a quest title to complete.

Conclusion

We've covered the basics of creating a quest system in Unity. We learned how to create quests, manage them, display them in the UI, and interact with them. These concepts can be expanded to create more complex quest systems in your Unity projects.