Enemy AI not following or moving towards player

Updated on January 17, 2017 in [A] 2D
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
2 on January 10, 2017

I’ve done a lot of searching, and none of the fixes have seemed to be relevant for what’s working for me. I’m not even sure the issue is in the enemy AI script, since i’ve copied and tried several i’ve seen posted on here. Gizmo shows enemy drawing path, but not following it. The only error i’m seeing is: “cancelled path because a new one was selected”

Code for enemy AI script:

using UnityEngine;
using System.Collections;
using Pathfinding;
[RequireComponent (typeof (Rigidbody2D))]
[RequireComponent (typeof (Seeker))]
public class EnemyAI : MonoBehaviour {
// What to chase?
public Transform target;
// How many times each second we will update our path
public float updateRate = 2f;
// Caching
private Seeker seeker;
private Rigidbody2D rb;
//The calculated path
public Path path;
//The AI’s speed per second
public float speed = 1000f;
public ForceMode2D fMode;
public bool pathIsEnded = false;
// The 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;
private bool searchingForPlayer = false;
void Start () {
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
if (target == null) {
if(!searchingForPlayer){
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
StartCoroutine (UpdatePath());
}
IEnumerator SearchForPlayer(){
GameObject sResult = GameObject.FindGameObjectWithTag(“Player”);
if(sResult == null){
yield return new WaitForSeconds(0.5f);
StartCoroutine(SearchForPlayer());
}else{
target = sResult.transform;
searchingForPlayer = false;
StartCoroutine(UpdatePath());
}
}
IEnumerator UpdatePath() {
if (target == null) {
if(!searchingForPlayer){
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
yield break;
}
// 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) {
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}
void FixedUpdate () {
if (target == null) {
if(!searchingForPlayer){
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
//TODO: Always look at player?
if (path == null)
return;
if (currentWaypoint >= path.vectorPath.Count) {
if (pathIsEnded)
return;
// I add this part. When reach the end we gave it players new position.
StartCoroutine(SearchForPlayer());
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 January 11, 2017

You should try to stop the UpdatePath() coroutine when the path has been created. If I’m right it will endlessly loop itself, which might create your error. Also the problem could be in your “seeker” script.

  • Liked by
Reply
Cancel
0 on January 17, 2017

I’m pretty new to this, where should I put the stop in? Would increasing the Waitforseconds do anything? I’m just wondering because it seems like if the script works It should stop automatically, where based on my error it definitely isn’t. Sorry, i’m really new to coding and eventually found myself in here when I couldn’t fix this myself.

  • Liked by
Reply
Cancel