My script works perfectly except, if enemy1 hasnt been “killed”(when health = 0, it dies.) enemy2 cant be attacked. I need to be able to reduce any enemies health when pressing the Attack button and enemy is inside the attackThreshold…
like i said this all works, just i cant kill enemy2 unless enemy1 is dead.
Anyone have any idea what i can do to achieve this?
using UnityScript by the way.
This is my EnemyScript.js
private var enemyMaxHp : float;
private var enemyCurHp : float;
var startingHealth : float = 100.0;
var target : Transform;
var moveSpeed = 10;
var rotationSpeed = 10;
var attackThreshold = 10; //distance in which to attack
var giveUpThreshold = 51; //how close you get before enemy start chase
var attackRepeatTime = 100; //attack delay
private var damage;
private var chasing = false;
var attackTime;
var myTransform : Transform; //current transform data of the enemy
private var distance;
var idle;
myTransform = transform; //cache transform data for easy access/performance
function Awake(){
attackTime = Time.time;
myTransform = transform; //cache transform data for easy access/performance
}
function Start(){
enemyMaxHp = startingHealth;
enemyCurHp = startingHealth;
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
distance = (target.position - myTransform.position).magnitude;
if (enemyCurHp > enemyMaxHp){
enemyCurHp = enemyMaxHp;
}
if (enemyCurHp < 0){
enemyCurHp = 0;
}
if (enemyCurHp < 1){
Destroy(gameObject);
}
if(chasing){
Chase();
}
if(distance > giveUpThreshold) { //give up if too far away
chasing = false;
idle = true;
}
if(idle){
//Idle animation.
}
//not currently chasing
//start chasing if target comes close
if(distance < giveUpThreshold) {
chasing = true;
}
if(distance < attackThreshold){
Attack();
}
}
function ApplyEnemyDamage (enemyDamage : float) {
distance = (target.position - myTransform.position).magnitude;
if(distance < attackThreshold){
enemyCurHp = enemyCurHp - enemyDamage;
Debug.Log(enemyCurHp);
}
}
function Attack () {
chasing = false;
if(attackTime < Time.time){
// Calls the function ApplyDamage with a value of 5
gameObject.FindWithTag("Player").SendMessage ("ApplyDamage", 5.0);
attackTime = Time.time + attackRepeatTime;
}
}
function Chase(){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; //move to player
}
this is the attack inside of my PlayerScript.js
function PlayerAttack () {
chasing = false;
if(attackTime < Time.time){
// Calls the function ApplyDamage with a value of 5
gameObject.FindWithTag("Enemy").SendMessage ("ApplyEnemyDamage", 10.0);
attackTime = Time.time + attackRepeatTime;
}
}