- Home /
Reducing a value over time
Ok, read a few questions, haven't managed to get anything to work.
I have a system set up so when someone leaves a building, their body temp should decrease over time, using a modifiable variable. Everything i try so far either leaves it not working at all, or defaulting to frames rather than seconds. I'm using js. Below is the relevant lines from my current code, with my last attempt at getting it to work.
var bodytemp : float = 20.0;
var heatloss : float = 0.1;
var playeroutside : boolean = false;
if (playeroutside){
InvokeRepeating("bodytempmody", 2, 20);
}
function bodytempmody (){
bodytemp -= heatloss;
}
Any thing anyone can suggest is much appreiciated.
Answer by BiG · May 28, 2012 at 08:57 AM
Try this:
var bodytemp : float = 20.0;
var heatloss : float = 0.1;
var playeroutside : boolean = false;
var function_lock = false;
function Update(){
if (playeroutside && !function_lock){
function_lock = true;
bodytempmody();
}
}
function bodytempmody(){
yield WaitForSeconds(2);
bodytemp -= heatloss;
function_lock = false;
}
Answer by Alismuffin · May 28, 2012 at 09:04 AM
Maybe you could use Time.deltaTime? (Time passed since last frame). This should give you a steady decrease of 0.1 to bodytemp each second
Untested however so I could be wrong
var bodytemp : float = 20.0;
var heatloss : float = 0.1;
var playeroutside : boolean = false;
if(playeroutside){
bodytemp -= heatloss*Time.deltaTime;
}
Oh and of course you have to wrap all but the variables in the Update function
I tested it now and works fine If you wish it to go down 1 value per second, simply change the variable of heatloss to 1. It will count down in seconds.
Your answer
Follow this Question
Related Questions
slowly decrease value when in trigger 1 Answer
TimeSpan why are you being difficult? 2 Answers
Need help with value over time! 1 Answer
Static Variable Doesn't Change ? 0 Answers
Variable that decreases after a certain amount of time? 2 Answers