Damage on Collison with my First person Controller

Hi sorry I am in a video design class at my highschool and i am doing my best to make a zombie game. i have my first person controller with this script

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {
public float maxHealth = 100;
public float regeneration = 5;
float health = 100; // Decrease this in your 'damage' script- I won't go into that
	// Use this for initialization
	void Start () 
	{
	health = maxHealth;
	}
	
	// Update is called once per frame
	void Update () 
	{
	       if(health < maxHealth)
		   {
				health += regeneration * Time.deltaTime;
				if(health > maxHealth)
				{
					 health = maxHealth;
				}
			}

	}
	
	void OnGUI()
	{
			if(health < maxHealth)
			{
				Color tempColour = GUI.color;
				GUI.color = Color.red;
				GUI.Label(new Rect((Screen.width / 2) - 70, (Screen.height / 2) - 20, 140, 40), "OH NOE THE PAIN");
				GUI.color = tempColour;
			}
	}

}

and i have a zombie which i am wanting to just make a bunch of copies of when i get it right apply damage when it collides with my first Person Controller. It has an animation playing and has a smooth look at script so it follows my player and runs at him. whenever they collide i want damage to occur to my player any ideas?

This would be your player health script or whatever it is that you posted above in your question ->


using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {

public float maxHealth = 100.0f ;
float health = 0.0f ;
public float regeneration = 10.0f ;
 
void Start(){
   health = maxHealth ;
}
 
void Update(){
   if(health < maxHealth)
      health += regeneration * Time.deltaTime ;

   if(health > maxHealth)
      health = maxHealth ;
}
 
void OnGUI(){
   if(health < maxHealth){
      Color tempColour = GUI.color ;
      GUI.color = Color.red ;
      GUI.Label(//!!!---whateveryou have here---!!!) ;
      GUI.color = tempColour ;
   }
}
 
void OnControllerColliderHit(ControllerColliderHit other){
   if(other.gameObject.CompareTag("Zombie")){ //make sure you tag zombie as Zombie
      //if we hit a zombie, get it's script for damage->
      ZombieAttributes otherScript = other.gameObject.GetComponent<ZombieAttributes>();
      //now apply said damage
      ApplyDamage(otherScript.damageToInflict) ;
   }
}
 
void ApplyDamage(float damage){
   health -= damage ;
}
}


Now, make a new c# script. Name it “ZombieAttributes” . Attach it to your zombie object, and make sure you tag said object as “Zombie”. Then you can have different zombie prefabs with different damage settings etc.

ZombieAttributes →


using UnityEngine;
using System.Collections;

public class ZombieAttributes : MonoBehaviour {

   public float damageToInflict = 5.0f ;

}

...and that's all that goes on that script, for doing this anyway. You can add whatever else to that as well, and use it like we did above for damaging the player or slowing the player speed down etc etc.

This will keep damaging the player when you keep colliding with it, e.g. you continue pressing forward while the zombie is in front of you and it will keep depleting player health. If you want to limit it a bit, say have the zombie only able to damage the player once every 2 seconds, then you’ll need to mod a bit.