Search Unity

Connecting Score Value to Gravity Scale

Discussion in '2D' started by twojastaramt23, Jan 26, 2020.

  1. twojastaramt23

    twojastaramt23

    Joined:
    Jan 2, 2020
    Posts:
    8
    So I have a script that increases the value of player's score over time and it looks like this.



    Code (CSharp):
    1. playerScore = 0f;
    2. pointIncreasePerSecond = 1;
    3.  
    4. playerScore += pointIncreasePerSecond * Time.deltaTime;
    I want to connect it to gravity scale so every time the score goes up gravity scale also goes up.


    I am still learning so I'll be glad if you can explain your answers to me like to a newbie :]


    It's a 2D ball going down with horizontal movement , the ball's (Player's) gravity is based on RigidBody2D.

    If you need anything more to answer this question just tell me.



    Also I would like to make the player's horinzotal movement harder based on how high the gravity scale is but that's another question.(I am just leaving it there in case anyone knows how can I do that, but that's less important atm).
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    You can use lerp to solve both problems.

    Code (CSharp):
    1. float t = playerScore / maxGravityScore;
    2. float gravityScale = Mathf.Lerp(minGravityScale, maxGravityScale, t);
    3. float movementMod = Mathf.Lerp(minMovementMod, maxMovementMod, t);
    If you want it to go beyond the max, use Mathf.LerpUnclamped instead.
     
  3. twojastaramt23

    twojastaramt23

    Joined:
    Jan 2, 2020
    Posts:
    8
    @Chris-Trueman Thanks for the answer! Yet I have a problem with applying that since I have 2 seperate scripts for horizontal movement and for increasing score value that's connected to gravity scaling(I managed to do that but it could use more math). Maybe I'll just show it, so You could guide me a bit more?

    That's for horinzotal movement
    upload_2020-1-26_16-51-5.png
    upload_2020-1-26_16-52-40.png
    it is connected to both buttons
    upload_2020-1-26_16-53-49.png

    and here's for counting score connected to gravity scale from RigidBody2D

    upload_2020-1-26_16-51-53.png
    upload_2020-1-26_16-52-10.png
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Your movement script needs access to the score script so make a public field in the character movement script and set it in the inspector. Once it has access than its figuring out the math you want. I would look up Mathf.Log it will give you more of a curve, you can also try an AnimationCurve, which you can set in the inspector.

    When it comes down to it, you will need to find out what suits your game the best. Play around with the speed and gravity scale to see how you want the game to react and when. In my game Happy Turd, I use a combination of lerp and smoothDamp to slowly speed up the game as the score increases.