AI stops following player.
I've made an AI in my game to follow the player but for some reason, after reaching the first waypoint the AI's path isn't update and it just stops. Please help me. Here is the AI. using UnityEngine; using Pathfinding; using System.Collections;
[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(Seeker))]
public class FlyingEyeballAI : MonoBehaviour
{
public bool AutomaticallyAware = false;
private Vector3 v_diff;
private float atan2;
private int rotationSpeed = 1;
public Transform target;
public float updateRate = 15f;
private Seeker seeker;
private Rigidbody2D rb2d;
public Path path;
public float speed = 300f;
public ForceMode2D fMode;
[HideInInspector]
public bool pathIsEnded = false;
public float nextWayPointDistance = 3;
private int currentWaypoint = 0;
private bool searchingForPlayer = false;
public bool idle;
private PolygonCollider2D visionCone;
void Start()
{
/* if(AutomaticallyAware == true)
{
idle = false;
}
else
{
idle = true;
}*/
visionCone = GetComponentInChildren<PolygonCollider2D>();
seeker = GetComponent<Seeker>();
rb2d = GetComponent<Rigidbody2D>();
if(target == null)
{
if(!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
seeker.StartPath(transform.position, target.position, OnPathComplete);
StartCoroutine(UpdatePath());
}
IEnumerator UpdatePath()
{
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return false;
}
seeker.StartPath(transform.position, target.position, OnPathComplete);
yield return new WaitForSeconds(1 / updateRate);
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());
return false;
}
}
public void OnPathComplete(Path p)
{
if(!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void Update()
{
v_diff = (target.position - transform.position);
atan2 = Mathf.Atan2(v_diff.y, v_diff.x);
transform.rotation = Quaternion.Euler(0f, 0f, atan2 * Mathf.Rad2Deg);
}
void FixedUpdate()
{
if (target == null)
{
if (!searchingForPlayer)
{
searchingForPlayer = true;
StartCoroutine(SearchForPlayer());
}
return;
}
if (path == null)
{
return;
}
if (currentWaypoint >= path.vectorPath.Count)
{
if (pathIsEnded)
{
return;
}
StartCoroutine(SearchForPlayer());
pathIsEnded = true;
return;
}
pathIsEnded = false;
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
if (idle == false)
{
rb2d.AddForce(dir, fMode);
}
float dist = Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]);
if (dist < nextWayPointDistance)
{
currentWaypoint++;
return;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
2D Enemy Ai 0 Answers
How can I make a 4 direction animation for an enemy? 1 Answer
Vector 2 only using x (Enemy AI) 1 Answer
Trouble making jumping spider enemies 0 Answers