I have a project where I’m utilizing A* algorithm. Initially I was using integer positions for grid nodes, but later I made one that uses float positions. Since then, I tested it with a unit, and an if block in one function executes though the condition should be false. The function is this:
List<FloatCell> GetNeighbors(FloatCell of)
{
List<FloatCell> neighbors = new List<FloatCell>();
if (of.x - TerrainGenerator.CELL_SIZE >= 0)
{
FloatCell left = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y);
if (left.walkable)
{
neighbors.Add(left);
}
if (of.y - TerrainGenerator.CELL_SIZE >= 0)
{
FloatCell lowerLeft = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y - TerrainGenerator.CELL_SIZE);
if (lowerLeft.walkable)
{
neighbors.Add(lowerLeft);
}
}
if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
{
Debug.Log((of.y+TerrainGenerator.CELL_SIZE)+" lesser than "+height*TerrainGenerator.CELL_SIZE);
FloatCell upperLeft = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y + TerrainGenerator.CELL_SIZE);
if (upperLeft.walkable)
{
neighbors.Add(upperLeft);
}
}
}
if (of.x + TerrainGenerator.CELL_SIZE < width*TerrainGenerator.CELL_SIZE)
{
FloatCell right = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y);
if (right.walkable)
{
neighbors.Add(right);
}
if (of.y - TerrainGenerator.CELL_SIZE >= 0)
{
FloatCell lowerRight = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y - TerrainGenerator.CELL_SIZE);
if (lowerRight.walkable)
{
neighbors.Add(lowerRight);
}
}
if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
{
FloatCell upperRight = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y + TerrainGenerator.CELL_SIZE);
if (upperRight.walkable)
{
neighbors.Add(upperRight);
}
}
}
if (of.y - TerrainGenerator.CELL_SIZE >= 0)
{
FloatCell lower = GetCell(of.x, of.y - TerrainGenerator.CELL_SIZE);
if (lower.walkable)
{
neighbors.Add(lower);
}
}
if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
{
FloatCell upper = GetCell(of.x, of.y + TerrainGenerator.CELL_SIZE);
if (upper.walkable)
{
neighbors.Add(upper);
}
}
return neighbors;
}
The condition is on the line 19. Float cell is following:
public class FloatCell
{
public bool walkable = true;
public float x, y;
public int hCost, gCost, fCost;
public FloatCell previous;
public FloatCell(float x,float y)
{
this.x = x;
this.y = y;
}
public void CalculateFCost()
{
fCost = hCost + gCost;
}
public override string ToString()
{
return "X "+x+", Y "+y;
}
}
And here is a screenshot with log output: