Zombie wont repeat hit

When my zombie gets in range and hits me once it just stands there, how do i get it to keep hitting me even when its in range. Heres my 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("fallToFace");
		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("shamble");
	    }

	}
}

function ApplyDamage(dmg : float){

	health -= dmg;

}

2 Answers

2

Try this, put this at the bottom of your code -

function attack(){
	animation["attack1"].wrapMode = WrapMode.Loop;
	animation.Play("attack1");
}

Now change line 38 to -

attack();

Now add this between line 40 and 41 -

animation.Stop("attack1");

No, didnt work :(

Try changing line 37 to - if ((distance == 3.0f) || (distance < 3.0f)) {

What isn't working exactly? Is the attack animation not looping? Or what is actually happening when the zombie is in range?

Can else statements reside inside nested if’s? I dont know if that’s causing anything, but maybe you could try taking the else and putting in the If like so:

 var distance = Vector3.Distance(target.position, myTransform.position);
        if (distance < 3.0f) {
            animation.Play("attack1");
            //move towards the player
              myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
              animation.Play("shamble");
        }

If you think about it, the zombie will stand there and attack while standing still, yet, you could still be in range. Now as the the only attacking once, try adding a this into your code and see If it would count up. You would just have to change the animations a bit. Maybe throw in a walking/lunging attack type thing.

var count = 0;

Debug.Log(count);
count++;

Throw it into the If statement to see If the update is actually happening.