Hi I’m trying to get my player attack enemies with the tag “Enemy”, but my code only allows me to attack an enemy. I wanted an attack could reach various of them.
Is there any method of doing this?
PlayerAttack javascript:
var target : GameObject[];
var attackTimer : float;
var coolDown : float;
function Start () {
attackTimer = 0;
coolDown = 0.45f;
}
function Update() {
target = GameObject.FindGameObjectsWithTag("Enemy");
for (var i = 0; i < target.length; i++) {
var ab = target*.gameObject;*
}
-
if(attackTimer > 0)*
-
attackTimer -= Time.deltaTime;*
-
if(attackTimer < 0)*
-
attackTimer = 0;*
-
if(Input.GetButton(“Fire1”)) {*
-
if(attackTimer == 0)*
-
attack ();*
-
attackTimer = coolDown;*
-
}*
-
}*
-
private function attack () {*
-
var distance : float = Vector3.Distance(target[0].transform.position, transform.position);*
-
var dir : Vector3 = (target[0].transform.position - transform.position).normalized;*
-
var direction : float = Vector3.Dot(dir, transform.forward);*
-
if(distance < 20) {*
-
if(direction > 0) {*
-
var eh : enemyVida = target[0].GetComponent(enemyVida);*
-
eh.addjustCurrentHealth(-10);*
-
}*
-
}*
-
}*
Someone could help me.