0% found this document useful (0 votes)
12 views

Enemy Class

This document contains the code for an EnemyClass script in C# that controls enemy behavior and functionality in a game. The EnemyClass script handles the enemy's health, damage reactions, death explosion, and loot dropping. When damaged, it plays hurt animations and stops movement briefly. When health reaches zero, it explodes, spawns loot, and destroys itself.

Uploaded by

David Parra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Enemy Class

This document contains the code for an EnemyClass script in C# that controls enemy behavior and functionality in a game. The EnemyClass script handles the enemy's health, damage reactions, death explosion, and loot dropping. When damaged, it plays hurt animations and stops movement briefly. When health reaches zero, it explodes, spawns loot, and destroys itself.

Uploaded by

David Parra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.AI;

public class EnemyClass : MonoBehaviour


{
[SerializeField] public float Health;
[SerializeField] ParticleSystem explosion;
[SerializeField] GameObject loot;
[SerializeField] private float lootOnKill = 3;
public bool _explode = false;
public bool isBeingDamage;
[SerializeField] private Animator animator;
public float GotDamagedTimer = 2.0f;
private NavMeshAgent _navAgent;
public bool CanCure;

// Start is called before the first frame update


void Start()
{
_navAgent = GetComponent<NavMeshAgent>();
}

// Update is called once per frame


void Update()
{
if(Health <= 0 && _explode == false)
{
StartCoroutine(Dead());
}
}

public void Damage(float damage)


{
Debug.Log(damage);
Health -= damage;
Debug.Log(Health);
DamageEmission();
Instantiate(loot, new Vector3(transform.position.x, 0.5f,
transform.position.z), Quaternion.identity);
if (Health != 0)
GotDamaged();
}

public void GotDamaged()


{
CanCure = true;
isBeingDamage = true;
if (_navAgent.isActiveAndEnabled)
_navAgent.isStopped = true;
animator.SetTrigger("isBeingStunned");
Invoke(nameof(ResetGotDamagedAnimation),GotDamagedTimer);
}

private void ResetGotDamagedAnimation()


{
CanCure = false;
isBeingDamage = false;
_navAgent.isStopped = false;
}

public IEnumerator Dead()


{
Instantiate(explosion, transform.position, Quaternion.identity);
for (int i = 0; i < lootOnKill; i++)
{
Instantiate(loot, new Vector3(transform.position.x, 0.5f,
transform.position.z), Quaternion.identity);
}
Destroy(this.gameObject);
yield return null;
}

private void DamageEmission()


{
List<Renderer> _renderers = GetComponentsInChildren<Renderer>().ToList();
foreach (Renderer renderer in _renderers)
{
Material[] myMaterials = renderer.materials;
foreach (Material _material in myMaterials)
{
_material.EnableKeyword("_EMISSION");
}
}
this.Invoke(() => ReturnNormalEmission(_renderers), 0.1f);
}

private void ReturnNormalEmission(List<Renderer> renderers)


{
foreach (Renderer renderer in renderers)
{
Material[] myMaterials = renderer.materials;
foreach (Material _material in myMaterials)
{
_material.DisableKeyword("_EMISSION");
}
}
}
}

You might also like