Skip to content
Permalink
07bb02fdb8
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
88 lines (65 sloc) 1.96 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
//Used this script for most of the enemies, all the basics really. animation, health and UI health bar which was commented out in Enemy2
public Animator animator;
public float maxHealth = 75; //health
public float currentHealth;
public GameObject healthBarUI;
public Slider slider;
public GameObject enemie;
public Transform ghost;
public int Shurikendamage = 10;
public GameObject score; //shuriken
void Start()
{
currentHealth = maxHealth;
slider.value = CalculateHealth();
ghost.position = new Vector3(ghost.position.x, ghost.position.y, ghost.position.z);
}
void Update()
{
slider.value = CalculateHealth();
if(currentHealth < maxHealth)
{
healthBarUI.SetActive(true);
}
}
public void TakeDamage(int damage)
{
healthBarUI.SetActive(true);
currentHealth -= damage;
slider.value = CalculateHealth();
//Hurt Animation
SoundManagerScript.PlaySound("GhostHurt");
animator.SetTrigger("Hurt");
if (currentHealth <= 0)
{
Instantiate(score, new Vector3(ghost.position.x, ghost.position.y, ghost.position.z), Quaternion.identity);
animator.SetBool("IsDead", true);
Die();
}
}
void Die()
{
enemie.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Bullet"))
{
other.gameObject.SetActive(false);
TakeDamage(Shurikendamage);
healthBarUI.SetActive(true);
slider.value = CalculateHealth();
SoundManagerScript.PlaySound("GhostHurt");
}
}
float CalculateHealth()
{
return currentHealth / maxHealth;
}
}