- Home /
How to change the rate of decrease in a variable in the update function
My truck has fuel, and I wanted it to decrease slowly. In side a function I defined to draw the fuel gauge, which is called in the update function, I had this simple line: gameLevel_001_Fuel -= 0.5
. Everything worked fine, the gauge in the game displayed the right amount of fuel, but it drained WAY TOO QUICKLY. Simple fix I thought, just make the value smaller. Only problem was that when I changed 0.5, to 0.05, the rate at which it decreased didn't change. I tried 0.000005, same rate. I even tried calling it directly in in the update function. Maybe there's a better way to have it decrease as time passes?
function SetGuages() { //Called immediately in update function
gameLevel_001_Fuel -= 0.05;
if (gameLevel_001_Fuel > 94){
fuelGuage.renderer.material = fuel_100;
}
else if (gameLevel_001_Fuel > 87){
fuelGuage.renderer.material = fuel_094;
}
else if (gameLevel_001_Fuel > 81){
fuelGuage.renderer.material = fuel_087;
}
else if (gameLevel_001_Fuel > 75){
fuelGuage.renderer.material = fuel_081;
}
else if (gameLevel_001_Fuel > 69){
fuelGuage.renderer.material = fuel_075;
}
else if (gameLevel_001_Fuel > 62){
fuelGuage.renderer.material = fuel_069;
}
else if (gameLevel_001_Fuel > 56){
fuelGuage.renderer.material = fuel_062;
}
else if (gameLevel_001_Fuel > 50){
fuelGuage.renderer.material = fuel_056;
}
else if (gameLevel_001_Fuel > 44){
fuelGuage.renderer.material = fuel_050;
}
else if (gameLevel_001_Fuel > 37){
fuelGuage.renderer.material = fuel_044;
}
else if (gameLevel_001_Fuel > 31){
fuelGuage.renderer.material = fuel_037;
}
else if (gameLevel_001_Fuel > 25){
fuelGuage.renderer.material = fuel_031;
}
else if (gameLevel_001_Fuel > 19){
fuelGuage.renderer.material = fuel_025;
}
else if (gameLevel_001_Fuel > 12){
fuelGuage.renderer.material = fuel_019;
}
else if (gameLevel_001_Fuel > 6){
fuelGuage.renderer.material = fuel_012;
}
else if (gameLevel_001_Fuel > 0){
fuelGuage.renderer.material = fuel_006;
}
}
That's a whole lot of IFs and you're setting the material even when it's not changed!
Place your materials in an array:
var fuel$$anonymous$$aterials[];
Create two ints:
var required$$anonymous$$aterial : int;
var current$$anonymous$$aterial : int;
Use a little math to deter$$anonymous$$e which material to show:
required$$anonymous$$aterial = int(materialgameLevel_001_Fuel / 6);
Only change the material when needed:
if(current$$anonymous$$aterial != required$$anonymous$$aterial){
fuelGuage.renderer.material = fuel$$anonymous$$aterials[required$$anonymous$$aterial];
current$$anonymous$$aterial = required$$anonymous$$aterial;
}
This is only pseudo code! You'll probably need to change a line or too.
Answer by Hydropulse17 · Jul 09, 2014 at 12:44 AM
NVM, I'm a dunce... It's because gameLevel_001_Fuel was an int variable, not a float. God I'm ashamed... I'll leave this up though, so people even less experienced than me can see how crucial it is to use the right var types.