- Home /
Enemy AI not Animating properly and acting strangely.,Enemy AI animations only playing on 1 unit.
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, still rotate to follow the player, and are still killable. 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 for android.,I have created an AI script based on a youtube tutorial and have modified to my purpose. But for some reason the animator only works on the first one i place in the scene. all others only play the default idle anim. Also after killing the first one the rest of the others wont follow the player anymore and wont attack anymore, but they still rotate to look at the player and still play the default idle anim.
Here is my script 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 this is the other script on the AI that deals with health
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 and all help would be appreciated.
Your answer
Follow this Question
Related Questions
In-App-Purchases not working (Android) 1 Answer
Video Player preparing too long Android 0 Answers
Why can't I see how I rewind time? 0 Answers
Android port bug? 1 Answer
Character Wont Stop Moving 1 Answer