Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Dont understand how meth works

Discussion in 'Scripting' started by ItsMe1423, Apr 3, 2024.

  1. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    how do I transform this equasion into somthing the script can rid because I can seem to be able to do it

    upload_2024-4-3_22-54-6.png
    this is what I have come up with and its not working:
    Code (CSharp):
    1.         int stat = Mathf.RoundToInt((Mathf.Pow(player.Str, (5 / 4)) * (Mathf.Pow(player.Dex, (2 / 3)) + 1)) / 10);
    2.  
    for Str = 16 and Dex = 12 the result is supposed to be 20 (19.smth but I round it) and it comes out 3 (3.2 but again I round it)
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    752
    Division of integers rounds towards zero. You have to use a float for one of the parameters in order for the division to work as you intend (compiler treats it as a float operation instead of an integer operation).
     
    Bunny83 likes this.
  3. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    What do you mean by Division of integers? I know what ints are obviously
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    752
    Bunny83 likes this.
  5. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    ok tnx
    ok tnx
    btw I know this is not unity-specific I have just gotten accustomed to the Unity forums already
    so how do I turn one of the to float? just add .0 or smth like that?
     
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    An integer division happens when you divide an integer by another integer. If that happens the result will be an integer again. So you can not get any fractional numbers as result. So something like
    5/4
    is an integer division and will result in the number
    1
    while the division
    2/3
    will result in
    0
    . You need to use float literal values instead. As soon as one of the operands is a float, it would be a float division and the result would be a float. So do

    Code (CSharp):
    1.  int stat = Mathf.RoundToInt((Mathf.Pow(player.Str, (5f / 4f)) * (Mathf.Pow(player.Dex, (2f / 3f)) + 1f)) / 10f);
    Not all
    f
    are necessary here, but when you work with floats, it's best to use the proper literal value.
     
    orionsyndrome and Spy-Master like this.
  7. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    oh ok thank you
     
  8. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    752
    It needs to also have an f, so for instance 1.0f or just 1f, so it’s treated as a float instead of a double. Mathf.Pow expects floats. Integer types are automatically converted to float for the call, double is not.
     
    Ryiah and Bunny83 like this.
  9. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    It works now thank you all for helping me
     
  10. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,153
    A useful tip: make a general function for integer ratios that should evaluate as floating point. I do that all the time because you can't make an integer division error by accident.
    Code (csharp):
    1. static public float ratio(int a, int b) => a / (float)b;
    (As soon as one operand is explicitly cast to float, the other is converted automatically and the operation is inferred as floating point division. It also looks nicer in the user code, you don't need to cast there, and you don't need f suffix etc.)

    Edit:
    Obviously no need to do this when you're working with literal values. This is more like a general approach for variables.

    Edit2:
    Also I'm astonished that no one made a joke out of your title typo :)
    So, here's another answer to your question :D

    Edit3:
    This is how I'd write your expression
    Code (csharp):
    1. var strTerm = pow(str, 5f/4f); // you could just write 1.25f but these divisions are
    2. var dexTerm = 1f + pow(dex, 2f/3f); // converted to value constants in compile time anyway
    3. int stat = roundToInt(strTerm * dexTerm / 10f);
     
    Last edited: Apr 5, 2024
    Sluggy, mopthrow, Nad_B and 5 others like this.
  11. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    Lol I thought to my self how math is supposed to be typed but I just went with what came out first
     
    orionsyndrome likes this.
  12. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    Yesterday I was going to write a joke about it, but felt lazy :D
     
    orionsyndrome likes this.
  13. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,960
    Not even one Breaking Bad joke?
     
  14. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    what is var, I see it constantly being used but I am not really sure what it is
     
  15. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    134
  16. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,153
    In this context, it's the same as
    float
    . You can find much more about it here.
     
  17. ItsMe1423

    ItsMe1423

    Joined:
    Sep 23, 2020
    Posts:
    78
    ok tnx