[HOWTO] How To: Initialize NPC/Object Locations In Scene By Quest State

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 20632
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Initialize NPC/Object Locations In Scene By Quest State

Post by Tony Li »

Here's a quick way to set up NPC/object positions based on a quest's state when starting a scene.

Add the CharacterQuestSetup script below to the GameObject:

CharacterQuestSetup.cs

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class CharacterQuestSetup : MonoBehaviour
{

    [QuestPopup] public string quest;
    public Transform inactiveLocation;
    public Transform activeLocation;
    public Transform completedLocation;

    private void Start()
    {
        switch (QuestLog.GetQuestState(quest))
        {
            case QuestState.Unassigned:
                GotoLocation(inactiveLocation); break;
            case QuestState.Active:
                GotoLocation(activeLocation); break;
            case QuestState.Success:
            case QuestState.Failure:
                GotoLocation(completedLocation); break;
            default:
                GotoLocation(null); break;
        }
    }

    private void GotoLocation(Transform location)
    {
        if (location == null)
        {
            gameObject.SetActive(false);
        }
        else
        {
            transform.SetPositionAndRotation(location.position, location.rotation);
        }
    }
}
Then assign locations for each quest state. If you don't want to the character to start active in the scene if the quest is in a specific state, leave that field unassigned.

characterQuestSetup.png
characterQuestSetup.png (23.79 KiB) Viewed 384 times
Post Reply