I have a navmesh-based game using a RecastGraph with RichAI and Seekers to move AI and players along it. All works pretty well
But I want to give the option to block specific cells (a collection of nodes) to only be walkable by a player having a building on it (letâs say for visualization, a wall surrounds the whole cell).
If the wall is owned by Player A, only units from Player A should be able to walk over these nodes.
So I tried first a custom ITraversalProvider, which basically does:
public class FactionTraversalProvider : ITraversalProvider
{
public uint? Id { get; private set; }
public FactionTraversalProvider(uint? id)
{
Id = id;
}
public bool CanTraverse(Path path, GraphNode node)
{
unchecked
{
if (!(node.Walkable && (path.enabledTags >> (int)node.Tag & 0x1) != 0))
return false;
}
var wall = GridGenerator.Instance.GetGridView((Vector3)node.position)?.Wall;
return !wall || wall.FactionId == Id || !wall.FactionId.HasValue;
}
public uint GetTraversalCost(Path path, GraphNode node)
{
unchecked { return path.GetTagPenalty((int)node.Tag) + node.Penalty; }
}
}
So to recap the custom implementation, just the basic check + wall check.
I would say, that kind of works, but sometimes the AI is still moving through it, especially if the wall was recently built (I guess that could be fixed by reducing the path recalculation timeframe?)
My second problem is:
I try, especially for AI, to find suitable nearest nodes for operations, which basically rely on NavGraph.GetNearest
So I thought, well for that I can also just easily add a new NNConstraint
public class FactionNNConstraint : PathNNConstraint
{
public uint? Id { get; private set; }
public FactionNNConstraint(uint? id)
{
Id = id;
constrainArea = true;
}
public override bool Suitable(GraphNode node)
{
if (!base.Suitable(node))
return false;
var wall = GridGenerator.Instance.GetGridView((Vector3)node.position)?.Wall;
return !wall || wall.FactionId == Id || !wall.FactionId.HasValue;
}
}
But once I start using that constraint on paths of the RichAI/Seeker component it gets really weird, as my characters just randomly start to teleport into a point, which wasnât even reachable before (because separated by water and the nodes were not even connected, even so on the same graph).
So my question is:
a) is my approach even the right one or do I even miss a simpler thing?
b) if my approach is right, what is going on with my teleports?
Cheers and appreciate any help!