- Home /
Problem is not reproducible or outdated
A* pathfinding - incorrect nodes calculated
All my scripts find the correct vector3 position for both the start and end position however when the nodes to use for these positions are calculated they always seem to be offset by 5 nodes in the z direction.
I have checked the maths and it all works out, yet the incorrect nodes are still calculated.
public Node NodeFromWorldPoint(Vector3 worldPosition) {
float percentX = (worldPosition.x + gridWorldSize.x/2) / gridWorldSize.x;
float percentY = (worldPosition.z + gridWorldSize.y/2) / gridWorldSize.y;
percentX = Mathf.Clamp01(percentX);
percentY = Mathf.Clamp01(percentY);
int x = Mathf.RoundToInt((gridSizeX-1) * percentX);
int y = Mathf.RoundToInt((gridSizeY-1) * percentY);
return grid[x,y];
}
The error must be in here yet after days and days of working on this one issue I can't find it. Any ideas would be greatly appreciated.
What are your values for gridWorldSize and gridSizeX, gridSizeY? Also what's the type of those variables?
gridWorldSize.x = 80, gridWorldSize.y = 160. gridWorldSize is a Vector2, x and y are floats.
gridSizeX = 40 and gridSizeY = 80 and they are int values derived from gridWorldSize / nodeDiameter where nodeDiameter = 2.
Should
float percentY = (worldPosition.z + gridWorldSize.y/2) / gridWorldSize.y;
be
float percentY = (worldPosition.y + gridWorldSize.y/2) / gridWorldSize.y;
?
I'll try it but as I understand, I need to use z as gridWorldSize is a Vector2 and worldPosition is a Vector3.
Answer by ltlead · Feb 09, 2018 at 01:53 PM
Fixed it. After trawling through and rewriting all of my code with the persisting problem, my A* gameobject transform was (0, 0, 9), resulting in a difference of 9 units between my scripts and hence 5 nodes.
Follow this Question
Related Questions
A* endless loop 0 Answers
A* Pathfinding Turn Off Rotation 2 Answers
How can i take the right path in this pathfind algorithm? 0 Answers
A* pathfinding generating new path when created new obstacle 0 Answers
Custom pathfinding and node-values 1 Answer