AstarPath.active.GetNearest precision

Hello!

I’m using AstarPath.active.GetNearest on a randomly generated point passing a walkable constraint to find the nearest walkable point.

It works fine if I use the nnInfo.node.position but I’m finding some occasional issues when using nnInfo.position where the target point seems to fall outside the walkable node (see below).

The result is a failure in calculating a valid path once the agent has reached the last valid node.

Should I steer away from using nnInfo.node.position when looking for a constrained destination?

Hi

I can’t really make out what it is you want to say with that picture. Could you elaborate a bit?
nnInfo.position will always be on the surface of the node. Note that it may lie exactly on the boundary between two nodes.

Hello,

The picture was only meant to show that the destination point (blue circle) is not reached by the path (green line) nor calculated as a path node (purple squares) and it’s not clear to me why given that I have calculated that point via AstarPath.active.GetNearest with walkable constraint.

Thanks

Hi

Are you sure you do not have any other scripts that set the destination (e.g. a AIDestinationSetter component)?

Also, I think you have the ‘Show Search Tree’ option enabled in A* Inspector -> Settings. That makes it harder to see the full extent of the graph.

Hi,

AIDestinationSetter target was set to null, but I have removed and tested again getting the same result. Also disabled the search tree in settings. I suppose I would expect to see the path in the picture below ending at the blue circle destination, rather than on the edge of the previous node.

What are your settings for the Seeker -> Start End Modifier?

image

That is very odd. Can you post the exact code you are using to set the destination?

This is part of a modified version of the OPSIVE Wander behavior, so I can’t quite post the exact code without lots of irrelevant context. But below are the relevant bits.

Before you spend any more time on this, I should stress that this issue is fairly random and it has taken me some time to reproduce and I’m OK for now with the workaround I’m using for the demo I’m working on. The point of the post was to determine if there was an underlying known issue with AstarPath.active.GetNearest but you have implicitly confirmed not to be the case.

If you want to keep troubleshooting this I’m happy to help, but it may lead to an input error.

        // from an arbitrary position, it returns the nearest valid position.
        // useNodeCenter is the workaround I have been using, for the issues posted here this has been 
        // set to false
        protected Vector3 GetNearestValidPosition(Vector3 position, bool useNodeCenter)
        {
            var constraint = NNConstraint.None;
            // Constrain the search to walkable nodes only
            constraint.constrainWalkability = true;
            constraint.walkable = true;
            var nnInfo = AstarPath.active.GetNearest(position, constraint);
            if(useNodeCenter)
                return new Vector3(nnInfo.node.position.x*0.001f,nnInfo.node.position.y*0.001f, nnInfo.node.position.z*0.001f);
            else
                return nnInfo.position;
        }
        
        protected IAstarAI agent;

        //sets agent destination (input target is GetNearestValidPosition output)
        protected override bool SetDestination(Vector3 target)
        {
            agent.canSearch = true;
            agent.canMove = true;
            agent.destination = target;
            return true;
        }

Okay. Well, I cannot see anything strange in your code.
Though you should know that you can convert an Int3 to a Vector3 more easily by just casting:

if(useNodeCenter)
    return (Vector3)nnInfo.node.position;
 else
    return nnInfo.position;
}

It’s also useful to use Debug.DrawLine to debug things like this.

Thanks for looking into this and for the tips. If I run into this again and have a need for it I may ask you for some advice on how to debug the root cause.