Search Unity

Problem with A* pathfinding c#

Discussion in 'Scripting' started by jamstorr86, Sep 17, 2014.

  1. jamstorr86

    jamstorr86

    Joined:
    Sep 5, 2014
    Posts:
    8
    Hi guys, I'm having great difficulties calculating a new path, and then implementing a new path when I call an updatepath method in my script. Can someone please point me in the right direction?

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using Pathfinding;

    Code (CSharp):
    1. public class Enemy_Move : MonoBehaviour {
    2.  
    3.     public Transform targetAtstart;
    4.     public Transform newTarget;
    5.     public Transform currentTarget;
    6.     public List<GameObject> targetList;
    7.     public float radius = 2f;
    8.     public float MoveSpeed = 5f;
    9.     private Vector3 velocity = Vector3.zero;
    10.     public float smoothTime = 1f;
    11.     public Path path;
    12.     public float nextWayPointdistance = 3;
    13.     private int currentWayPoint = 0;
    14.     private CharacterController controller;
    15.     private Seeker seeker;
    16.     private AstarPath astar;
    17.  
    18.  
    19.  
    20.     void Start ()
    21.     {
    22.         astar = GameObject.Find("A*").GetComponent<AstarPath>();
    23.         currentTarget = targetAtstart;
    24.         seeker = GetComponent<Seeker> ();
    25.         controller = GetComponent<CharacterController>();
    26.         seeker.StartPath (transform.position, currentTarget.position, OnPathComplete);
    27.              
    28.     }
    29.  
    30.  
    31.     public void FixedUpdate ()
    32.     {
    33.  
    34.  
    35.  
    36.         if (path == null) {
    37.             return;
    38.                 }
    39.         if (currentWayPoint >= path.vectorPath.Count)
    40.         {
    41.  
    42.             Debug.Log ("End of Path Reached");
    43.             return;
    44.         }
    45.  
    46.         Vector3 dir = (path.vectorPath [currentWayPoint] - transform.position).normalized;
    47.         dir *= MoveSpeed * Time.deltaTime;
    48.         controller.SimpleMove (dir);
    49.  
    50.         if (Vector3.Distance (transform.position, path.vectorPath [currentWayPoint]) < nextWayPointdistance)
    51.         {
    52.             currentWayPoint++;
    53.             return;
    54.         }
    55.  
    56.  
    57.  
    58.     }
    59.  
    60.     public void OnPathComplete(Path P)
    61.     {
    62.  
    63.         Debug.Log ("We got a path Back. " + P.error);
    64.         if (!P.error) {
    65.             path = P;
    66.             currentWayPoint = 0;
    67.                 }
    68.  
    69.     }
    70.  
    71.     public void UpdatePath()
    72.     {
    73.         currentTarget.position = targetAtstart.position;
    74.         astar.Scan();
    75.         //path = null;
    76.         seeker.StartPath(transform.position, currentTarget.position, OnPathComplete);
    77.  
    78.  
    79.  
    80.     }
    81.  
    82. }
     
    Last edited: Sep 17, 2014
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
  3. jamstorr86

    jamstorr86

    Joined:
    Sep 5, 2014
    Posts:
    8
    The problem occurs when the UpdatePath method is called. I believe the issue is linked to the astar.Scan() method called within UpdatePath. It updates the graph as expected, but a new path isn't created, and so the gameobject simply stops moving.
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Why are you scanning in the first place? This might be quite a heavy operation depending on your amount of nodes and graphs. If I remember correctly Scan() actually clears all nodes first and does not update nodes (which can be done with a different method), only then it creates new nodes. I can imagine that once the StartPath is called on the seeker, no nodes are available so no path can be computed.

    I think there was also an option in the A* settings to raise the error log level - maybe that give's you a clue in the console.

    Sorry I don't have my projects here where I am extensively using A* as well, so I cannot check with real code.
     
  5. jamstorr86

    jamstorr86

    Joined:
    Sep 5, 2014
    Posts:
    8
    Thanks for the feedback. I have seen some success when I use AstarPath.active.UpdateGraphs(gameobject.collider.bounds);
    I run this from a script attached to the building I am moving. It appends the nodes to the area in which the building has been placed. However, it doesn't change the nodes where the original building was back, so the area in which the building was originally located remains untraversable. If i can solve this, I may have worked it all out.
     
  6. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    I think what would help there is to create a new GraphUpdateObject (or re-use an existing) with the bounds of the building from before moving. Use that in UpdateGraphs to update the nodes at the original position.