Similar NavMeshAgent nextPosition Capability

I like where the 4.1.0 is going with a drop in replacement for the unity NavmeshAgent. I like that the target is now a Vector3, not a transform, that was always a pain for me.

I have used the a NavmeshAgent to calculate the position along its path but I do the following:

NavmeshAgent.updatePosition = false;
NavmeshAgent.updateRotation = false;

This allows me to use the NavMeshAgent nextPosition to set the characters position manually.

In A* I dont see any similar capability, I currently use the AIPath (with several modifiers (funnel, smooth)) and have to make quite a few hacks to get what I want. I upgraded to Pro, but have yet to update to the RichAI due to the repeat of hacks required. Now there is the new Beta, again will have to redo my hacks.

Any thought of getting this capability into the core to make A* more of a NavAgent drop in replacement?

Hi

In the beta you can do this:

// The IAstarAIMovement interface (name not finalized) exposes some additional movement methods
// but is not available on all movement scripts (in particular not AILerp) because they don't make sense there.
ai = GetComponent<AIPath> as IAstarAIMovement;
ai.canMove = false;
ai.updatePosition = false;
ai.updateRotation = false;

void Update () {
     // This line can be removed if you also remove canMove = false above
     // but sometimes you might want the extra control it provides
     ai.MovementUpdate(Time.deltaTime, true);

     // This is the velocity that the AI wants to move with during this frame
     var desiredVelocity = ai.desiredVelocity;
     // Do some custom movement
     desiredVelocity = new Vector3(desiredVelocity.x*1.2, desiredVelocity.y, desiredVelocity.z);
     
     ai.Move(ai.position, desiredVelocity * Time.deltaTime);
}

I am considering renaming updatePosition/updateRotation in a future beta because they don’t work in exactly the same way as the NavmeshAgent and I want to avoid that potential confusion. The NavmeshAgent continues to move with some internal position/rotation updates but does not update the Transform anymore, but the AIPath/RichAI movement scripts will simply stop applying the position/rotation updates. In your case I think they should be equivalent though.

Also. Note that in the 4.1.0 beta the funnel modifier may give you incorrect results sometimes (this is noted at the top of the changelog).

What hacks have you had to do? Perhaps this is something I could integrate into the main package?

See also https://www.arongranberg.com/astar/documentation/dev_4_1_0_9f8b6eb7/interface_pathfinding_1_1_i_astar_a_i.php#a3fe2c59b75abe53721d72ccca45ff013
https://www.arongranberg.com/astar/documentation/dev_4_1_0_9f8b6eb7/interface_pathfinding_1_1_i_astar_a_i.php#a0305f3583086d917ed37186af80cd66e
https://www.arongranberg.com/astar/documentation/dev_4_1_0_9f8b6eb7/interface_pathfinding_1_1_i_astar_a_i_movement.php

Actually that did the job.

Only little thing was I cant get to the current path being used. I use it to draw my path and display an estimated time of arrival.

But, without the funnel working its kinda silly looking… :slight_smile:

Thanks for your help and quick response!

Any timeline on the Funnel fix? Kinda hard to use or test the beta without it.

Sorry, I haven’t got a timeline at the moment.
The funnel modifier works most of the time, it will just generate incorrect results in some situations. So you can try to use it.

Hey Aron, do you have a quick updated example of how to do this behavior with the new API?

I tried something similar to this with no luck:

        _ai = this.GetComponent<IAstarAI>();
        _ai.canMove = false;
        _ai.destination = moveTo;

        if (_ai is RichAI)
        {
            ((RichAI)_ai).updatePosition = false;
            ((RichAI)_ai).updateRotation = false;
            ((RichAI)_ai).slowWhenNotFacingTarget = false;
        }

        Vector3 nextPosition;
        Quaternion nextRotation;
        _ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);

But nextPosition keeps coming back with my current position.

Hi

With the latest beta (4.1.2) it works like this:

void Start () {
	ai = GetComponent<IAstarAI>();
	ai.canMove = false;
	ai.destination = moveTo;
}

void Update () {
	Vector3 nextPosition;
	Quaternion nextRotation;
	ai.MovementUpdate(Time.deltaTime, out nextPosition, out nextRotation);
	// Do stuff with nextPosition/nextRotation here
	ai.FinalizeMovement(nextPosition, nextRotation);
}

Here is the beta documentation for the IAstarAI.MovementUpdate method which also has a similar code example: https://www.arongranberg.com/astar/documentation/dev_4_1_2_9dcd8927/interface_pathfinding_1_1_i_astar_a_i.php#a4da302ed0601695b2cff9d3c45310f56

I think the funnel code should be fixed in the 4.1.2 beta. Let me know if it doesn’t work as expected.