# Variable that decreases after a certain amount of time?

 0 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 more ▼ asked Nov 26 '10 at 10:35 PM Tyler 2 1.1k ● 211 ● 246 ● 264 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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); } `````` more ▼ answered Dec 26 '10 at 04:45 AM Loius 10.7k ● 1 ● 11 ● 41 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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. more ▼ answered Nov 27 '10 at 01:35 AM oliver-jones 2.5k ● 205 ● 225 ● 254 That will invoke Timer every frame, and every Timer call will reduce the score by exactly 4. Dec 26 '10 at 04:46 AM Loius add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x823
x123
x27

asked: Nov 26 '10 at 10:35 PM

Seen: 941 times

Last Updated: Nov 26 '10 at 10:35 PM