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

A* pathfinding project [extension] problems - seeker rubs wall and is in unwalkable

Discussion in 'Scripting' started by 1337GameDev, Dec 24, 2012.

  1. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I have a scene that is based on the example. I just have capsule with the seeker script, an AITester script (code provided below) and i have layers for obstacles and ground. I have my a star object with the pathfinder script and am doing a grid graph. The graph scans fine and shows unwalkable areas, but then when i run the scene, my seeker seems to walk on these and get stuck trying to go through a wall.

    The attached images show my AI settings and my scene layout with my ai graph that is generated. The green line is the path generated, and my ai (the capsule) just moves on the line, but then "rubs" up against the wall until it slides sideways enough to follow the path closer, then it rubs the path again when it is closer to the goal and has to round a corner. My ai is crossing nodes that are marked as unwalkable and i am confused why. There aren't even any nodes here.

    My AI settings (same as example set up guide)
    $settingsForAI.png

    My Ai graph with the wall selected (the wall my ai rubs against)

    $aiGraph.png

    My scene geometry
    $aiGraphWithGeometry.png



    My goal is on the right and my ai moves left (to end of green line of the path it calculated).

    The first spot my ai rubs against
    $aiRubsWall.png
    The second spot my ai rubs against
    $aiRubsWall2.png

    What could be causing this, and how do i make the ai not rub up against the wall and actually stay on the graph as a* is meant to do?
     
  2. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Well your path seems right. Need some code though - maybe the code of how you move your char.
     
  3. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    oh whoops, i forgot to include my code:
    here is its:

    The variables and gameobjects are being found, so no null references, and i have made my script variables private and public again (to avoid inspector overriding values).

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. using Pathfinding;
    7.  
    8. public class testAStar : MonoBehaviour
    9. {
    10.    
    11.     //The point to move to
    12.     public Vector3 targetPosition;
    13.     public Vector3 targetDefaultPos = new Vector3(0,0,0);
    14.     public GameObject targetObj;
    15.    
    16.     public Seeker seeker;
    17.    
    18.     public CharacterController controller;
    19.    
    20.     //The calculated path
    21.     public Path path;
    22.    
    23.     //The AI's speed per second
    24.     public float speed = 100;
    25.    
    26.     //The max distance from the AI to a waypoint for it to continue to the next waypoint
    27.     public float nextWaypointDistance = 3;
    28.  
    29.     //The waypoint we are currently moving towards
    30.     private int currentWaypoint = 0;
    31.  
    32.     public void Awake()
    33.     {
    34.         targetObj = (GameObject)GameObject.Find("target");
    35.     }
    36.    
    37.     public void Start()
    38.     {
    39.         //Get a reference to the Seeker component we added earlier
    40.         seeker = GetComponent<Seeker>();
    41.         controller = GetComponent<CharacterController>();
    42.         if(targetDefaultPos.Equals(Vector3.zero) )
    43.         {
    44.             targetPosition = targetObj.transform.position;
    45.         }
    46.         else
    47.         {
    48.             targetPosition = targetDefaultPos;
    49.         }
    50.         //Start a new path to the targetPosition, return the result to the OnPathComplete function
    51.         seeker.StartPath(transform.position,targetPosition, OnPathComplete);
    52.     }
    53.    
    54.     public void OnPathComplete(Path p)
    55.     {
    56.         Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
    57.         if (!p.error)
    58.         {
    59.             path = p;
    60.             //Reset the waypoint counter
    61.             currentWaypoint = 0;
    62.         }
    63.        
    64.     }
    65.  
    66.     public void FixedUpdate()
    67.     {
    68.         if (path == null)
    69.         {
    70.             //We have no path to move after yet
    71.             return;
    72.         }
    73.        
    74.         if (currentWaypoint >= path.vectorPath.Length)
    75.         {
    76.             Debug.Log ("End Of Path Reached");
    77.             return;
    78.         }
    79.        
    80.         //Direction to the next waypoint
    81.         Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
    82.         dir *= speed * Time.fixedDeltaTime;
    83.         controller.SimpleMove(dir);
    84.        
    85.         //Check if we are close enough to the next waypoint
    86.         //If we are, proceed to follow the next waypoint
    87.         if (Vector3.Distance(transform.position,path.vectorPath[currentWaypoint] ) < nextWaypointDistance )
    88.         {
    89.             currentWaypoint++;
    90.             return;
    91.         }
    92.        
    93.     }
    94.    
    95.     public void Update()
    96.     {
    97.        
    98.     }
    99.    
    100. }
    101.  
    102.  
     
  4. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Looks alright. But 3 is a pretty big distance for next way point. Try 0.5f or using math approx
     
  5. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I created another scene with the same setup (an a*star object, a seeker script and obstacles and ground labels. The only thing different is in the script for testing the ai, i changed:
    Code (csharp):
    1. public float nextWaypointDistance = 3;
    to

    Code (csharp):
    1. public float nextWaypointDistance = 0.5f;
    As of current my AI seems to not follow the grid nodes and runs straight into the wall. In the below images the first shows my scene and my start and goal obstacles.
    In the second image, i show the game path and where my ai gets stuck (the highlighted capsule object).
    Again, my path that is calculated and my ai should follow is highlighted in green.

    $AIPathError.png

    I also have a problem with my ai pausing for a very long time before moving. It tells me in the terminal debugger that the path took 60ms to calculate, but then my ai doesnt start moving till a minute later. If i hit play and let it calculate the path and then stop the editor preview, and then play again, my ai moves instantly, showing that it took 60ms again to calculate the path. Why could this be?
     
    Last edited: Dec 28, 2012
  6. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    I do have an idea of why it runs into the wall. But first i need to know how do you trace back the path? You find the end node and then trace its parents? Dont know why the second thing occur.
     
  7. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    I did not trace the path, by following nodes or with some extra script, i just ran my scene in the editor and while it was running i viewed it in scene view (not game view or it wont show up). The framework generates this green highlighted path at runtime for the scene view only. I assume this path is correct as it is part of the framework and not my own code.

    And what could cause these? And how could i try and debug them?
     
  8. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Seems like it is heading directly for the end node..
     
  9. 1337GameDev

    1337GameDev

    Joined:
    Oct 31, 2011
    Posts:
    54
    The ai follows a curve and then doesnt go far enough above the wall and it "slides" back down towards the goal along the wall. Here's three images to show where the ai gets stuck on the wall and starts to slide down:

    $stuckOnWall.png

    Here is a video showing the problem:
    http://www.youtube.com/watch?v=A4OKhCYRONs&feature=youtu.be

    My hierarchy for my ai object (the capsule the moves) is this:

    [Empty gameobject] (parent) -- has components: seeker, CharacterController
    |-[Capsule Gameobject] (child) -- has components: testAStar script
     
    Last edited: Jan 4, 2013
  10. Bungalo

    Bungalo

    Joined:
    Mar 15, 2013
    Posts:
    1
    I have the exact same problem. Any help would be appreciated since I have no idea how to fix it. The green line which is supposed to be the calculated route is drawn correctly, but the object just isn't following it.
     
  11. Mops

    Mops

    Joined:
    Oct 27, 2012
    Posts:
    1
    I was having a similar problem. I was using a terrain, not a plane. The seeker was walking straight into unwalkable nodes. Upon inspection, the Path.vector array only had two nodes, the start point and the finish point. Root cause: the max climb value was set to 0.1 (default, maybe? Or maybe I put it in, no clue), which broke up the nav grid into many small areas, with no indication of that showing in the editor. Setting the max climb to 10 or something and pressing "scan" solved the issue for me.

    The max climb thing is not a bug by itself, but it's a pretty big deal that you don't see the exclusive areas in the editor. What is a bug is the seeker going through unwalkables when this happens (doesn't always happen, sometimes the proper error is returned).
     
  12. kdogg_49

    kdogg_49

    Joined:
    Nov 20, 2019
    Posts:
    1
    I'm guessing this thread is long dead and the problem has been solved however I came accross this as my seeker was exhibiting the same issues. I resolved it by changing the Seekers 'Traversable Graphs' to only my graph rather than the default which is all.
     
    ZhavShaw likes this.
  13. thaitai192

    thaitai192

    Joined:
    Mar 15, 2017
    Posts:
    1
    Fixed by In AIPath, change Pick Next Way Point Dist from 2 to 0.5
     
    stealthyshiroean likes this.