I’m trying to make a player spawn at lv 1 (beginning of game) then save the data later. But when the player spawns, its hp remains 0, which means the script has to destroy the player. How do I start with full hp? Heres my script:
function Update ()
{
if(level==0)
{
level+=1;
}
hp = Mathf.RoundToInt((((ivhp+(2*basehp)+(evhp/4)+100)*level)/100)+10);
currenthp = hp;
So he should begin with 10hp? Why not just assign this in the Awake() function? Also, why check every frame for the current hp? You could set it so that only when the player receives damage the check is performed and if then the hp is lower than or equal to 0, kill the player.
var levelHPModifier:float;
function Awake(){
hp=10+levelHPModifier*level //or whatever your algorithm is
currenthp=hp
}
var ApplyDamage(damage:int){
currenthp-=damage;
//Code for visuals and other stuff goes here
}
var levelUp(){
hp+=levelHPModifier*level
currenthp+=levelHPModifier*level
}
I am going to assume hp is the maximum hp and currenthp is the character’s hitpoints at any given time.
That is pretty much everything you need to make it functional , without setting the hp every frame.
Setting the hp to a certain number every frame may lead to the player ignoring getting damage , since his health is set to a number every frame.
The function Awake sets the player’s ax hp and current hp depending on his level , right when the map is loaded , so your character will not start with 0hp.
The rest of the functions are for generic usage , you can use ApplyDamage with a positive number to do damage , or with a negative to heal your player.
Level up increases the maximum hp for the character , and adds the amount of maximum hp added to the player.
So he should begin with 10hp? Why not just assign this in the Awake() function? Also, why check every frame for the current hp? You could set it so that only when the player receives damage the check is performed and if then the hp is lower than or equal to 0, kill the player.
– asafsitnerI still think giving hp an initial value and only chancing it when the player levels up is better than checking it every frame.
– asafsitner