Callback when AI reaches Node in Path?

Is there a callback that occurs when an AI reaches a Node in its path? I need to stop an AI after it touches a set number of Nodes (which will often be less than the full path).

I am using a GridGraph with AILerp, so the posts I’ve found so far in the forums may not work for me (since they are referring to AIPath and not AILerp).

If there isn’t a callback or notification, then where in the code does this happen? I can then tap into that to add a callback. It has to happen somewhere, as the AI turns correctly towards the next node.

Thanks

Hey RVandal,

I don’t think there’s such a callback but I’m not sure. Scanning through the AILerp code, it uses the PathInterpolator which has a NextSegment function. There is a lot going on in there so might not be the best place.

I tried making a test and adding a debug and it looks to work pretty well. You could set up an event action to be called from there.

	protected virtual void NextSegment () {
		segmentIndex++;
        Debug.Log("NextSegment " + segmentIndex + " " + path[segmentIndex]);
		distanceToSegmentStart += currentSegmentLength;
		currentSegmentLength = (path[segmentIndex+1] - path[segmentIndex]).magnitude;
	}

resulted in “2D” scene running like this, so it seems to be what you want. Should get you on track at least

there is definitely a cleaner way of doing it but I don’t have a good birds eye view of the project yet

1 Like

I think this is exactly what I’m looking for… thanks!

1 Like

This is what the code in PathInterpolator.cs looks like now:

Towards the top, add:

public delegate void OnNextSegmentDelegate();
public OnNextSegmentDelegate onNextSegment;

PathInterpolator.NextSegment now looks like:

	protected virtual void NextSegment () {
        var tmpOnNextSegment = onNextSegment;
        if(tmpOnNextSegment != null) {
            tmpOnNextSegment();
        }

		segmentIndex++;
		distanceToSegmentStart += currentSegmentLength;
		currentSegmentLength = (path[segmentIndex+1] - path[segmentIndex]).magnitude;
	}

I have my AILerp subclass subscribe to the onNextSegment event, and it works as expected.

1 Like