Agents falling off of complex navmesh

https://youtu.be/wUxPzfK1aPk

Hi @aron_granberg

Wondering about an issue i’m having with agents falling off of the navmesh, as shown in the video above. You’ll see the red agents moving around the mobius strip, shooting, and eventually they just fly off the nav mesh. Later in the video you see one fly off and reattach itself to a part of the navmesh, but it just gets stuck there.

This is all setup like Example19_Spherical in the beta, so it’s Navmesh, RVO, etc. I’ve tried Constrain to graph but that doesn’t work, they still fly around at some point pretty quickly. I’ve also tried the Clamp to Navmesh script included in the A* package, but then agents just get stuck in a location, not able to move at all it seems.

Any advice on how to restrict them to the structure? Maybe make their movements a little less jerky as well :slight_smile: If you need any more info just let me know. Thanks!

Wow, that is indeed a really complex navmesh.

I’d try something like:

void Update () {
    ai.position = AstarPath.active.GetNearest(ai.position).position;
}

to see if that works.
Disable constrain to graph (it doesn’t work well for non-planar graphs).

Also, if you can, make your collision mesh pretty smooth. That will allow the agents to move across it better.

Thanks! This seems to help but is also a big performance hit with even a couple units. Seems like I can get away with calling it less frequently than the Update loop, but need to test it out more.

Just to be clear, you’re updating the transforms position and not a value in one of the A* scripts, right? Are there any redundant interactions happening with doing this while using Seeker / RVO Controller / AI Path aligned to Surface / Funnel Modifier on the AI units?

Thanks so much!

Some updates on trying to address this performance drop @aron_granberg

I made an inherited AI Path Aligned To Surface class that added your Get Nearest suggestion on update and it seems to work, but massively decreases performance. I have a drop from 60+ FPS to roughly 20 FPS. This is in editor, with 5 enemy units on the mesh. At some points it even drops to 5 or 6 FPS. Profiler shows it’s definitely the addition of Get Nearest.

I’ve also tried calling it less frequently, hoping to strike a balance of staying on mesh and using less calls, but after a short amount of time the enemies will fly off the mesh.

Any ideas of how to better implement this? Have I headed down the wrong direction with how to use this?

Appreciate all your help!

EDIT: Also want to note, after this I tried upgrading to the latest beta just to try things out, and now all of my enemies throw this error from my custom script, which only adds the suggested GetNearest to the update of AIPathAlignedToSurface

transform.position assign attempt for ‘Enemy Dodeca AI 2(Clone)002’ is not valid. Input position is { Infinity, Infinity, Infinity }.
UnityEngine.Transform:set_position (UnityEngine.Vector3)
Pathfinding.CustomAIPathAlignedToSurface:Update () (at Assets/Scripts/CustomAIPathAlignedToSurface.cs:15)

Another update! Fixed the whole infinity thing, seems it was an issue with the saved graph cache and having updated to the newer beta.

Have this working now, and it seems like on beta 71 i’m not getting the massive performance decreases I got before, but still seems to effect frame rate substantially. Still wondering how to best improve performance, if you have any advice it would be appreciated, thanks!

1 Like

I’m not quite sure what the best approach would be. Perhaps a faster option could be to rely on the physics engine and do a sphere cast to try to find a close-enough position to snap to?

Thanks for the idea, I’ve actually changed some other things and seemed to get performance back, but have now run into other issues. I’m not sure where I went wrong but hoping you have some ideas regarding this situation. To try and figure out potential geometry issues, i’ve dialed things back to your provided example, to see if issues persist. Here’s a video of my units trying to traverse the strange box and try to follow the player. You’ll see them spawn on the top of the surface, and then attempt to move to the side.

Seems like paths are being accurately generated, but it’s having trouble getting over this edge. Below is my extension on AI Path Aligned to Surface for reference, used some of your advice above. The original example of strange box seems to work fine, so I’m trying to figure out what’s happening, as the attributes are set similarly, just scaled up larger. Also it’s still possible for them to fall off, with an increased speed they can hit that edge and fly off the mesh.

using UnityEngine;
using Pathfinding;
using Chronos;

namespace Pathfinding
{
    public class CustomAIPathAlignedToSurface : AIPathAlignedToSurface
    {
        public Timeline clock; // Chronos local clock

        private void Awake()
        {
            clock = GetComponent<Timeline>();
        }

        void FixedUpdate()
        {
            base.OnUpdate(clock.deltaTime); // Use the Chronos time instead of Unity time

            // Get the nearest walkable node
            Vector3 nearestPosition = AstarPath.active.GetNearest(transform.position).position;

            if (float.IsNaN(nearestPosition.x) || float.IsNaN(nearestPosition.y) || float.IsNaN(nearestPosition.z) ||
                float.IsInfinity(nearestPosition.x) || float.IsInfinity(nearestPosition.y) || float.IsInfinity(nearestPosition.z))
            {
                // Handle the invalid position, e.g., skip updating the position or assign a default position
                ConditionalDebug.LogWarning("Invalid position detected: " + nearestPosition);
            }
            else
            {
                transform.position = nearestPosition;
            }
        }

    }
}

Any help greatly appreciated! Keep getting stuck on proper implementation of this, will be happy to get something consistently working :slight_smile:

So an update, I’ve tried going back to your beta example and using various complex, but generally rounder and simpler meshes with some good results. I can find some that agents never seem to fall off of when i leave them running thankfully :slight_smile: But I’m not certain on how far I can push it, is it possible for you to give some guidelines on complexity, best practices for more complex meshes. For example, any idea what kind of curve limits is the maximum for them?