- Home /
Trying to get my enemy attack damage to work
Hey everybody I amtrying to get my enemy damage to work. I have my player to attack the enemy, and when he attacks the enemy he should die. I dont have any animation set up for the death, I just want the enemy to be destroyed. So far I have this
// sound clips: var struckSound : AudioClip;
var health = 3;
function ApplyDamage( amount : int ) {
health -= amount; // <- (update on-screen health displays here)
if (health < 0) { Die(); } else { // <- (play "hurt" animation & sound here) //audio.PlayOneShot(struckSound);
}
}
function Die() {
// now remove this gameObject from the scene:
Destroy(gameObject);
}
when I attack the enemy the editor stops and gives me the error the prefab you want to instantiate is null. What does this mean? I get this error everytime a run it.
You should click the error to see the line causing the problem, I don't see anything wrong here.
Answer by user-11402 (google) · Apr 11, 2011 at 03:48 AM
Here's the scripts i use i hope they can help
Here's the PlayerHealth script but it has GUI problems:
var maxHealth : int = 100; var curHealth : int = 100;
var healthBarLength : float ;
// Use this for initialization function Start () { healthBarLength = Screen.width / 2; }
// Update is called once per frame function Update () { AddjustCurrentHealth(0);
}
function OnGUI() { GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth); }
function AddjustCurrentHealth(adj) { curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (maxHealth / curHealth);
}
and here's the EnemyAttack:
var target : GameObject; var attackTimer : float = 2; var coolDown : float = 2; var damage : int = 10;
// Use this for initialization function Start () { attackTimer = 0; coolDown = 2.0f;
}
// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;
if (attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0){
Attack();
attackTimer = coolDown;
}
} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);
var direction = Vector3.Dot(target.transform.position - transform.position, transform.forward);
if(distance < 3) {
if(direction > 0) {
var go = GameObject.Find("Player");
go.GetComponent(PlayerHealth).AddjustCurrentHealth(-damage);
}
}
}
Your answer
Follow this Question
Related Questions
How do I animate my enemy randomly with time? 1 Answer
Enemy Ragdoll help 1 Answer
Problem with enemy AI 1 Answer