- Home /
how do i make a script that calculate how much damage did i loss
hey, i have a player with 100 health points. everytime i get hit by an enemy, i lose a random number between 5-20 hp. i have made a prefab with the 3d text, created a font and made an animation to the text so it will be above the head of the player and go up and disapear. the text in that prefab represent the number of health points that i have loss. like if i lost 10 hp (health points) than it will instantiate the prefab with the text "10" on it. the problem is, the hp i lose is random and i dont know how to make it work... thanks.
A straightforward answer is storing the random damage in a variable, then using that variable to update the text on your 'damage value prefab'. Similar with what Tylo suggested below. You did, however, reply to him that you somehow could not use that action plan. $$anonymous$$ay we know how you calculate that random damage and why you can't simply store it in a variable?
Answer by tylo · Aug 28, 2010 at 12:48 AM
This is pretty simple.
Your HP is stored in a variable, right?
Just update what the 3D text says when you lose HP like this:
YourText.Text = hp;
If that doesn't work, you'll need someway to change your hp number value to a string. That really depends on which language you are using, I think.
EDIT
Since you clarified that this is displaying the damage done, try looking at it this way.
I am unsure how you are causing your HP to deplete. It could be that the enemy is directly depleting your HP. Or it could be that your player receives the value of damage done, and then depletes his own HP.
In either case...what you need to do is this:
YourText.Text = damageDone;
If for some reason you can't do that, then you can do this:
int temp = hpBeforeDamage;
/* Damage is dealt */
YourText.Text = hpBeforeDamage - currentHP;
Sorry i probably didnt explain brifly... i need to write in the text the HP that i have lost, not the current hp, i have an hp bar for that but thanks for the answer.
Oooh, I understand now. Sort of like the -32 from getting hit, etc.
I'll edit my answer.
doesnt really help me, but i have think of something now, it ill make a var damage (And i have another var in the script called curHealth(current health)) and than ill say both of them will be the same only that the var damagae will have a delay of 1 second until it will be like the curHealth. and than in another script ill say if(damage != curHealth) for(var n=1, n<2, n++) { { Instantiate(HP); HP.text = damage - curHealth; } }
Your answer
Follow this Question
Related Questions
Ranged damage not working 1 Answer
Health Bar fire damage 1 Answer
Universal Damage Sytem 2 Answers
Enemy Health, Player Damage 0 Answers
Help with fire damage on a health bar? 2 Answers