Hi all~
i have problems on my enemy health bar, when i try to hit single enemy the health bar on all enemies will decrease at the same time,
i don’t know what happen in here. Can someone help me Fix this ?
here is my scripts Player attack :
var targets : GameObject [];
var attackTimer : float;
var coolDown : float;
var prefabBullet : Transform;
var shootForce : float;
var hand : Transform;
var fireRate : float = 1.4f;
public var playerTarget : Transform;
public var HealthEnabled:boolean = false;
public var lastPlayerTarget;
private var nextFire : float = 0f;
// Use this for initialization
function Start () {
targets = GameObject.FindGameObjectsWithTag("Enemy");
attackTimer = 0;
coolDown = 1.0f;
}
// Update is called once per frame
function Update ()
{
if(Health == 0)
{
PlayerAnimation.Stop("dead");
}
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.Mouse0) || Input.GetKey (KeyCode.Return) && prefabBullet.rigidbody)
{
if(attackTimer == 0)
{
if( Health.health > 0 && EnemyHealthJava2.healthBarLength > 0)
{
//Health.health > 5;
Attack();
attackTimer = coolDown;
}
}
}
}
function Attack()
{
for (var i = 0; i < targets.Length; i++)
{
var distance : float = Vector3.Distance(targets*.transform.position, transform.position);*
var dir : Vector3 = (targets*.transform.position - transform.position).normalized;*
var direction : float = Vector3.Dot(dir, transform.forward);
* if(distance < 10)*
* {*
* if(direction > 0)*
* {*
* nextFire = Time.time + fireRate;*
* var clone : Transform = Instantiate(prefabBullet, hand.position, hand.rotation) as Transform;*
_ clone.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 20);_
* animation.Play(“shoot”);*
_ var eh : EnemyHealthJava2 = targets*.GetComponent(“EnemyHealthJava2”);
eh.AdjustCurrentHealth(-10);*_
* }*
* }*
* } *
* }*
here is the enemy health:
var playerName : String;
var adjustment : float= 2.3f;
var healthTex : Texture2D;
static var health : int = 100;
static var curHealth : int = 100;
static var enemy : String;
static var alive : boolean = true;
static var healthBarLength : float = 100;
private var worldPosition : Vector3= new Vector3();
private var screenPosition : Vector3= new Vector3();
private var myTransform : Transform;
private var myCamera : Camera;
private var healthBarHeight : int= 5;
private var healthBarLeft : int= 110;
private var barTop : int= 1;
//private var healthBarLength : float= 100;
private var labelTop : int= 18;
private var labelWidth : int= 110;
private var labelHeight : int= 15;
private var myStyle : GUIStyle= new GUIStyle();
function Awake ()
{
* myTransform = transform;*
* myCamera = Camera.main;*
* myStyle.normal.textColor = Color.green;*
* myStyle.fontSize = 12;*
* myStyle.fontStyle = FontStyle.Bold;*
* myStyle.clipping = TextClipping.Overflow;*
}
function Update () {
* if (healthBarLength != null && curHealth <= 0)*
* {*
* Destroy(gameObject);*
* Debug.Log(“Enemy Death”);*
* return;*
* }*
}
function OnGUI ()
{
* worldPosition = new Vector3(myTransform.position.x, myTransform.position.y + adjustment,myTransform.position.z);*
* screenPosition = myCamera.WorldToScreenPoint(worldPosition);*
* GUI.Box(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,100, healthBarHeight), “”);*
* GUI.DrawTexture(new Rect(screenPosition.x - healthBarLeft / 2, Screen.height - screenPosition.y - barTop,healthBarLength, healthBarHeight), healthTex);*
* GUI.Label(new Rect(screenPosition.x - labelWidth / 2, Screen.height - screenPosition.y - labelTop, labelWidth, labelHeight), playerName, myStyle);*
*} *
static function UpOrDown (amount : int)
{
* healthBarLength += amount;*
}
public function AdjustCurrentHealth( adj : int)
{
* healthBarLength += adj;*
* if(healthBarLength <=0 && alive)*
* {*
* alive = false;*
* healthBarLength = 0;*
}
* if(healthBarLength > health)*
* healthBarLength = health;*
* if(health < 1)*
* health = 1;*
}