- Home /
Math Problem
Hiho.
I have a simple question (My english is not the best, that's why I keep it short):
Can someone explain me why this line won't work how expected:
hptemp = hptemp - (damage + BonusHero1) * (2*BonusHero2);
Sample data: hptemp = 10; damage = 1; BonusHero1 = 1; BonusHero2 = 1.2;
If I'm correct the result should be: hptemp = 5.2
Unity's result is hptemp = 10
all variables are from the typ "double".
hptemp = hptemp - (damage + BonusHero1) * 2;
works as expected. (hptemp = 6)
hptemp = hptemp - (damage + BonusHero1) * (damage + BonusHero1) ;
doesn't work as expected (hptemp = 10)
It seem's that everytime I add a second pair of clamps the whole calculation won't work.
I have done this several times in C# with Forms. So I think it's a problem with Unity (Or a problem with with me :/ )
Hmm that's quite interesting, $$anonymous$$aybe try adding more brackets.
hptemp = hptemp - ((damage + BonusHero1) * (2*BonusHero2));
Or try changing everything to a float. I usually only work with floats unless I'm trying to round the number.
I just remade those in normal vs studio console app and I get:
5.2
6
6
for those 3 with your given values. I don't see how this should be different in unity.
Answer by JaredHD · Apr 28, 2017 at 09:12 AM
So I did a quick test and everything seems right. Here is the code I made to test Float and Double:
using UnityEngine;
public class Test : MonoBehaviour
{
float healthFloat = 10, damageFloat = 1, bonusHero1Float = 1, bonusHero2Float = 1.2f;
double healthDouble = 10, damageDouble = 1, bonusHero1Double = 1, bonusHero2Double = 1.2;
private void Start()
{
healthFloat = healthFloat - (damageFloat + bonusHero1Float) * (2 * bonusHero2Float);
healthDouble = healthDouble - (damageDouble + bonusHero1Double) * (2 * bonusHero2Double);
Debug.Log(healthFloat);
Debug.Log(healthDouble);
}
}
Make a script called "Test", and attach that to a gameobject and press play. Both numbers in the console say 5.2.
Just as a check. Make sure you have assigned all the values correctly in your script. If this is to damage an enemy, use a collider/raycast to determine what enemy it is and use the .GetComponet().VariableName; on that enemy to get its health.
I hope this helps
Edit: So in answer to your question of "Can someone explain to me why this line of code won't work." It will work.
Answer by Bunny83 · Apr 28, 2017 at 09:13 AM
Unity uses ordinary C# and uses Mono as runtime. The language works exactly the same. Are you sure that you have checked the content of your variables right at the time where you're using them? I mean something like:
Debug.Log("damage: " + damage + " Bonus1: " + BonusHero1 + " Bonus2: " + BonusHero2);
Debug.Log("hp before:" + hptemp);
hptemp = hptemp - (damage + BonusHero1) * (2*BonusHero2);
Debug.Log("hp after:" + hptemp);
I'm sure that there is some issue with your variables. For example when you declared your variables as public class variables, they will be serialized in the inspector of your component. So they might contain different values you might expect.
I've controlled the variables several times, I haven't found the issue.
But when I look at the inspector the value of BonusHero1 and Bonushero2 are 0. This explained why hptemp = 10.
I initialized both variables with the value 1. (and Bonushero2 was later set to 1.2). I have no Idea why the inspector show a 0.
I "solved" the problem by saving the game. But I still don't know why this happend. I can not reproduce this failure. Even if I delete the savegame.
Thank you for your answer. Looking at the inspector was a crucial note. (Even if I still don't understand what exactly happend.)
Answer by _Keagan · Apr 28, 2017 at 11:17 AM
Just as side note maybe you found the issue by now.. Check that it's executing by adding a print statement saying "executing" or something because like previous answers said the calculation should work. maybe just check if your are doing something you don't mean to like repeatedly setting "hptemp" to 10 somewhere after the calculation. Also this sounds like a problem simple enough to solve in seconds with MonoDevelop's debugger. Here's the link to the page MonoDevlop's Debugger Just read through the article, it helped me to know debugging when I started out and am still a begginer as a disclaimer. @xardius
Your answer
Follow this Question
Related Questions
Rotate a point around another point? 2 Answers
Clamping between everything but the min/max values 1 Answer
how to normalize a value to a range between 0 and 1 4 Answers
Rotation problem: Check is failing/ endlessly spins 1 Answer
What's a more efficient way to sort an array into multiple groups? 1 Answer