Hello, I’m using recast graph in my game. My usecase is currently imagine if two characters are fighting amongst themselves other npc’s moves through them. I want them to take alternate way for that I’m trying to apply penalties to the node but it seems like even after applying penalty they are still using same path and moving though fighting npc’s. IS there any way to fix this issue
public void ApplyPenaltyToArea(Vector3 center, float radius, uint penalty)
{
var graph = AstarPath.active.data.recastGraph;
graph.GetNodes(node => {
if (Vector3.Distance((Vector3)node.position, center) <= radius)
{
node.Penalty = penalty;
}
});
}
This may be an issue with the arguments themselves? What is the value of the penalty you’re setting? I tried this code just to make sure it’s not an issue with setting the nodes themselves and it worked fine. It’s simply your code but I took out the arguments and set it to random, so every node just gets a random value. I set my graph coloring to “Penalty” and worked fine. Every node changed color.
public void RandomPenalty(){
var graph = AstarPath.active.data.recastGraph;
graph.GetNodes(node => {
node.Penalty = (uint)Random.Range(0f, 5000f);
});
}
Can you share how your map looks after applying penalty? I tried applying high penalty from (1000-30000) but it didn’t work. Also maps color stayed same blue.
Be sure that you set your graph coloring to show the penalities. It does not by default. You can find it in the settings of your AstarPath component. This is what my graph does when I run that code.
Thanks. I tried again and penalties are applied to the graph but even then as you can see in the gif npc’s still walked through high penalty zone. What I’m trying to do in my game is that I’m applying penalty on recast graph when two character fight each other because right now if two characters are fighting on the point which falls on optimal path of other npc they kinda walk through them which looks weird https://drive.google.com/file/d/1_OPbrk3Kz4gc3R3xXvD1GxUZHBPKEpeY/view?usp=sharing
From the look of it, that looks like it’s because the cost of going through, even at it’s worst, is still much lower than it would be going around maybe? Try setting it to something absurd and seeing if you can get it to react from there- in the video you sent it looks like there’s a large amount of “walking around” that would need to be done.
Penalties on recast graphs are kinda tricky, because the nodes are so large. A penalty can only be applied to a whole node (triangle) at once, which usually doesn’t represent the shape you actually want.
When using penalties, grid graphs are usually more suitable.
If you want to use penalties with recast graphs, I would recommend that you use the 5.4.1 beta since it has significant improvements for penalties on recast graphs.
To get around the triangle shape issue, I would recommend you to use tags instead of raw penalties, and apply them using either Recast Graph Settings → Per Layer Rules, or by setting a custom tag by attaching a RecastMeshObj component to an object and configuring it to set a tag. This will shape the tag region to the object.
On your Seeker/FollowerEntity you’ll then want to increase the “Cost Per Distance” setting for that specific tag.