Enemy collision problem

I’ve been dealing with this problem for a few days now. The enemy is supposed to be chasing the player within a certain range, which works. but when the player and the enemy collide the enemy causes rapid damage and then spawns somewhere behind the player.
I made a recording of this here:

I already tried using box collision meshes but it didn’t work.

Script attached to enemy:

var damageToPlayer : int = 0; 
var distance; 
var maxDistance : float = 5.0;
var target : Transform; 
var lookAtDistance = 8.0; 
var attackRange = 3.0; 
var moveSpeed = 1.0; 
var damping = 5.0; 
var enemyMace : Transform; 

public var enemyHealth = 100; 

function Update ()
{
	var attackTime = Time.time;  
	
	distance = Vector3.Distance(target.position, transform.position);
	
	if(enemyHealth <= 0) 
	{
		enemyDead();
		
	}

	if (distance < lookAtDistance) 
	{
		renderer.material.color = Color.yellow;
		lookAt();
	}
	
	if (distance > lookAtDistance) 
	{
		renderer.material.color = Color.green;
	}
	
	if (distance <attackRange) 
	{
		renderer.material.color = Color.red;
		
		//chase
		transform.Translate(Vector3.forward * moveSpeed /** Time.deltaTime*/);
			
		enemyMace.animation.CrossFade("Sprint");
		
		attack ();	
        }
	
	if (enemyMace.animation.isPlaying == false) 
	{
	enemyMace.animation.CrossFade("Idle"); 
	}
	
}

function lookAt () 
{
	var rotation = Quaternion.LookRotation(target.position - transform.position); 
	transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

}



/*function applyDamage (damage : int) 
{
	enemyHealth -= damage;
}*/

function enemyDead () 
{
	Destroy (gameObject);
}

function attack () 
{	
	//yield WaitForSeconds (2);
	var hit : RaycastHit;
	if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
	{
		distance = hit.distance;
		if (distance < maxDistance)
		{
			//var playerHealth =- damageToPlayer; 	
			
			Debug.Log ("Youŕe taking damage");							
			enemyMace.animation.Play("Attack"); 
			
		}
	}
}

And the script attached to player:

#pragma strict

var damage : int = 50; 
var distance : float;
var MaxDistance : float = 1.5;
var Mace : Transform;  

public var playerHealth = 100; 

function Update ()
{
	if (Input.GetButtonDown("Fire1")) 
	{	//attack animation
		Mace.animation.Play("Attack"); //2) zegt dat de attack animatie afgespeelt moet worden
		//attack function
		var hit : RaycastHit; //2) controleert of de vijand binnen maximale distance is en doet de damage wanneer dat wel het geval is
		if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
		{
			distance = hit.distance;
			if (distance < MaxDistance)
			{
				 
				//var enemyHealth =- damage;
				hit.transform.gameObject.GetComponent(EnemyScript).enemyHealth -= damage;
				Debug.Log ("It's a hit");
			}
		}
	}
	if (Mace.animation.isPlaying == false) 
	{
	Mace.animation.CrossFade("Idle");
	}
	if (Input.GetKey (KeyCode.LeftShift))
	{
		Mace.animation.CrossFade("Sprint"); 
	}
	if (Input.GetKeyUp(KeyCode.LeftShift))
	{
		Mace.animation.CrossFade("Idle"); 
	}
	
		if(playerHealth <= 0)
	{
		playerDead();
	}
	
}

function playerDead ()
{ 
	Application.LoadLevel ("GameOver");
}

You are never waiting for the enemies animation to complete before attacking again, resulting in an attack every update call. Try adding this:

Update()
{
//......
 if (distance <attackRange) 

    {

        renderer.material.color = Color.red;

        

        //chase

        transform.Translate(Vector3.forward * moveSpeed /** Time.deltaTime*/);

            

        enemyMace.animation.CrossFade("Sprint");

        
        if(animation.IsPlaying("Attack") == false) //HERE
             attack ();  

        }
//......
}

Yes it worked like a charm. Completely forgot about basics. :stuck_out_tongue:

The collision problem is still there, but at least the attack damage is working