Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jonathanbooker2911 · Nov 29, 2018 at 07:59 AM · scripting problemscript.scripting beginnerscriptingbasicsscriptingproblem

Trying to find the highest number than add it to itself.

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class HumanRacial : MonoBehaviour { public float agility; public float strength; public float intellect; public float highestStat; public float coolDown = 120; public float coolDownTimer; public float duration; public float durationTimer;

 public void Start()
 {
     
 }

 public void Update()
 {          
     Active();
     Cooldown();
 }

 public void RacialBonus()
 {
     float maxValue = Mathf.Max(agility, strength, intellect);

     if ((agility == maxValue && (agility == strength || agility == intellect)) || (strength == maxValue && strength == intellect))
     {
         //Two or more of our attributes are equally maxValue
         //Decide how to deal with this situation

     }
     else if (agility == maxValue)
     {
         agility *= 2f;
     }
     else if (strength == maxValue)
     {
         strength *= 2f;
     }
     else if (intellect == maxValue)
     {
         intellect *= 2f;
     }
 }

 private void Cooldown()
 {

     if (duration > 0)
     {
         durationTimer -= Time.deltaTime;
     }
     if (durationTimer < 0)
     {
         durationTimer = 0;
     }

     if (coolDownTimer > 0)
     {
         coolDownTimer -= Time.deltaTime;
     }
     if (coolDownTimer < 0)
     {
         coolDownTimer = 0;
     }
 }

 private void Active()
 {
     if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
     {
         coolDownTimer = coolDown;
         durationTimer = duration;
         RacialBonus();
     }
 }

}

Comment
Add comment · Show 3
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 ShadyProductions · Nov 29, 2018 at 08:02 AM 1
Share

$$anonymous$$aybe you should explain your problem a bit ins$$anonymous$$d of just posting a bunch of code and a title which doesn't give a lot of information about the actual problem you are having?

avatar image ecv80 · Nov 29, 2018 at 09:06 AM 0
Share

What @ShadyProductions said. As a rule of thumb when asking questions, one should at the very least explain what the problem is or what is currently happening, all the better the more specific one is, giving error codes and the lines where these point to, or the problematic variable or methods, AND what one intends to achieve, the expected behavior, being as specific as possible too. That's if one expects to get any or good answers at all. Failing to do so will probably yield misleading or no answers, because most of us can't read $$anonymous$$ds nor have crystal balls.

avatar image jonathanbooker2911 ecv80 · Nov 29, 2018 at 10:19 AM 0
Share

I thought the title was enough I was trying to find the highest value out of all three numbers. Than adding it to one of the previous values. I did it with if statements. I want to know if they was a more efficent way to do it. public void Start() {

} public void Update() {
Active(); if (duration > 0) { durationTimer -= Time.deltaTime; } if (durationTimer < 0) { durationTimer = 0; } if(coolDownTimer > 0) { coolDownTimer -= Time.deltaTime; } if(coolDownTimer < 0) { coolDownTimer = 0; } } public void RacialBonus() { highestStat = $$anonymous$$athf.$$anonymous$$ax(agility, strength, intellect); float myHighestStat = $$anonymous$$athf.$$anonymous$$ax(agility, strength, intellect); highestStat += myHighestStat; if((agility > intellect) && (agility > strength)) { agility += highestStat; } if ((strength > intellect) && (agility > strength)) { strength += highestStat; } if ((intellect > agility) && (intellect > strength)) { intellect += highestStat; } } private void Active() { if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && coolDownTimer == 0 && durationTimer == 0) { coolDownTimer = coolDown; durationTimer = duration; RacialBonus(); } }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by calpolican · Nov 29, 2018 at 08:06 AM

You're overwriting the value. Just store it in a temporary variable.

And try to use lower case for variables.

  float myHighestStat= Mathf.Max(Agility, Strength, Intellect);
  HighestStat += myHighestStat;   





Comment
Add comment · Show 1 · 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 jonathanbooker2911 · Nov 29, 2018 at 08:50 AM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

[System.Serializable] public class HumanRacial : $$anonymous$$onoBehaviour { public float agility; public float strength; public float intellect; public float highestStat; public float coolDown = 120; public float coolDownTimer; public float duration; public float durationTimer;

 public void Start()
 {
     
 }

 public void Update()
 {          
     Active();


     if (duration > 0)
     {
         durationTimer -= Time.deltaTime;
     }
     if (durationTimer < 0)
     {
         durationTimer = 0;
     }

     if(coolDownTimer > 0)
     {
         coolDownTimer -= Time.deltaTime;
     }
     if(coolDownTimer < 0)
     {
         coolDownTimer = 0;
     }
 }

 public void RacialBonus()
 {
     highestStat = $$anonymous$$athf.$$anonymous$$ax(agility, strength, intellect);
     float myHighestStat = $$anonymous$$athf.$$anonymous$$ax(agility, strength, intellect);
     highestStat += myHighestStat;

     if((agility > intellect) && (agility > strength))
     {
         agility += highestStat;
     }
     if ((strength > intellect) && (agility > strength))
     {
         strength += highestStat;
     }
     if ((intellect > agility) && (intellect > strength))
     {
         intellect += highestStat;
     }
 }

 private void Active()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D) && coolDownTimer == 0 && durationTimer == 0)
     {
         coolDownTimer = coolDown;
         durationTimer = duration;
         RacialBonus();
     }
 }

}

I tried your changes but the only way I know hot to fix this is with if statements.

avatar image
0

Answer by ecv80 · Nov 29, 2018 at 01:04 PM

Okay, so if I got you right, you want to double whatever the highest attribute is. There really isn't much room for efficiency improvement from what you have already done in your last code. Another way to put it would be:

 float maxValue=Mathf.Max(agility, strength, intellect);
         
         if ( (agility==maxValue && (agility==strength || agility==intellect)) || (strength==maxValue && strength==intellect)) {
             //Two or more of our attributes are equally maxValue
             //Decide how to deal with this situation
 
         }
         else if (agility==maxValue) {
             agility*=2f;
         }
         else if (strength==maxValue) {
             strength*=2f;
         }
         else if (intellect==maxValue) {
             intellect*=2f;
         }

You might find this clearer to work with... or not... that's entirely your preference.

EDIT: But all in all, @calpolican 's answer should be the valid answer to the original question. Again, if I got all this right.

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 ecv80 · Nov 29, 2018 at 01:09 PM 0
Share

Tell me if I missed the point. Also, I don't wanna be a pest but I had to copy your code from your reply comment to my comment in your question and format it manually only to realize it's the same code you posted as an answer, so please use code tags. It's also a good idea to edit your original question to reflect changes along the way.

avatar image jonathanbooker2911 · Nov 29, 2018 at 07:04 PM 0
Share

True but now how do I make the bonus stop when the duration ends?

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

182 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

How do I make a photography function? 0 Answers

How do I make my projectile deal damage to a target it hit. 1 Answer

Need help fixing my script 1 Answer

how can I change a light with multiple triggers. ? 0 Answers

How to lock an int as a playerPref? 1 Answer


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