desiredVelocity only applying gravity after some time

Hello there,

I have replaced the Unity NavMesh with A* Pathfinding Project… great asset! already love it…

I am currently running into some issues with the desiredVelocity not being calculated correctly after some time. I am using canMove = false, and moving the object per code - but cannot see any issues with that.

Version 4.3.41
Using Recast Graphs, with the RichAI, and Gravity is turned on.

See this video:

https://drive.google.com/file/d/1FTVW9tm9_CZQzwvCf3gjFhegDb9MS2L9/view?usp=sharing

Green Line --> destination
Magenta Line --> desiredVelocity

Everything works fine until ~ 0:14, when the RichAI does stop giving me the desiredVelocity that I need… only applying gravity…

Any idea what that could be? It sometimes seems related to the AI being a little bit off the navmesh, but then again sometimes the AI can find its way out of these situations…

Hi

Note that setting canMove=false will disable all the movement calculations for the RichAI script. This includes the calculation for the desired velocity. Maybe that’s the issue?

If you want to set canMove=false but still calculate everything I recommend that you call ai.MovementUpdate every frame. See also https://arongranberg.com/astar/docs/iastarai.html#MovementUpdate

1 Like

Hey there,

thx for your reply… aaaand you are right :slight_smile:
That was actually the problem… I still had some code running from Unity NavMesh where I had to disable the NavMeshAgent if the character was let’s say “flying of the navmesh”.

But now I have a new one… exact same result… the player just stops acting, and that happens if I re-enable a “game scene” again…

I am loading my scenes and storing them in game objects, to enable and disable whichever scene I currently need… each “scene” contains one Pathfinder script…

Just asking: (1) when I disable one Pathfinder, and enable another one… is that one the one found by
“var navmesh = AstarPath.active;” ?

Because if I use it for setting the position of the player (instantly or even 1 frame after “loading”/enabling a scene), the player is set to some strange position above the mesh (like, 10 or more units above).

If I simply position the player (without asking for navmesh.GetNearest(position)), it will not move at all and the desiredVelocity vector simply points down (gravity).

Greetings
Denzi

Forgot some more questions :):

(2) Do I have to re-scan the graphs when I re-enable the underlying GameObjects?
(3) I am moving my characters per script … but I wonder: currently I only disable updateRotation. Both updatePosition and canMove are set to true. And still this seems to work?

Just in general, it would be nice to have some words in your tutorial regarding these questions… :slight_smile:

Especially the enable/disable behaviour - because I try to GetNearest(position) on a re-enabled Pathfinder… is that legit? I can imagine that it needs some time to reload graphs, or whatever…

Ok, I got it running… but I am receiving one error that I am not sure how to fix…

Here is the situation: the AstarPath.active instance is only set in Awake(), so there is no chance for me to ever reactivate it OnEnable() …

The solution: whenever I cache my scene (deactivate my “scene” gameobject), I disable the “old” AstarPath instance in it, and create a new one that I use when the “scene” is re-activated… here is the code:

// deactivate the “scene” container, but before make a copy of the “old AstarPath”, and destroy the old one.

     public void Deactivate()
    {
        var oldAstar = container.GetComponentInChildren<AstarPath>(true);

        container.SetActive(false);

        var newAstar = GameObject.Instantiate(oldAstar, oldAstar.transform.parent);
        newAstar.gameObject.name = oldAstar.gameObject.name;

        GameObject.Destroy(oldAstar.gameObject);
    }

// coroutine that activates the “scene” container object, re-scans the graphs, and waits until scanning is finished. (loading screen will be hidden afterwards)

    public IEnumerator Activate()
    {
        container.SetActive(true);

        AstarPath.active.Scan();

        do
            yield return Game.Coroutines.WaitForEndOfFrame;
        while (AstarPath.active.isScanning);
    }

So far it works like a charm… BUT: I guess due to some Unity gameobject lifecycle shizzle, the “old AstarPath” gameobject is not destroyed until I re-open that scene container… remember, the cached “scene” is deactivated, so I guess Unity does not call OnDestroy() until the container is re-activated.

Which leads to this error message - which does nothing else but throw an error - the game itself seems to run fine… :

