Trying to update a path every frame

Hi, I’m new to this asset, but I have to say that it is really awesome and I’m enjoying programming my own AI using it.

I was wondering if there’s any way to update a path every frame? I don’t want to call StartPath over and over again because that seems like it would be taxing on performance if I had a lot of NPC’s in my scene.

The reason I need this feature is because my levels are dynamic and always moving and rotating. (Which later on I might come back to ask more questions about updating the graph every frame)

Currently to start a path you need to call StartPath with a Vector3 start and end. I would like to update the path every frame in case the position of the end point changes or moves before the NPC arrives at said destination.

Thanks for your time! If I didn’t explain my issue well enough I will be sure to explain what I can.

Best Regards,
Soloman

Hi

If you want to update the path every frame then you obviously have to calculate the path every frame, there is not much you can do to avoid this. You might want to use AstarPath.WaitForPath to make sure that your call to seeker.StartPath doesn’t just cancel the previous path which is calculated asynchronously in the background.
Really most games don’t update paths every frame, it’s usually enough to do it much less frequently.

Ah, in that case you might want to take a look at the example scene called ‘Moving’ (available in the pro version, though the technique for solving it can be used in the free version as well). What that does is that it calculates the path in a local space relative to a ship that is moving, and the graph stays at the world origin at all times, then it transforms the path points back into world space in every Update() when it moves.

I’m still a bit lost. Sorry to completely sound like a noob, but all I really want to do, is just update the path and graph whenever a surface is moving. Here’s some code showing my current setup (please don’t laugh at it :frowning:)

protected override void OnUpdate()
{
    if (Waypoints == null) return;

    if (Waypoints.value.Length > 0)
    {
        if (!StartNav)
        {
            mSeeker.StartPath(agent.transform.position, Waypoints.value[WaypointIndex].position);

            StartNav = true;
        }

        float distanceToWaypoint = Vector3.Distance(agent.transform.position, Waypoints.value[WaypointIndex].position);

        if (distanceToWaypoint <= StopDistance)
        {
            // Clear motion controller target position and rotation.
            mMotionController.ClearTargetPosition();
            mMotionController.ClearTargetRotation();

            // Count up the wait time.
            CurrentWaitTime += Time.deltaTime;

            if (CurrentWaitTime >= WaitTime)
            {
                // Finish waiting and get the next waypoint.
                CurrentWaitTime = 0;
                PathIndex = 0;
                WaypointIndex = (WaypointIndex + 1) % Waypoints.value.Length;
                StartNav = false;
            }
        }
        else
        {
            Path path = mSeeker.GetCurrentPath();

            if (path != null && path.vectorPath.Count > 0 && PathIndex < path.vectorPath.Count)
            {
                // Cache
                List<Vector3> vectorPath = path.vectorPath;
                Vector3 pathPosition = vectorPath[PathIndex];
                float distanceToPathPosition = Vector3.Distance(agent.transform.position, pathPosition);

                if (distanceToPathPosition <= StopDistance)
                {
                    // Increment the path index.
                    PathIndex++;
                }
                else
                {
                    // Set the motion controller target.
                    mMotionController.SetTargetPosition(pathPosition, 0.5f);
                    mMotionController.SetTargetRotation(Quaternion.LookRotation(pathPosition - agent.transform.position));
                }
            }
        }
    }
}

Is there any way I can incorporate the “LocalSpaceRichAI” into my code? I don’t want to use the RichAI to move the character or use any physics, instead I just want to use it to re-calculate the path. This way I can just simply tell my character to move to the next node.

Thanks, Aron.

Soloman

Do you mean that you want to recalculate the path, i.e plan a new path from the start to the end OR that you want to transform the path so that the path you previously calculated moves correctly with the surface that is moving (I assume the whole graph sits on a single moving surface).

Right, I’d like to recalculate the path (unless you think that’s unnecessary). My game is basically a space shooter, that allows the player to walk around the ship, and talk to different NPC’s that are walking around and interacting with different things on the ship, all while the ship is moving and or rotating.

In a way, I’d like to make a carbon copy of your “Moving” scene but using my own AI (I’m using node-canvas).

I told my buddy what we’re trying to do isn’t something that’s simple, but he really want’s this to be a thing in our game.

Thanks for your time, man.