Hi all, just a quick question in relation to scripting for a game I’m currently designing: I want the main player character to have a total ‘health’ bar and every second a weapon is fired (the mouse is held down) a value is deducted from the total vaule.
For example:
- total health of 100
- the mouse is held down for 5 seconds and the main machine gun is fired
- 2 health is taken away for each second
Also once 1 second has elapsed the health has to recharge at a rate of 5 health per second.
How would I go about scripting this…?
Just a basic code that I can build on myself (I’m quite new to scripting!)
Thanks!
Time.deltaTime is what you need for both things. I don’t know much about your code, but supposing the variable beingShot is true when the player is being hit, you could do something like this:
var healRate: float = 5;
var damageRate: float = 2;
private healTime: float = 0;
function Update(){
if (beingShot){ // while player under fire...
health -= damageRate * Time.deltaTime; // decrement health at damageRate
healTime = Time.time+5; // and set healing start to 5 seconds from now
} else // but when not being shot...
if (Time.time > healTime){ // and if it's time to start healing...
health += healRate * Time.deltaTime; // increment health at healRate
}
health = Mathf.Clamp(health, 0, 100); // clamp health to the 0..100 range
}