using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyManager : MonoBehaviour { public float health = 100; public float damage = 10; public float attackRate = 2; private float attackTime = 2; private float actualHealth; private NavMeshAgent navMeshAgent; private PlayerManager player; private BarricadeManager barricadeManager; public List bodyParts = new List(); // Start is called before the first frame update void Start() { attackTime = attackRate; actualHealth = health; player = FindObjectOfType(); navMeshAgent = GetComponent(); } // Update is called once per frame void Update() { DropBodyPart(); if (actualHealth <= 0) { Destroy(this.gameObject); } DestroyBarricades(); if (navMeshAgent != null && barricadeManager == null) { navMeshAgent.SetDestination(player.transform.position); } else if (navMeshAgent != null && barricadeManager != null) { navMeshAgent.SetDestination(barricadeManager.transform.position); } } void FixedUpdate() { if (actualHealth > 0 && barricadeManager == null) { Vector3 _dir = player.transform.position - transform.position; _dir.Normalize(); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime); } else if (actualHealth > 0 && barricadeManager != null) { Vector3 _dir = barricadeManager.transform.position - transform.position; _dir.Normalize(); transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_dir), 5 * Time.deltaTime); } } public void Damage() { WeaponManager weaponManager = player.weaponList[player.selectedWeapon].GetComponent(); if (weaponManager != null) { actualHealth -= weaponManager.damage; } } void DestroyBarricades() { Debug.Log(navMeshAgent.pathPending); if (navMeshAgent.pathStatus == NavMeshPathStatus.PathInvalid) { BarricadeManager[] barricadeManagers = FindObjectsOfType(); foreach(BarricadeManager localBarricadeManager in barricadeManagers) { if (barricadeManager == null) { barricadeManager = localBarricadeManager; } else { if (Vector3.Distance(barricadeManager.transform.position, transform.position) > Vector3.Distance(localBarricadeManager.transform.position, transform.position)) { barricadeManager = localBarricadeManager; } } } } } void DropBodyPart() { if (bodyParts.Count > 0) { float number = health / (bodyParts.Count + 1); for (int i = 0; i < bodyParts.Count; i++) { if ((health - actualHealth) / number > i) { if (bodyParts[i] != null && bodyParts[i].transform.parent != null) { bodyParts[i].transform.parent = null; bodyParts[i].AddComponent(); Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent() as Rigidbody; bodyParts[i].AddComponent(); MeshCollider meshCollider = bodyParts[i].GetComponent() as MeshCollider; meshCollider.convex = true; if (i == bodyParts.Count - 1) { navMeshAgent.isStopped = true; navMeshAgent.enabled = false; navMeshAgent = null; rigidBodyEnemy.velocity = bodyParts[i].transform.forward; } rigidBodyEnemy.mass = 10 * bodyParts.Count; Destroy(bodyParts[i], 4); } } } } } void Attack() { RaycastHit hit; if (Physics.Raycast(transform.position + new Vector3(0,1,0),transform.forward,out hit,2) && attackTime <= Time.time) { if (hit.transform.CompareTag("Player")) { player.Damage(damage); } else if (hit.transform.CompareTag("BarricadeField")) { BarricadeManager barricadeManager = hit.transform.GetComponent(); if (barricadeManager != null) { barricadeManager.Damage(damage); } } } } private void OnParticleCollision(GameObject other) { if (other.transform.tag == "Bullets") { Damage(); } } }