Search Unity

Incrementing weight based on current scene number?...

Discussion in 'Scripting' started by strongbox3d, May 22, 2015.

  1. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Hello guys,

    This is what I am trying to achieve:

    I want to increment the weight of a fish depending on the current loaded level number. The fish can weight between 5 and 100 pounds. There are 10 levels in the game.

    So say that the fish should weight 5 pounds in the first level and increase by level up to 100 in the final level.

    What kind of calculation do I have to do in order to the fish have a proportional weight depending the level the player is at, programmatically?

    I appreciate any advice on this.

    Regards,
    Carlos
     
  2. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
  3. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    Thank you very much alucardj! I appreciate your help. So if I understood the logic here... You take the current game level and subtract 1 from it to get the float lvl... Then the weight equals the maximum weight - 5, divided by the maximum game levels - 1, and add 5 as a constant?... Is this the underlaying logic of your algorithm?
    Regards,
    Carlos
     
  4. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    I'm not very good at explaining....

    Code (csharp):
    1. float w = ( ( (maxValue - minValue) / totalLvl ) * currentLvl ) + minValue;
    lvl can be derived from loadedLevel, spawnWave, etc.
    Modify this so the minLvl is 0, and the totalLvls is maxLvl - 1. (much like an array index reference)
    eg if minLvl is 1 and maxLvl is 10, modified would be 0 and 9.

    if you just needed a value between 0 and a maxValue, that would be easy :
    Code (csharp):
    1. float w = ( maxValue / totalLvl ) * currentLvl;
    this part ( maxValue / totalLvl ) determines each increment per lvl.
    Multiplied by the curent lvl gives result.
    eg if currentLvl is 0, w is 0; if currentLvl is totalLvl, w = maxValue;

    but to return a result with a minValue, this needs to be factored in :
    Code (csharp):
    1. float w = ( ( (maxValue - minValue) / totalLvl ) * currentLvl ) + minValue;
    so now this part ( (maxValue - minValue) / totalLvl ) determines an increment minus the minValue that is always added.
    Multiplied by the curent lvl, then adding the minValue gives result.
    eg if currentLvl is 0, w is minValue; if currentLvl is totalLvl, w = maxValue;

    clear as mud?! sorry, really am bad at explaining things. hope the pseudocode line at the top helps.
     
  5. strongbox3d

    strongbox3d

    Joined:
    May 8, 2012
    Posts:
    860
    You have been a great help alucardj, I really appreciate it very much. I understand totally now. I actually created a helper class with the static function that do this for me based on your explanation and it works perfectly!

    Thank you very much!
    Regards,
    Carlos