Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Can anyone that knows trigonometry help me with a curve?

Discussion in 'Scripting' started by astracat111, May 18, 2019 at 3:58 AM.

  1. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    465
    Hey there,

    In my rpg I have a float representing how much power an ability has at it's start. It calculates this power from the mana and hp cost (abilities cost mana and hp basically only).

    So, I have this modifier that I apply to the power float. This mod starts at 2.00f and basically becomes less and less the more powerful the ability becomes. My curve might look like this:



    Because I don't have a clue how to do this in a few lines of code using trigonometry, right now my C# method looks like this:

    Code (CSharp):
    1.  private static float GetLessAndLessPowerMod(float maxLessAndLessMod, float PowerFloat)
    2.         {
    3.  
    4.             //The power has to be cut more and more the higher it goes.
    5.             //You don't want to have attacks that are way too powerful.
    6.  
    7.              /*
    8.             50    2.0
    9.             100    1.8
    10.             150    1.6
    11.             200    1.4
    12.             250    1.2
    13.             300    1.0
    14.             350    0.9
    15.             400    0.8
    16.             450    0.75
    17.             500    0.70
    18.             550    0.65
    19.             600    0.60
    20.             650    0.55
    21.             700    0.50
    22.             */
    23.  
    24.             float lessAndLessMod = maxLessAndLessMod;
    25.  
    26.             if (PowerFloat < 50)
    27.             {
    28.                 lessAndLessMod = maxLessAndLessMod;
    29.             } else if (PowerFloat >= 50 && PowerFloat < 100)
    30.             {
    31.                 lessAndLessMod *= 1.00f;    
    32.             } else if (PowerFloat >= 100 && PowerFloat < 150)
    33.             {
    34.                 lessAndLessMod *= 0.95f;
    35.             } else if (PowerFloat >= 150 && PowerFloat < 200)
    36.             {
    37.                 lessAndLessMod *= 0.90f;
    38.             } else if (PowerFloat >= 200 && PowerFloat < 250)
    39.             {
    40.                 lessAndLessMod *= 0.85f;
    41.             } else if (PowerFloat >= 250 && PowerFloat < 300)
    42.             {
    43.                 lessAndLessMod *= 0.80f;
    44.             } else if (PowerFloat >= 300 && PowerFloat < 350)
    45.             {
    46.                 lessAndLessMod *= 0.75f;
    47.             } else if (PowerFloat >= 350 && PowerFloat < 400)
    48.             {
    49.                 lessAndLessMod *= 0.70f;
    50.             } else if (PowerFloat >= 400 && PowerFloat < 450)
    51.             {
    52.                 lessAndLessMod *= 0.65f;
    53.             } else if (PowerFloat >= 450 && PowerFloat < 500)
    54.             {
    55.                 lessAndLessMod *= 0.60f;
    56.             } else if (PowerFloat >= 500) {
    57.                 lessAndLessMod *= 0.55f;
    58.             }
    59.  
    60.             return PowerFloat * lessAndLessMod;
    61.  
    62.         }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    3,551
    Have you considered just using a Unity AnimationCurve object and doing it in the editor?

    Just make one of these:

    Code (csharp):
    1. public AnimationCurve TheCurve;
    Assuming you're using ScriptableObjects to store information about your ability, you can just add the above field in and set it right there in the editor.
     
    TaleOf4Gamers, SparrowsNest and Ryiah like this.
  3. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    465
    @kurt-Decker I haven't, thanks I'll look into it.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    536
    I'm not sure that what you wrote is a curve. You want M to go from 1 to 0.55, by 0.05 every 50, as P goes from 50 to 500. So roughly the whole thing is: float fifties=(P-50)/50; M=1.0f-fifties*0.05f;. Add some ifs to fix the ends.
     
    astracat111 likes this.
  5. astracat111

    astracat111

    Joined:
    Sep 21, 2016
    Posts:
    465
    Gotcha, thank you so much that helps greatly, the solution was a lot simpler than I thought.