public var target:Transform;
public var attackTimer:float;
public var cooldown:float;
private var anim:Animator ;
public var delay:float=0.1;
function Start () {
attackTimer = 0;
cooldown = 2.0f;
//starts the animator
anim = GetComponent(Animator);
yield WaitForSeconds(0.8);
Destroy(this);
}
function Update () {
anim.SetBool("Attack001",false);
go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
if(Input.GetKeyUp(KeyCode.F)){
anim.SetBool("Attack001",true);
}
}
//take 10 from healthbar
private function Attack() {
(anim.SetBool("Attack001",true));
var Healthbarph :Healthbar ;
Healthbarph = target.GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
}
function OnCollisionEnter(other :Collision){
if (other.collider.tag == "Enemy"){
Attack();
yield WaitForSeconds(delay);
Destroy(this);
}
}
my problem is that this script allows the player to damage the enemy he is colliding with by gathering all the enemies with tag enemy and damage them upon collision but it gathers all the healthbar in a random order so if i collided with enemy a enemy b will take damage what i want is a way to damage the specific enemy i am colliding with i tried to
take out all of the targeting lines
go = GameObject.FindGameObjectWithTag("Enemy");
target = go.transform;
and change
Healthbarph = target.GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
to
Healthbarph = GetComponent("Healthbar");
Healthbarph.AddjustCurrentHealth(-10);
but it didn’t work
i have a targeting script but cant use it for the as i only targets one person at a
time and i need some attacks damage multiply people i am colliding with
here is the healthbar script
public var maxHealth: int = 100;
public var curHealth: int = 100;
public var healthBarLength: float;
function Start() {
healthBarLength = Screen.width / 2;
}
function Update() {
AddjustCurrentHealth(0);
}
function OnGUI() {
GUI.Box(Rect(10, 50, healthBarLength/2, 20), curHealth + "/" + maxHealth);
}
function AddjustCurrentHealth(adj: int) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0 ;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1 ;
if(curHealth == 0 ){
Destroy (gameObject, 1);
animation.Play("Death");}
var maxHealthF: float = maxHealth;
healthBarLength = (Screen.width / 2) * (curHealth / maxHealthF);
}