Search Unity

Progressive score (1,2,4,8,16 etc)

Discussion in 'Scripting' started by dacomb, Mar 18, 2019.

  1. dacomb

    dacomb

    Joined:
    Mar 14, 2019
    Posts:
    5
    Hi All,

    I have a simple (but hard to solve) question,

    How can I make my score grow exponentially without a massive rewrite of my code, banging my head against the wall at the moment.

    Currently I am referring back to my manager with " manager.UpdateScore (1); " upon 3d party review of my beta, the consensus is to increase the score better than 1+1+1+1 etc.

    Is it possible to use greater than arguments eg
    "score = greater than 10, so plus 2 points rather than 1"
    "Score = greater than 30, so plus 3 points rather than 2" etc

    I have tried a few ways but my syntax is off, any help appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gate : MonoBehaviour {
    6.    
    7.  
    8.     public AudioSource scoreAudio;
    9.    
    10.  
    11.     GameManager manager;
    12.     bool addedScore;
    13.    
    14.     void Start(){
    15.    
    16.         manager = GameObject.FindObjectOfType<GameManager>();
    17.     }
    18.    
    19.     void OnTriggerEnter(Collider other){
    20.         if(!other.gameObject.transform.root.CompareTag("Player") || addedScore)
    21.             return;
    22.        
    23.         addedScore = true;
    24.         manager.UpdateScore (1);
    25.         scoreAudio.Play();
    26.     }
    27. }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    Not sure what you want to do here. Do you want the players score to double any time the player scores points? Do you want only the gates to have an increasing points value each time you activate one?
     
  3. dacomb

    dacomb

    Joined:
    Mar 14, 2019
    Posts:
    5
    Update the score better than +1 but not basic multiplication (i can do both of those)

    Ideally increase the score at set points eg,

    1 to 5 plus one point

    6 to 10 plus two points etc

    Open to any suggestions to improve my basic score board.
     
  4. dacomb

    dacomb

    Joined:
    Mar 14, 2019
    Posts:
    5
    Can i not do something along the lines of

    Add score +1 unless score = 5, if so plus 2 points, unless score = 10, plus 3 points etc.

    I’m sure it can be done, just not sure how to approach it with out writing lots.
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Is this what you are trying to do?
    Code (CSharp):
    1.     public int CurrentScore;
    2.     public int ScoreMultiplerPoint = 5;
    3.  
    4.     public void AddToScore()
    5.     {
    6.         var score = 1;
    7.  
    8.         if (CurrentScore / ScoreMultiplerPoint > 0)
    9.         {
    10.             score *= CurrentScore / ScoreMultiplerPoint + 1;
    11.         }
    12.  
    13.         CurrentScore += score;
    14.  
    15.         Debug.Log("Score Added = " + score + " Current Score = " + CurrentScore);
    16.     }
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    I assume you mean >= (greater than or equal to) 5, 10 etc.?
    If your score is an int than the formula for this is just

    Score += (Score/5)+1;
     
    dacomb likes this.
  7. dacomb

    dacomb

    Joined:
    Mar 14, 2019
    Posts:
    5
    Code (CSharp):
    1.  score += (score/10)+0*1;
    Thanks kdgalja, sent me on the right path (the +0*1 is to ensure the game starts with a score of zero)
     
  8. dacomb

    dacomb

    Joined:
    Mar 14, 2019
    Posts:
    5
    Thanks WarmedxMints, could not get to work how I wanted, appreciate the time you have taken though. I will re evaluated for further levels.
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The +0*1 doesn't do anything at all (unless those operators are overloaded). Whatever score the game starts with is going to be determined by how you initialize the "score" variable, not how you increment it.
     
    rakkarage likes this.