Health Regeneration

Hi there and thanks for looking. The problem i am having is this. When i hit an object i want my script to wait 10 seconds then start regenerating health but if i hit something again within those 10 seconds restart the timer before regenerating health.

As when i hit something within the ten seconds it adds health after 10 seconds for the first object hit then does the same for the second. here is the full script

var health : int = 100;
var lives : int;
var levelNameToLoadWhenDead : String;
var levelNumToLoadWhenDead : int;
var easyMode : boolean = false;
var normalMode : boolean = false;
var hardMode : boolean = false;     ///////// delete all the min health and max health apart from 1 each as this is all we need
var realisticMode : boolean = false;
var healthRegenaration : boolean = false;
var healthRegenerationTime : int = 10; // Time it takes to start regenerating health
var loseHealthTag1_5health : String;// In the inspector just type the name of the Tag you want to be affected by // TRY AN ARRAY IN HERE [] !!!!!!!!!!!!!!!!!!!!!!!
var loseHealthTag2_10health : String;
var loseHealthTag3_15health : String;
var loseHealthTag4_20health : String;
var loseHealthTag5_25health : String;// In the inspector just type the name of the Tag you want to be affected by
var loseHealthTag6_30health : String;
var loseHealthTag7_35health : String;
var loseHealthTag8_40health : String;
var loseHealthTag9_45health : String;// In the inspector just type the name of the Tag you want to be affected by
var loseHealthTag10_50health : String;

@HideInInspector var gotHit : boolean = false;
@HideInInspector var easyHealth : int = 200;
@HideInInspector var normalHealth : int = 100;
@HideInInspector var hardHealth : int = 75;
@HideInInspector var realisticHealth : int = 50;
@HideInInspector var checkpoint1 : boolean = false;
@HideInInspector var checkpoint2 : boolean = false;


function Start()
	{
		Difficulty();
	}


function Update()
{
	Modes();
	Health();

}



// EasyMode   ///////////  EasyMode /////////// EasyMode
function OnTriggerEnter(hit : Collider)
{

		if(hit.gameObject.tag == loseHealthTag1_5health && easyMode == true)
		{
			health -= 5;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
			
		}

		
		if(hit.gameObject.tag == loseHealthTag2_10health && easyMode == true)
		{
			health -= 10;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag3_15health && easyMode == true)
		{
			health -= 15;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag4_20health && easyMode == true)
		{
			health -= 20;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}		
		}
	
	
		
	
	if(hit.gameObject.tag == loseHealthTag1_5health && normalMode == true)
		{
			health -= 10;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
			
		}

		
		if(hit.gameObject.tag == loseHealthTag2_10health && normalMode == true)
		{
			health -= 20;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag3_15health && normalMode == true)
		{
			health -= 25;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag4_20health && normalMode == true)
		{
			health -= 50;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}		
		}
		
		
			if(hit.gameObject.tag == loseHealthTag1_5health && hardMode  == true)
		{
			health -= 20;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
			
		}

		
		if(hit.gameObject.tag == loseHealthTag2_10health && hardMode  == true)
		{
			health -= 25;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag3_15health && hardMode == true)
		{
			health -= 30;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}
		}
	
	
		if(hit.gameObject.tag == loseHealthTag4_20health && hardMode  == true)
		{
			health -= 50;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}		
		}
		
			if(hit.gameObject.tag ==  loseHealthTag1_5health && realisticMode  == true)
		{
			health -= 50;
			Debug.Log(health);
				if(healthRegenaration == true)
					{
						gotHit = true;
						HealthRegenaration();
						gotHit = false;
					}		
		}
		
		if(hit.gameObject.name == "CheckPoint")
			{
				checkpoint1 = true;
				checkpoint2 = false;
				Destroy(hit.gameObject);
			}
			
		if(hit.gameObject.name == "CheckPoint2")
			{
				checkpoint2 = true;
				checkpoint1 = false;
				Destroy(hit.gameObject);
				Debug.Log(checkpoint1 && checkpoint2);
			}
	
	
		
		// We neeed to add a GUI function to keep track of health
		

		
		
}




function Modes() // For the Editor
{
	if(easyMode)
		{
			normalMode = false;
			hardMode = false;
			realisticMode = false;
		}
		
	if(normalMode)
		{
			easyMode = false;
			hardMode = false;
			realisticMode = false;
		}
		
	if(hardMode)
	
		{
			easyMode = false;
			normalMode = false;
			realisticMode = false;
		}
		
	if(realisticMode)
		{
			easyMode = false;
			normalMode = false;
			hardMode = false;
		}
	


}


function HealthRegenaration()
{
		
		
		if(gotHit == true && healthRegenaration == true);
		
			yield WaitForSeconds (healthRegenerationTime);
			Debug.Log(healthRegenerationTime);
			health += 10;
			// Here i need to get it so if we hit an object start a timer counting down from ten then take health away if we hit something again within the 10 seconds restart the timer before health is debducted 
			
			
		
	
 
		
}

function Health()
{
	if(health <= 0)
		{
			lives -= 1;
			if(checkpoint1 == false && checkpoint2 == false)
				{
					transform.position = Vector3 (1,5,1);
					health = 100;
				}
			
			
			if(checkpoint1 == true)
				{
					transform.position = Vector3 (25,10,25);// In here type the Co-ordinates of where you would like to spawn. When you die
					health = 100;
				}
			
			if(checkpoint2 == true)
				{
					transform.position = Vector3 (10,20,10);
					health = 100;
				}
		}
		

		/*
	if(lives <= 0)
		{
			Application.LoadLevel(levelNumToLoadWhenDead || levelNameToLoadWhenDead);
		}
		*/
		
	if(easyMode == true && health >= easyHealth)
		{
			health = easyHealth;
		}
		
	if(normalMode == true && health >= normalHealth)
	
		{
			health = normalHealth;
		}
		
	if(hardMode == true && health >= hardHealth)
		{
			health = hardHealth;
		}
		
	if(realisticMode == true && health >= realisticHealth)
		{
			health = realisticHealth;
		}
		

}


function Difficulty()
{
	if(easyMode == true)
		
		{
			health = easyHealth;
		}
	
	if(normalMode == true)
		
		{
			health = normalHealth;
		}
		
	if(hardMode == true)
		
		{
			health = hardHealth;
		}
		
	if(realisticMode == true)
		{
			health = realisticHealth;
		}
}


function OnGUI ()
	{
		if(checkpoint1 == true)
			{
				GUI.Label (Rect (25, 25, 100, 60), "You reached the Checkpoint");
			}
			
		if(checkpoint2 == true)
			{
				GUI.Label (Rect(25,25,100,60), "You reached another Checkpoint Your smart!");
			}
	}

This part of your script is the problem:

function HealthRegenaration()
{

    if(gotHit == true && healthRegenaration == true);

        yield WaitForSeconds (healthRegenerationTime);
        Debug.Log(healthRegenerationTime);
        health += 10;
}

one, the if statement does nothing: the semicolon kills it so there is nothing inside of it

two, more importantly, you are just creating a coroutine that waits a bit and regens. This is far from an optimal approach. I would do the following:

  • kill this function. It is causing more harm than good.

  • create a var at the top of the script. Call it “timeOfLastHit” or something along those lines. In it, store a float. Every time you hit an object, get hurt, etc. do this:

    timeOfLastHit = Time.time;

  • Then, in the Update function,

    if(Time.time - timeOfLastHit > healthRegenerationTime)

    //do health regen code here.
    

I hope this has helped point you in the right direction.