I created an enemy AI based on a youtube tutorial and modified it to my requirements.
My problem is the AI only animates on the first one i place in the scene. All others only play the default idle anim. After killing the original unit all other stop following the player and stop attacking, but they continue to play the default anim and still rotate to follow the player. Which is very perplexing.
Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Chase : MonoBehaviour
{
public GameObject EnemyRay;
public float damage = 10f;
public float range = 1f;
public float detectAngle = 30f;
public float aiCoolDown = 2f;
public float aiCoolDownTimer;
public Transform player;
static Animator anim;
[SerializeField] private float detectDistance = 10;
[SerializeField] private float followDistance = 1;
[SerializeField] private float attackDistance = 1;
[SerializeField] float moveSpeed = 1f;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
//Attack Clock
if (aiCoolDownTimer > 0)
{
aiCoolDownTimer -= Time.deltaTime;
}
if (aiCoolDownTimer < 0)
{
aiCoolDownTimer = 0;
}
//============================================================================================================
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < detectDistance && angle < detectAngle)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
//Ai movement
anim.SetBool("IsIdle", false);
if(direction.magnitude > followDistance)
{
this.transform.Translate(0, 0, 0.05f * moveSpeed);
anim.SetBool("IsMoving", true);
anim.SetBool("IsAttacking", false);
}
else
{
anim.SetBool("IsMoving", false);
}
//Ai Attacking
if (direction.magnitude < attackDistance && aiCoolDownTimer == 0)
{
Attack();
aiCoolDownTimer = aiCoolDown;
Debug.Log("ai timer started");
}
}
else
{
anim.SetBool("IsIdle", true);
anim.SetBool("IsMoving", false);
anim.SetBool("IsAttacking", false);
}
}
void Attack()
{
RaycastHit hit;
if (Physics.Raycast(EnemyRay.transform.position, EnemyRay.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
anim.SetBool("IsAttacking", true);
PlayerMovement target = hit.transform.GetComponent<PlayerMovement>();
if(target != null)
{
target.PlayerTakeDamage(damage);
}
}
}
}
And here is the health script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float health = 100f;
public PlayerMovement playerMovement;
// Use this for initialization
void Start ()
{
playerMovement = FindObjectOfType<PlayerMovement>();
}
// Update is called once per frame
void Update ()
{
if(health == 0)
{
Die();
}
}
public void OnTriggerEnter(Collider col)
{
if(col.gameObject.name == "SwordCollider")
{
health -= playerMovement.damage;
}
}
void Die()
{
Destroy(gameObject);
}
}
Any help with this would be very appreciated.
PS this is an Android game.