IndexOutOfRangeException: Index was outside the bounds of the array.
Pathfinding.HierarchicalGraph.CompactifyDirtyNodes () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Misc/HierarchicalGraph.cs:168)
Pathfinding.HierarchicalGraph.AddDirtyNode (Pathfinding.GraphNode node) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Misc/HierarchicalGraph.cs:190)
Pathfinding.MeshNode.ClearConnections (System.Boolean alsoReverse) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Nodes/GraphNode.cs:604)
Pathfinding.GraphNode.Destroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/Nodes/GraphNode.cs:110)
Pathfinding.NavGraph+<>c.b__25_0 (Pathfinding.GraphNode node) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:291)
Pathfinding.NavmeshBase.GetNodes (System.Action`1[T] action) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:331)
Pathfinding.NavGraph.DestroyAllNodes () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:291)
Pathfinding.NavGraph.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:275)
Pathfinding.NavmeshBase.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:249)
Pathfinding.NavGraph.Pathfinding.IGraphInternals.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:416)
Pathfinding.AstarData.ClearGraphsInternal () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarData.cs:342)
Pathfinding.AstarData.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarData.cs:361)
AstarPath.OnDestroy () (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarPath.cs:1436)

Should I be worried? What can I do? :slight_smile:

Sorry for the long post!
Greetings
Denzi

One video for the error that doesn’t do no harm:

https://drive.google.com/file/d/10L339xaxijA5olmf-QPt7661YQYimRGk/view?usp=sharing

After playing around a bit, I notice that when transitioning to some scenes (not all), I am am gettting more and more errors like this:

NullReferenceException: Object reference not set to an instance of an object
Pathfinding.TriangleMeshNode.GetVertices (Pathfinding.Int3& v0, Pathfinding.Int3& v1, Pathfinding.Int3& v2) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NodeClasses/TriangleMeshNode.cs:97)
Pathfinding.TriangleMeshNode.ClosestPointOnNode (UnityEngine.Vector3 p) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NodeClasses/TriangleMeshNode.cs:129)
Pathfinding.BBTree.SearchBoxClosest (System.Int32 boxi, UnityEngine.Vector3 p, System.Single& closestSqrDist, Pathfinding.NNConstraint constraint, Pathfinding.NNInfoInternal& nnInfo) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Utilities/BBTree.cs:358)
Pathfinding.BBTree.SearchBoxClosest (System.Int32 boxi, UnityEngine.Vector3 p, System.Single& closestSqrDist, Pathfinding.NNConstraint constraint, Pathfinding.NNInfoInternal& nnInfo) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Utilities/BBTree.cs:381)
Pathfinding.BBTree.SearchBoxClosest (System.Int32 boxi, UnityEngine.Vector3 p, System.Single& closestSqrDist, Pathfinding.NNConstraint constraint, Pathfinding.NNInfoInternal& nnInfo) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Utilities/BBTree.cs:381)
Pathfinding.BBTree.SearchBoxClosest (System.Int32 boxi, UnityEngine.Vector3 p, System.Single& closestSqrDist, Pathfinding.NNConstraint constraint, Pathfinding.NNInfoInternal& nnInfo) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Utilities/BBTree.cs:381)
Pathfinding.BBTree.QueryClosest (UnityEngine.Vector3 p, Pathfinding.NNConstraint constraint, System.Single& distance, Pathfinding.NNInfoInternal previous) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Utilities/BBTree.cs:343)
Pathfinding.NavmeshBase.GetNearestForce (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:504)
Pathfinding.NavmeshBase.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint, Pathfinding.GraphNode hint) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/NavmeshBase.cs:456)
Pathfinding.NavGraph.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Generators/Base.cs:211)
AstarPath.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint, Pathfinding.GraphNode hint) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarPath.cs:2116)
AstarPath.GetNearest (UnityEngine.Vector3 position, Pathfinding.NNConstraint constraint) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarPath.cs:2081)
AstarPath.GetNearest (UnityEngine.Vector3 position) (at Library/PackageCache/com.arongranberg.astar@4.3.41/Core/AstarPath.cs:2049)

Next video on that: :slight_smile:

https://drive.google.com/file/d/1Jweon6ZHIAQ_h7seiP16-v9AT3Mq-OjA/view?usp=sharing

Uaah… finally fixed it…

At 2 AM :slight_smile:

I now use only one instance of the Pathfinder, without a generated cache, and scan each scene on opening/“re-activating” the scene container object…

Thanks for your support!

Keep up the good work! :slight_smile:

Greetings
Denzi!

1 Like