x


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

more ▼

asked Nov 26 '10 at 10:35 PM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 gravatar image

Loius
10.7k 1 11 41

(comments are locked)
10|3000 characters needed characters left

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 gravatar image

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
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x823
x123
x27

asked: Nov 26 '10 at 10:35 PM

Seen: 941 times

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