- Home /
Deducting a value from a total vaule
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!
Answer by aldonaletto · Jul 19, 2012 at 03:27 AM
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 }
I think I need to rephrase my question:
The way I've mixed up that strategy of my game is that you, a space probe, have a total 100 energy. This energy is used as 'resource' to fire weapons and move around the screen. Every second the space probe's machine gun is fired (left mouse click), 2 energy points are deducted from the total energy value of 100 every second the mouse is held down. Every second the movement button(s) are held down (left, right, up, down) 5 health is taken away from the energy value. Then once there is no activity (that is, no weapons fired, no movement) for 1 second, the health is to regenerate by 5 points per second.
Hope that clears things up a bit!
If you could put notes in the script to help me change the script for different types of weapons (i.e. change the instantiate machinegunfire to a missile...?)
Thanks!!!
Your answer
Follow this Question
Related Questions
Recharge delay on shield regen through coroutines 2 Answers
GUI box scaling issue [C#] 0 Answers
Health bar help 1 Answer
(Health bar) Remove lives properly 1 Answer