Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question If block executes when it shouldn't

Discussion in 'Scripting' started by Krosenut, Nov 30, 2023.

  1. Krosenut

    Krosenut

    Joined:
    Jun 1, 2023
    Posts:
    20
    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:

    Code (CSharp):
    1. List<FloatCell> GetNeighbors(FloatCell of)
    2. {
    3.     List<FloatCell> neighbors = new List<FloatCell>();
    4.     if (of.x - TerrainGenerator.CELL_SIZE >= 0)
    5.     {
    6.         FloatCell left = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y);
    7.         if (left.walkable)
    8.         {
    9.             neighbors.Add(left);
    10.         }
    11.         if (of.y - TerrainGenerator.CELL_SIZE >= 0)
    12.         {
    13.             FloatCell lowerLeft = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y - TerrainGenerator.CELL_SIZE);
    14.             if (lowerLeft.walkable)
    15.             {
    16.                 neighbors.Add(lowerLeft);
    17.             }
    18.         }
    19.         if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
    20.         {
    21.             Debug.Log((of.y+TerrainGenerator.CELL_SIZE)+" lesser than "+height*TerrainGenerator.CELL_SIZE);
    22.             FloatCell upperLeft = GetCell(of.x - TerrainGenerator.CELL_SIZE, of.y + TerrainGenerator.CELL_SIZE);
    23.             if (upperLeft.walkable)
    24.             {
    25.                 neighbors.Add(upperLeft);
    26.             }
    27.         }
    28.     }
    29.  
    30.     if (of.x + TerrainGenerator.CELL_SIZE < width*TerrainGenerator.CELL_SIZE)
    31.     {
    32.         FloatCell right = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y);
    33.         if (right.walkable)
    34.         {
    35.             neighbors.Add(right);
    36.         }
    37.         if (of.y - TerrainGenerator.CELL_SIZE >= 0)
    38.         {
    39.             FloatCell lowerRight = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y - TerrainGenerator.CELL_SIZE);
    40.             if (lowerRight.walkable)
    41.             {
    42.                 neighbors.Add(lowerRight);
    43.             }
    44.         }
    45.         if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
    46.         {
    47.             FloatCell upperRight = GetCell(of.x + TerrainGenerator.CELL_SIZE, of.y + TerrainGenerator.CELL_SIZE);
    48.             if (upperRight.walkable)
    49.             {
    50.                 neighbors.Add(upperRight);
    51.             }
    52.         }
    53.     }
    54.     if (of.y - TerrainGenerator.CELL_SIZE >= 0)
    55.     {
    56.         FloatCell lower = GetCell(of.x, of.y - TerrainGenerator.CELL_SIZE);
    57.         if (lower.walkable)
    58.         {
    59.             neighbors.Add(lower);
    60.         }
    61.     }
    62.     if (of.y + TerrainGenerator.CELL_SIZE < height*TerrainGenerator.CELL_SIZE)
    63.     {
    64.         FloatCell upper = GetCell(of.x, of.y + TerrainGenerator.CELL_SIZE);
    65.         if (upper.walkable)
    66.         {
    67.             neighbors.Add(upper);
    68.         }
    69.     }
    70.     return neighbors;
    71. }
    The condition is on the line 19. Float cell is following:

    Code (CSharp):
    1. public class FloatCell
    2. {
    3.     public bool walkable = true;
    4.     public float x, y;
    5.     public int hCost, gCost, fCost;
    6.     public FloatCell previous;
    7.  
    8.     public FloatCell(float x,float y)
    9.     {
    10.         this.x = x;
    11.         this.y = y;
    12.     }
    13.  
    14.     public void CalculateFCost()
    15.     {
    16.         fCost = hCost + gCost;
    17.     }
    18.  
    19.     public override string ToString()
    20.     {
    21.         return "X "+x+", Y "+y;
    22.     }
    23. }
    And here is a screenshot with log output:

     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,710