AI with Astarpath doesn’t work: NPC not following

Updated on May 17, 2017 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on May 17, 2017

Hello guys,
I want to implement the Astarpathfinder, so an NPC follows the player.
I followed this tutorial.
I am using Unity 2017.1.0 Beta 2 and I didn’t get any errors in the code but it doesn’t work. The NPC isn’t moving at all. There was one option different in my Unity version than in the video: when he made the Grid Graph, he changed in the inspector the option “climb axis” from y to z (12:35 min). I can’t find this option in my version though. Could this be the problem why my NPC doesn’t follow?
Screenshot of my inspector (Grid Graph)

Or do you have any other ideas? Did I do anything wrong?
Thank you very much!

   using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using Pathfinding;
     [RequireComponent (typeof (Rigidbody2D))]
     [RequireComponent (typeof (Seeker))]
 public class TeamAI : MonoBehaviour {
     
     //What to chase?
     public Transform target;
     //How many times each second we will update our path
     public float updateRate = 2f;
     //Catching
     private Seeker seeker;
     private Rigidbody2D rb;
     //The calculated path
     public Path path;
     //The AI`s speed per second
     public float speed = 300f;
     public ForceMode2D fMode;
     [HideInInspector]
     public bool pathIsEnded = false;
     //max distance from the AI to a waypoint for it to continue to the next waypoint
     public float nextWaypointDistance = 3;
     //The waypoint we are currently moving towards
     private int currentWaypoint = 0;
     void Start () {
         seeker = GetComponent<Seeker>();
         rb = GetComponent<Rigidbody2D>();
         if (target == null) {
             Debug.LogError ("No Player found? PANIC!");
             return;}
         //Start a new path to the target position, return the result to the OnPathComplete method
         seeker.StartPath (transform.position, target.position, OnPathComplete);
         StartCoroutine (UpdatePath ());
     }
     IEnumerator UpdatePath () {
         if (target == null) {
             yield return false;
         }
         //start a new path to the target position, return the result to the OnPathComplete method
         seeker.StartPath (transform.position, target.position, OnPathComplete);
         yield return new WaitForSeconds (1f / updateRate);
         StartCoroutine (UpdatePath ());
     }
     public void OnPathComplete(Path p) {
         Debug.Log ("We got a path. Did it have an error?" + p.error);
         if (!path.error) {
             path = p;    
             currentWaypoint = 0;
         }
     }
         void FixedUpdate (){
             if (target == null) {
             //player search here
             return;
         }
         //always look at player?
             if (path == null) {
                 return;
             if (currentWaypoint >= path.vectorPath.Count){
                 if (pathIsEnded)
                     return;
                 Debug.Log ("End of path reached.");
                 pathIsEnded = true;
                 return;
             }
             pathIsEnded = false;
         }
         //Direction to the next waypoint
         Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position ).normalized;
         dir *= speed * Time.fixedDeltaTime;
         //Move the AI
         rb.AddForce (dir, fMode);
         float dist = Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]);
         if (dist < nextWaypointDistance) {
             currentWaypoint++;
             return;
         }
     }
 }
  

  • Liked by
Reply
0 on May 17, 2017

Oh, dear.

  • Liked by
Reply
Cancel