My enemy stops taking damage after about three seconds from starting the game.
The shooting script is here:
#pragma strict
var Range : float = 1000;
var Force : float = 1000;
var hitMark : GameObject;
var texta : GUITexture;
var four : Texture;
var three : Texture;
var two : Texture;
var one : Texture;
var zero : Texture;
var five : Texture;
var bullets : int = 5;
var shootSound : AudioClip;
function Start () {
}
function Update () {
if(Input.GetKeyDown(KeyCode.R)){
bullets = 5;
GameObject.Find("Gun").animation.Play("reload");
}
if(bullets == 5){ texta.guiTexture.texture = five; }
else if(bullets == 0){ GameObject.Find("Gun").animation.Play("reload"); bullets = 5; }
else if(bullets == 4){ texta.guiTexture.texture = four; }
else if(bullets == 3){ texta.guiTexture.texture = three; }
else if(bullets == 2){ texta.guiTexture.texture = two; }
else if(bullets == 1){ texta.guiTexture.texture = one; }
var hit : RaycastHit;
var direction : Vector3 = transform.TransformDirection(Vector3.forward);
if(Input.GetMouseButtonDown(0)){
bullets--;
audio.PlayOneShot(shootSound);
if(Physics.Raycast(transform.position , direction , hit, Range)){
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(hit.collider.gameObject.tag == "zombie"){
//hit.rigidbody.AddForceAtPosition( direction * 100 , hit.point);
//hit.collider.gameObject.animation.Play("die");
Instantiate(hitMark);
hit.collider.gameObject.SendMessage("ApplyDamage", 25);
}
}
}
}
And the enemy script:
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var isNotDead : boolean = true;
var health : float = 100;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
if(health < 1){
isNotDead = false;
animation.Play("die");
Destroy(gameObject, 1);
}
if(isNotDead){
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
var distance = Vector3.Distance(target.position, myTransform.position);
if (distance < 3.0f) {
animation.Play("attack1");
}
else{
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
animation.Play("walk1");
}
}
}
function ApplyDamage(dmg : float){
health -= dmg;
}