Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved A* Pathfinding offset problem

Discussion in 'Scripting' started by Monique_Dumont, Dec 1, 2021.

  1. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello !

    I've implemented and modified an A* algorithm following this tutorial :

    .

    Everything is working as intended except for a single bug that's driving me crazy.

    Here is the bug in video :



    What is happening is that when I click on a cell, it gets a green outline and the robot is supposed to go to that cell. But if I click on the left part of the cell, it goes one cell shorter.
    And that's only when the robot.transform.position.x is positive, because when negative it has the same behaviour but in the other way. Meaning I click on the right part of the cell, it goes one cell shorter. Same with robot.transform.position.z.
    Finally, this "deadzone" grows the more I approach the extremities of the map. At the last cell, that deadzone is roughly half of a cell.

    I identified a rounding issue in this part of the code :

    Code (CSharp):
    1.     public Node NodeFromWorldPoint(Vector3 worldPosition)
    2.     {
    3.         float percentX = (worldPosition.x + gridWorldSize / 2) / gridWorldSize;
    4.         float percentY = (worldPosition.z + gridWorldSize / 2) / gridWorldSize;
    5.  
    6.         percentX = Mathf.Clamp01(percentX);
    7.         percentY = Mathf.Clamp01(percentY);
    8.  
    9.         int x = Mathf.RoundToInt((gridSizeX - 1) * percentX);
    10.         int y = Mathf.RoundToInt((gridSizeY - 1) * percentY);
    11.  
    12.         return grid[x, y];
    13.     }
    The source coder (Sebastian Lague) uses percentages to know where we are on the 2D array based on the world position.

    I hope someone can help because I'm already 6 hours in on this bug and I don't have a single clue about what is causing it.


    Edit : I'm stupid, someone has already solved this issue in the comment of one of the creator's video. Here is his solution for posterity sake :

    Code (CSharp):
    1.     public Node NodeFromWorldPoint(Vector3 worldPosition)
    2.     {
    3.         float percentX = (worldPosition.x + gridWorldSize / 2) / gridWorldSize;
    4.         float percentY = (worldPosition.z + gridWorldSize / 2) / gridWorldSize;
    5.  
    6.         int x = Mathf.FloorToInt(Mathf.Clamp((gridSizeX) * percentX, 0, gridSizeX - 1));
    7.         int y = Mathf.FloorToInt(Mathf.Clamp((gridSizeY) * percentY, 0, gridSizeY - 1));
    8.  
    9.  
    10.         return grid[x, y];
    11.     }
     
    Last edited: Dec 1, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Jsut make sure you are using the same exact code to determine which node to send the robot to as you are using to determine which node to highlight. If you're using different code, that's a window for different behavior.
     
  3. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    In fact I'm not highlighting the node, I'm just checking where the mouse is and draw a box around it. But that's just here to show the problem.
     
  4. Selkiesoul

    Selkiesoul

    Joined:
    Mar 11, 2022
    Posts:
    1
    I was experiencing a similar(ish) problem where there was a deadzone when the target was between a certain configuration of collision obstacles. The path to it was marked as walkable, but whenever the target was in this area no new paths were being generated. Your solution here fixed it for me! And I wasn't able to find the original comment on his video so thanks so much for the solution post :D