Sleeping/Resting

Hi I am fairly new to Unity and would like to create a script that puts the character to sleep and restores health.

Initially I just want to set up an interface with a button that puts the character to sleep for a short time and restores health.

Once I have done that I will worry about making it that only in certain places that the character can sleep i.e a bed. as well as nice character movements, etc.

If anyone could help me get off the ground or point me in the right direction with this one it would be much appreciated.

Cheers

var Health : int = 45;
var MaxHealth : int = 100;
var RestTimeSeconds : int = 10;
var HealthIncrementPerSecond  : int = 5;
var isRestingPlace : boolean = true;

private var isResting : boolean = false;

function OnGUI()
{
	if(!isResting  isRestingPlace)
	{
		if(GUI.Button(Rect(10,10,80,30), "Rest 10s"))
			StartResting();
	}
	else
	{
		if(isRestingPlace)
		{
			if(GUI.Button(Rect(10,10,80,30), "Stop Resting"))
				isResting = false;
		}
	}
	GUI.Label(Rect(10,50,80,20),"Health: " + Health);
}

private function StartResting()
{
	isResting = true;
	for(var i = 0 ; i < RestTimeSeconds ; i++)
	{
		Health += HealthIncrementPerSecond;
		if(Health > MaxHealth)
		{
			Health = MaxHealth;
			break;
		}
		yield WaitForSeconds(1.0);
		if(!isResting)
			break;
	}
	isResting = false;
}

Just remeber to check when trying to move if player is resting and then handle situation. Either aborting resting or interrupt moving and letting player rest.

Cheers heaps!!

Hey guys, it’s the time to cheers.