Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I pre-bake math?

Discussion in 'Scripting' started by Not_Sure, Jul 30, 2023.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    So I’m wanting a loop to handle status effects levels.

    Not exactly this, but I want to take say 100 fire damage and get a level of burn applied to a target.

    So let’s say you do 1 fire damage makes level 1.

    2 fire damage makes level 2, and so on so that 100 fire damage would be level 50 burn.

    Obviously doing such a loop would get expensive at say 100,000 fire damage.

    Is there a way I can cut corners here and pre-bake the math?

    Or maybe there’s a better way?

    Especially considering what do I do if I apply more fire to a target already burning?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    What do you mean by pre-bake? Do you mean an algorithm?

    Just figure out some kind of calculation that determines the level of burn based off the value. No need to loop.
     
    Ryiah, Bunny83 and Not_Sure like this.
  3. Elhimp

    Elhimp

    Joined:
    Jan 6, 2013
    Posts:
    71
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
    there's also LookUpTables (pre calculated values in array, if your algorithm is slow/complex)
    https://en.wikipedia.org/wiki/Lookup_table

    note: calling AnimationCurve.evaluate() in large loops can be slow, LUT can help there too.

    or use another thread / jobs / compute shaders, if need to crunch numbers.
     
    Ryiah and Not_Sure like this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    What you described here is not really a pattern. If 1 fire damage gives you level 1 burn and 2 fire damage gives you 2, it would be linear relationship. How do you end up with 50 at 100 damage? You said "and so on" but your last example doesn't make sense. If you can't even explain your desired logic, you can't code it. Animation curves can help to setup non linear relationships between values. However to me it's not really clear what exactly you want to do. When you apply damage to an enemy you usually just want to calculate the level of burn to apply based on the damage. However you have to give more samples to actually identify a pattern. It's possible to find countless equations that would produce your 3 samples. Use a site like this and enter your desired values and desired kind of function. It will give you an equation that produces those values or at least approximates them
     
    PraetorBlue likes this.
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Thanks for the help everyone, but I’m realizing m doing this backwards.

    what I SHOUD be doing is just adding to a value then have it tick away a percentage.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Every time I see a Not_Sure post I get so confused. Their communication sounds coherent but there's always this odd detail off in what they say, and it always stirs up at least a handful of responses, some of which is asking for clarification as well as giving a semblance of an answer.

    Not_Sure then either doesn't respond, or if they do, they certainly never clarify on the confusing part and it is more a "complain about this thing" thread.

    My suspicion is that they use the forums more as a rubber duck, than as an actual discussion. Or at the very most as a one-way transaction.

    Case in point I seldom ever see them respond to someone else, but only create their own threads. And a quick glance at their post history just now sort of confirms this... lots of posts... all their own threads.
     
    POOKSHANK, PraetorBlue and Bunny83 like this.