I tried to put the IsAlive pool in both the scripts but this didn’t work. I don’t know what I’m doing wrong. Any help is appreciated!
//script to make the enemy follow the player
public class EnemyAI : MonoBehaviour
{
public Animator animator;
[Header("Pathfinding")]
public Transform target;
public float activateDistance = 50f;
public float pathUpdateSeconds = 0.5f;
[Header("Physycs")]
public float speed = 200f;
public float nextWaypointDistance = 3f;
[Header("Custom Behaviour")]
public bool followEnabled = true;
public bool directionLookEnabled = true;
private Path path;
private int currentWaypoint = 0;
bool isGrounded = false;
Seeker seeker;
Rigidbody2D rb;
public void Start()
{
seeker = GetComponent<Seeker>();
rb = GetComponent<Rigidbody2D>();
InvokeRepeating("UpdatePath", 0f, pathUpdateSeconds);
}
private void FixedUpdate()
{
if (TargetInDistance() && followEnabled)
{
PathFollow();
}
}
private void UpdatePath()
{
if (followEnabled && TargetInDistance() && seeker.IsDone())
{
seeker.StartPath(rb.position, target.position, OnPathComplete);
}
}
private void PathFollow()
{
if (path == null)
{
return;
}
if (currentWaypoint >= path.vectorPath.Count)
{
return;
}
isGrounded = Physics2D.Raycast(transform.position, -Vector3.up, GetComponent<Collider2D>().bounds.extents.y);
Vector2 direction = ((Vector2)path.vectorPath[currentWaypoint] - rb.position).normalized;
Vector2 force = direction * speed * Time.deltaTime;
rb.AddForce(force);
float distance = Vector2.Distance(rb.position, path.vectorPath[currentWaypoint]);
if (distance < nextWaypointDistance)
{
currentWaypoint++;
}
if (directionLookEnabled)
{
if (rb.velocity.x > 0f)
{
transform.localScale = new Vector3(-1f * Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
else if (rb.velocity.y < 0.05f)
{
transform.localScale = new Vector3(Mathf.Abs(transform.localScale.x), transform.localScale.y, transform.localScale.z);
}
}
}
private bool TargetInDistance()
{
return Vector2.Distance(transform.position, target.transform.position) < activateDistance;
}
private void OnPathComplete(Path p)
{
if (!p.error)
{
path = p;
currentWaypoint = 0;
}
}
void Update()
{
bool followEnabled = EnemyHealth.IsAlive;
animator.SetFloat("Speed", Mathf.Abs(speed));
}
//script that manages the Die() function and the enemy health
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public Animator animator;
public int maxHealth = 100;
int currentHealth;
public static bool IsAlive = true;
void Start()
{
currentHealth = maxHealth;
IsAlive = true;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
animator.SetTrigger("Hurt");
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy died");
animator.SetBool("IsDead", true);
IsAlive = false;
GetComponent<Collider2D>().enabled = false;
this.enabled = false;
}
}