using UnityEngine;
using System.Collections;
public class enemybehaviors : MonoBehaviour {
//health of enemy
public int health;
//is the enemy graced?
private bool attackable;
//length of grace period
public int grace;
//local call for grace period value
private int gracee;
void Start(){
Debug.Log ("START");
//default attackable to true
attackable = true;
//local call for grace period value
gracee = grace;
}
void Update(){
Debug.Log ("UPDATE");
//if enemys health is 0 destroy enemy
if(health == 0)Destroy (gameObject);
//if enemy is invincible, decrease grace period remaining
if(attackable == false){
gracee=gracee-1;
}
//if grace period is 0 allow to be attacked again
if(gracee == 0){
attackable = true;
gracee = grace;
}
}
void OnCollisionEnter (Collision col){
Debug.Log ("HIT");
//if sword hits enemy take away health and turn on grace period
if (col.gameObject.name == "sword" && attackable == true) {
health=health-1;
attackable = false;
}
}
}
I have been trying to make a top down game where you slash a sword at enemies. I tried adding a health and grace period mechanic however but enemies still die in one hit. Any fixes? If its obvious I did something wrong I’m new to Unity.