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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Algorithm for RPG XP

Discussion in 'Scripting' started by DarkWolfSoftware, Jun 13, 2015.

  1. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    I am not very good with algorithms to begin with but was wondering if anyone had an example script for "Level Up/Reward" Experience I could base mine on. I don't want to "copy" your code, just something to learn from.

    Thanks.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This is from my Flash framework, should be easy enough to convert to JS or C#:
    Code (csharp):
    1.  
    2.     public static function calculateLevel(currentXp : Number, scale : Number) : Number
    3.      {
    4.        return Math.floor((scale + Math.sqrt(scale * scale - 4 * scale * ( -currentXp))) / (2 * scale));
    5.      }
    6.  
    Scale is just an arbitrary constant to decide how "big" the levels are. I usually used 500.
     
  3. DarkWolfSoftware

    DarkWolfSoftware

    Joined:
    Jun 4, 2015
    Posts:
    31
    This will help me tremendously! Thank you!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That formula seems to be more complex then needed. Plus it's recursive. I would suggest something more like:

    Code (CSharp):
    1. // quadratic growth
    2. expNeeded = a * level * level + b * level;
    Or set it up manually with an animation curve.