- Home /
Armor/defense systeme
Hello ! I try to do a armor/defense systeme.i have life systeme :
#pragma strict
private var life_max : int ;
public var life_current : int ;
function Start (){
life_max = 100
life_current = 100
}
function Update (){
//simulate damage
if(Input.GetKeyUp("t"))
{
life_current -= 10 ;
}
//hud
function OnGUI(){
GUI.Box(Rect(10,10,200,30), life_current) ;
So actually, if i take damage, my player loose 10 life. Now i want create an armor system, and if i have 100 armor when i take 10 damage, my armor should loose 10. and when armor=0 , damage continu on my life.
Any idea ?
Answer by FreeTimeDev · Feb 03, 2014 at 04:17 PM
First, with unity if your variables are myVariable the editor will show them as "My Variable" in the inspector and it's easier to read. It's easier to type (imo) in code as well.
As for your question, add another Armor variable. armor_max, armor_current
if(armor_current > damageTaken)
{
armor_current -= damageTaken;
}
else
{
if(life_current > damageTaken)
{
life_current -= damageTaken;
}
else
{
//player dead
}
}
As a caveat, if you have 3 armor and take 10 damage, it will go straight to health. This was a rather easy question for you to ask since you practically had the code in your script already! You just needed to think about your armor.
Answer by Bloodyem · Feb 03, 2014 at 04:20 PM
Some ideas, yes. Guarantee they will work, not so much, I'm still somewhat of a noob anyway.
First, you need to make sure your script knows you have armor. Then, it needs to see how much armor is left, and once there is no more, take away from life.
And so: var armor : float;
function Update () {
if(Input.GetKeyUp("t"))
{
if (armor >=0)
armor -= 1-;
else
life_current -= 10
}
}
That's all you should need to implement that much, but to make the damage come from an outside source will require more work.
Your code looks solid except that armor >= 0 could get him -1 armor since you're checking if it's only not negative. Which may be okay if he wants armor to take all of the last hit.
Well, if not, he could look at a damage variable and make it so it checks how much damage would be given before applying it.
Your answer
Follow this Question
Related Questions
How can I get my Lives scripts working 1 Answer
WHY IT ISNT WORKING?! 2 Answers
Permutations, and player lights? 0 Answers
real life time 2 Answers
Unity Physics and real Physics 3 Answers