How to navigate to NPC using Unity Navigation System

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
cozysmommy
Posts: 7
Joined: Wed Sep 27, 2023 7:53 pm

How to navigate to NPC using Unity Navigation System

Post by cozysmommy »

Hi Tony,
I'm encountering a new problem right now. :oops: :oops:
I want to use the Unity navigation system to show a navigation route from player to NPC. The navigated NPC is depend on the quest state. If the quest state is Active, then the current navigated NPC will be the quest's target actor.
To accomplish this function, these're what I've done so far.
(1) I've set up a new Quest template called Target Actor, whose type is Actor and the initial value is None. After that, I assigned the Target Actor value of each quest based on the Actors I created. Let's say there's a quest called Talking to Li, and the Target Actor of Talking to Li is the Actor named Li.
(2) I've added the Dialogue Actor component to each NPC Game Object and assigned the Actor value based on the quest the Game Object would interact with. Take the quest Talking to Li as an example, there's a Game Object called LiChanjuan in my scene, and the player must complete the conversation with this object to complete the quest Talking to Li, so the Actor value of this object is Li.
(3)I've written a script about the navigation route as you can see below. This script requires the navigation target, which is a public Game Object. I want to dynamically change the navigation target actor when there exists a quest state change, then assign the target NPC game object to this script. I don't know how to listen to the quest state change event and I'm not sure if steps (1) & (2) help me fix this problem. What I can do right now is to just assign a static target and show a navigation route to that target NPC in the game.

Code: Select all

public class InGameNavigation : MonoBehaviour

{

    public NavMeshAgent agent;

    public Transform target;

    public LineRenderer lineRenderer;




    void Update()

    {

        agent.SetDestination(target.position);

        Vector3[] path = agent.path.corners;

        for (int i = 0; i < path.Length; i++)

        {

            path[i] = path[i] + new Vector3(0, 1, 0);

        }

        lineRenderer.SetVertexCount(path.Length);

        for (int i = 0; i < path.Length; i++)

        {

            lineRenderer.SetPosition(i, path[i]);

        }

    }

}
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to navigate to NPC using Unity Navigation System

Post by Tony Li »

Hi,

To listen for quest state changes, add an OnQuestStateChange(string questName) method to a script on your Dialogue Manager GameObject or any of its children. The Dialogue System will automatically call this method when a quest state changes.

If the LiChanjuan GameObject has a Dialogue Actor whose Actor dropdown is set to "Li", then you can get this GameObject using CharacterInfo.GetRegisteredTransformTransform("Li").
Post Reply