Forum rules - please read before posting.

Getting path node transforms in script

Hi

Is there a way to get the next node of a path within a script?

I have an NPC which moves along a path, but it flys so I need to know the Y position of the next nodes transform so that I can lerp the mesh object toward that value, and do it again for the next node until the path ends.

I can see the node section of the scripting guide but I wonder if there are any code examples kicking around as I can't make it work as expected.

I could do something with a cutscene/actionlist on each node, but I have a lot of paths, across a lot of scenes and I'm always keen to code stuff.

Thanks

Olly

Comments

  • You can hook into the OnCharacterReachNode custom event to get details of how far along a Path a character is:

    using UnityEngine;
    using AC;
    
    public class GetNextNodePosition : MonoBehaviour
    {
    
        void OnEnable () { EventManager.OnCharacterReachNode += OnCharacterReachNode; }
        void OnDisable () { EventManager.OnCharacterReachNode -= OnCharacterReachNode; }
    
        void OnCharacterReachNode (AC.Char character, Paths path, int nodeIndex)
        {
            int nextNodeIndex = nodeIndex + 1;
            if (nextNodeIndex >= path.nodes.Count) return; // Reached the end
    
            Vector3 nextNodePosition = path.nodes[nextNodeIndex];
        }
    
    }
    
  • Great, thanks Chris. That should give me a better way to handle this.

  • Chris, any idea why that event would be called twice for each node?

  • I can't recreate such behaviour. Do you have multiple instances of the script present?

    One thing I should have mentioned is that the "character" parameter should be used to filter out events called by other characters. If you attach the script to your NPC, you can begin the event function with this:

    if (character.gameObject != gameObject) return;
    

    If you're still getting duplicates, put a Debug.Log statement in there and check the stacktrace - what's showing up in the Console?

  • Hi Chris. Thanks for the tip, already had that one covered. Turns out it was a duplicate gameobject from the issues around the NPCs not being instantiated. Removed it and it's all working.

    Thanks

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.