Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ariath · Feb 03, 2014 at 03:57 PM · lifearmor

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 ?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FreeTimeDev · Feb 03, 2014 at 04:23 PM 0
Share

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.

avatar image Bloodyem · Feb 03, 2014 at 04:25 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges