- Home /
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();
}
}
}
$$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?
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.
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(); } }
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;
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.
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.
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.
True but now how do I make the bonus stop when the duration ends?