- Home /
Heart/Energy system
I have been looking around, trying to find a way to make a "heart" system. Where you have got 5 hearts, that is showed on the "menu scene" and then if you lose a level you lose one heart, and then every 30 minute a heart regenerates. Then if you have zero hearts you cant play before they regenerate, but if you have a heart you are able to play. How would I do this?
Answer by supernat · Mar 20, 2014 at 04:35 PM
Here's the basic gist for one way to do it:
On startup:
Load the last "heart time" from PlayerPrefs (this would be a time describing when the last heart was gained)
Get current time
Set next heart time to last heart time + 30 minutes
While (current time is > next heart time)
Add 1 to hearts available
next heart time = next heart time + 30 minutes
save current time to PlayerPrefs as last heart time.
In Update:
Check if current time > next heart time
If so:
Add 1 to hearts available
save current time to PlayerPrefs as last heart time.
In your OnGUI:
Loop through all hearts showing
If (index of current heart < hearts available)
draw full red heart
else
draw empty gray heart
Thanks for your respond.
I understand most of the things you mention, but how would i make the Load the last "heart time" from PlayerPrefs.
PlayerPrefs is a class available in the script. For instance, in C#, you could do this:
int lastTime = PlayerPrefs.getInt("LastHeartIncreaseTime");
if (lastTime == 0)
{
Debug.Log("Last time doesnt exist ");
lastTime = // your function here to compute time, could be UTC since 1970 in seconds, ms, could store a date, doesn't have to be an integer.
PlayerPrefs.SetInt("LastHeartIncreaseTime", lastTime);
}
else
{
Debug.Log("Last time is " + lastTime);
}
For working with time, see this: http://stackoverflow.com/questions/2883576/how-do-you-convert-epoch-time-in-c
For the Player Prefs, see this: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
Your answer
Follow this Question
Related Questions
Increase energy bar by collecting item, partially working 0 Answers
This not reading folders in the Windows Store 0 Answers
System.IO in javascript 1 Answer
How to Implement System.StringBuilder into JavaScript? 2 Answers
Energy or lives regen by time 1 Answer