using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyManager : MonoBehaviour { public float health = 100; public float actualHealth; private NavMeshAgent navMeshAgent; private PlayerManager player; public List bodyParts = new List(); // Start is called before the first frame update void Start() { actualHealth = health; player = FindObjectOfType(); navMeshAgent = GetComponent(); } // Update is called once per frame void Update() { navMeshAgent.SetDestination(player.transform.position); DropBodyPart(); if (actualHealth <= 0) { if (bodyParts.Count > 0) { foreach (GameObject bodyPart in bodyParts) { Destroy(bodyPart, 3); } } navMeshAgent.isStopped = true; navMeshAgent.enabled = false; gameObject.AddComponent(); Rigidbody rigidBodyEnemy = gameObject.GetComponent() as Rigidbody; rigidBodyEnemy.velocity = -gameObject.transform.forward; rigidBodyEnemy.mass = 50; Destroy(this.gameObject, 3); } } void FixedUpdate() { if (actualHealth > 0) { Vector3 _dir = player.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 DropBodyPart() { if (bodyParts.Count > 0) { float number = health / (bodyParts.Count + 1); for (int i = 0; i < bodyParts.Count; i++) { if ((health - actualHealth) / number > i) { bodyParts[i].transform.parent = null; bodyParts[i].AddComponent(); Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent() as Rigidbody; bodyParts[i].AddComponent(); rigidBodyEnemy.mass = 10; } } } } private void OnParticleCollision(GameObject other) { if (other.transform.tag == "Bullets") { Damage(); } } }