- Home /
Variable that decreases after a certain amount of time?
Hello. How can I have a static variable decrease at a set rate after a period of inactivity? Ex: A value increases when a player shoots an enemy. If the player has shot no enemies for 5 seconds, the value will start to decrease at a rate of 2 every 3 seconds. Thanks
Answer by Loius · Dec 26, 2010 at 04:45 AM
Something like this -
var lastScoreTime : float; var score : float; var pointLossPerSecond : float = 0.67; var penaltyTime : float = 0.5;
function Score( amnt : int ) { score += amnt; lastScoreTime = Time.time; }
function Update() { if ( Time.time - lastScoreTime > penaltyTime ) score -= Time.deltaTime * pointLossPerSecond; score = Mathf.Max(0, score); }
Answer by oliver-jones · Nov 27, 2010 at 01:35 AM
Humm, try something like this:
static var Score : int = 20; static var KilledEnemy : boolean = false;
function Update()
{
if(KilledEnemy == false){
Invoke("Timer", 5);
}
}
function Timer()
{
Score =-2;
yield WaitForSeconds(3);
Score =-2;
return;
}
Hasn't been tested, in a hurry - sorry.
That will invoke Timer every frame, and every Timer call will reduce the score by exactly 4.