[HOWTO] How To: Initialize NPC/Object Locations In Scene By Quest State
Posted: Wed Jan 31, 2024 2:56 pm
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
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.
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);
}
}
}