Hello,
I have just started using this great project, but am having difficulty killing a path.
Basically I have a game object “Child” which is trying to reach the end of a maze before the “Enemy” object can reach it. I am testing with one child game object for now, but I will have multiple. If the children reach the safe zone (game object with circle collider), then I destroy the child gameobject because they are safe.
The problem I have at the moment is that when my child gameobject reaches the safe zone and is destroyed, the Enemy still keeps going on the path to the last known position of the child.
Is there a way to kill the path when the target object is null? I can see from the Debug.Log I have that target is updated to null, but the enemy still continues on the path.
The enemy object has a Seeker component and 2 scripts, below:
Enemy_AI - used to get closest target, update path and destroy child object on trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class Enemy_AI : MonoBehaviour {
public Transform target;
private const int IsometricRangePerYUnit = 100;
Path path;
int currentWaypoint = 0;
Seeker seeker;
Rigidbody2D rb;
public Level_Config levelConfig;
void Start() {
levelConfig = GameObject.Find("Level_Config").GetComponent<Level_Config>();
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("UpdatePath", 0f, 0.5f);
}
void FindClosestChild() {
float distanceToClosestChild = Mathf.Infinity;
Child_AI closestChild = null;
Child_AI[] allChildren = GameObject.FindObjectsOfType<Child_AI>();
Debug.Log(target);
foreach (Child_AI currentChild in allChildren) {
float distancetoChild = (currentChild.transform.position - this.transform.position).sqrMagnitude;
if (distancetoChild < distanceToClosestChild) {
distanceToClosestChild = distancetoChild;
closestChild = currentChild;
target = closestChild.GetComponent<Transform>();
}
}
}
void UpdatePath() {
if (seeker.IsDone() && target != null) {
seeker.StartPath(rb.position, target.position, OnPathComplete);
}
else if (target == null) {
seeker.GetCurrentPath().Error();
path = null;
}
}
void OnPathComplete(Path p) {
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}
void Update() {
FindClosestChild();
// Render player in front / behind objects based on Y axis.
SpriteRenderer renderer = GetComponent<SpriteRenderer>();
renderer.sortingOrder = -(int)(transform.position.y * IsometricRangePerYUnit);
}
public void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.tag == "Child") {
levelConfig.currentCount--;
Destroy(other.gameObject);
}
}
}
Enemy_AIPath - used for 4-directional sprite animation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Pathfinding;
public class Enemy_AIPath : AIPath {
public Animator anim;
protected override void Start() {
anim = GetComponent<Animator>();
}
protected override void Update() {
base.Update();
if (Mathf.Abs(velocity.x) > Mathf.Abs(velocity.y)) {
if (velocity.x > 0) {
anim.Play("Enemy_Right");
}
else {
anim.Play("Enemy_Left");
}
}
else {
if (velocity.y > 0) {
anim.Play("Enemy_Up");
}
else {
anim.Play("Enemy_Down");
}
}
}
}
Any help with this would be greatly appreciated.
Many Thanks,
John