How do I make my melee script do damage.

I have a melee script but I don’t know how to make it do damage to either an ai or a player here is the script so far it is in js.

var target : Transform;

function Update ()
{
var dist : float = Vector3.Distance(transform.position, target.position);

if(Input.GetKeyUp(KeyCode.Mouse0) && dist <= 2.1){
	GetComponent.<Animation>().Play("Default Take");
}

if(Input.GetKeyUp(KeyCode.Mouse0) && dist > 2.1){
	GetComponent.<Animation>().Play("Default Take");
}

}

  • Create a script which will hold npc health
  • When attack is done check if you actually hit the target. You could use collision check, distance, trigger or raycasts.
  • When damage is done, use GetComponent from hit object - npc script and remove the damage done from its health. If health equals or is lower than 0 kill the npc.