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

A value that increases in a curvy way?

Discussion in 'Scripting' started by Rioneer, Jan 15, 2016.

  1. Rioneer

    Rioneer

    Joined:
    Nov 27, 2013
    Posts:
    51
    Hi guys, I have this game where the speed of a ball depends on the level. Now I want to increase the speed of the ball each level but not in a direct way. I want to do it in a curvy way. What kind of algorithm can I use for such thing? Sorry If my words don't make sense to you, Here's a couple of graphs :

    What I can already do :
    img1.png

    What I want to do :
    img2.png

    Thanks in advance fellas :)
     
  2. kozle

    kozle

    Joined:
    Aug 6, 2015
    Posts:
    46
    You can solve this problem in many different ways.
    First is to write a function that contains mathematical function of a curve you desire. By the look at your figure it is logarithmical function.
    it would go something like this:

    private float someConstant = 3.0f;
    private float logarithmicBase = 2.0f;
    private float getCurrentSpeed(float level) {
    return (someConstant * Mathf.Log(level, logarithmicBase));
    }

    You should figure values for someConstat na logarithmicBase in a way that suits your needs.

    The other option is to Use AnimationCurve - http://docs.unity3d.com/ScriptReference/AnimationCurve.html
    There you can draw your curve and call current value in script.
     
    Rioneer likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    AnimationCurve is probably your best option as tweaking it, as well as visualizing the curve, is very straight forward and easy.
     
    Kiwasi, Rioneer and LeftyRighty like this.
  4. Rioneer

    Rioneer

    Joined:
    Nov 27, 2013
    Posts:
    51
    Thanks for the great answers, you have no idea how much these helped me :)