Zombie drop body parts

This commit is contained in:
Kotrba Filip
2020-02-01 10:12:47 +01:00
parent e72b3e2092
commit 39b3f8277d
4 changed files with 92 additions and 178 deletions

View File

@@ -6,12 +6,15 @@ using UnityEngine.AI;
public class EnemyManager : MonoBehaviour
{
public float health = 100;
public float actualHealth;
private NavMeshAgent navMeshAgent;
private PlayerManager player;
public List<GameObject> bodyParts = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
actualHealth = health;
player = FindObjectOfType<PlayerManager>();
navMeshAgent = GetComponent<NavMeshAgent>();
}
@@ -20,9 +23,32 @@ public class EnemyManager : MonoBehaviour
void Update()
{
navMeshAgent.SetDestination(player.transform.position);
if (health <= 0)
DropBodyPart();
if (actualHealth <= 0)
{
Destroy(this.gameObject);
if (bodyParts.Count > 0) {
foreach (GameObject bodyPart in bodyParts)
{
Destroy(bodyPart, 3);
}
}
navMeshAgent.isStopped = true;
navMeshAgent.enabled = false;
gameObject.AddComponent<Rigidbody>();
Rigidbody rigidBodyEnemy = gameObject.GetComponent<Rigidbody>() 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);
}
}
@@ -30,7 +56,26 @@ public class EnemyManager : MonoBehaviour
{
WeaponManager weaponManager = player.weaponList[player.selectedWeapon].GetComponent<WeaponManager>();
if (weaponManager != null) {
health -= weaponManager.damage;
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>();
Rigidbody rigidBodyEnemy = bodyParts[i].GetComponent<Rigidbody>() as Rigidbody;
bodyParts[i].AddComponent<BoxCollider>();
rigidBodyEnemy.mass = 10;
}
}
}
}