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
112 lines (86 sloc) 2.41 KB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy2 : MonoBehaviour
{
//Basically the same script as the other enemies
//Health
public float maxHealth = 75; //health
public float currentHealth;
//Health bar UI
public GameObject healthBarUI;
public Slider slider;
public GameObject enemie;
public Transform ghost;
//not used
public int Shurikendamage = 10;
public GameObject score; //shuriken
//references
public GameObject bullet;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Start()
{
//ghost position there so it can drop score there
currentHealth = maxHealth;
slider.value = CalculateHealth();
ghost.position = new Vector3(ghost.position.x, ghost.position.y, ghost.position.z);
fireRate = 3f;
nextFire = Time.time;
}
void Update()
{
//failed attempt at healthbar being inactive on start
slider.value = CalculateHealth();
if (currentHealth < maxHealth)
{
healthBarUI.SetActive(true);
}
CheckIfTimeToFire();
}
//can he fire
void CheckIfTimeToFire()
{
if (Time.time > nextFire)
{
Instantiate(bullet, shotSpawn.position, shotSpawn.rotation);
nextFire = Time.time + fireRate;
}
}
//take damage function
public void TakeDamage(int damage)
{
healthBarUI.SetActive(true);
currentHealth -= damage;
slider.value = CalculateHealth();
//Hurt Animation
SoundManagerScript.PlaySound("GhostHurt");
if (currentHealth <= 0)
{
Instantiate(score, new Vector3(ghost.position.x, ghost.position.y, ghost.position.z), Quaternion.identity);
Die();
}
}
void Die()
{
Destroy(enemie);
}
//collide with bullet
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;
}
}