Search Unity

Trying to make a score multiplier system.

Discussion in 'Scripting' started by ecnalyr, Jun 8, 2010.

  1. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    I am trying to make a score multiplier system that works as follows:

    I have 5 different types of objects spawning on screen at any given time, the only difference in these objects is their size. (object1 - object5)

    I want to reward the following behaviors with score multipliers:

    Killing an object of the same size that you killed previously earns the player a 1.25 multiplier.

    Killing an object of sequential size (either one size bigger or one size smaller) relative to the previously killed object earns the player a 1.5x multiplier.

    I want these multipliers to be cumulative if the player maintains the same type of combo (ie if the player kills 5 objects consecutively that are the same size, the player earns normal points for the first kill, 1.25x points for the second, 1.25x1.25x points for the third, etc).

    I do not want these multipliers to be cumulative if the player moves on to a different combo type or breaks the combo all together.

    I have some code I am 'brainstorming' with, but I know the logic is flawed (it would not properly recognize if a player continued a combo, for example).

    Here is that conceptual code:

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     if (//Object1 is killed)
    5.     {
    6.         if (Object1 is killed)
    7.         {
    8.             1.25x multiplier (for killing the same type of Object)/*display notification of this on screen*/
    9.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    10.         }
    11.             if (Object2  or Object5 is killed)
    12.         {
    13.             1.5x multiplier (for killing the next highest or lowest size)/*display notification of this on screen*/
    14.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    15.         }
    16.         else
    17.         {
    18.             end multiplier
    19.         }
    20.         //wait for next Object to be killed, if it starts a combo, allow combo to start to build
    21.         //if combo is not continued, or if combo was not started, end routine (else?) and assign points
    22.         /*consider using a while statement with a variable that is turned on and off to 'wait' for next Object killed*/
    23.     }
    24.     if (//Object2 is killed)
    25.     {
    26.         if (Object2 is killed)
    27.         {
    28.             1.25x multiplier (for killing the same type of Object)/*display notification of this on screen*/
    29.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    30.         }
    31.             if (Object3  or Object1 is killed)
    32.         {
    33.             1.5x multiplier (for killing the next highest or lowest size)/*display notification of this on screen*/
    34.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    35.         }
    36.         else
    37.         {
    38.             end multiplier
    39.         }
    40.         //wait for next Object to be killed, if it starts a combo, allow combo to start to build
    41.         //if combo is not continued, or if combo was not started, end routine (else?) and assign points
    42.         /*consider using a while statement with a variable that is turned on and off to 'wait' for next Object killed*/
    43.     }
    44.     if (//Object3 is killed)
    45.     {
    46.         if (Object3 is killed)
    47.         {
    48.             1.25x multiplier (for killing the same type of Object)/*display notification of this on screen*/
    49.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    50.         }
    51.             if (Object2  or Object4 is killed)
    52.         {
    53.             1.5x multiplier (for killing the next highest or lowest size)/*display notification of this on screen*/
    54.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    55.         }
    56.         else
    57.         {
    58.             end multiplier
    59.         }
    60.         //wait for next Object to be killed, if it starts a combo, allow combo to start to build
    61.         //if combo is not continued, or if combo was not started, end routine (else?) and assign points
    62.         /*consider using a while statement with a variable that is turned on and off to 'wait' for next Object killed*/
    63.     }
    64.     if (//Object4 is killed)
    65.     {
    66.         if (Object4 is killed)
    67.         {
    68.             1.25x multiplier (for killing the same type of Object)/*display notification of this on screen*/
    69.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    70.         }
    71.             if (Object5  or Object3 is killed)
    72.         {
    73.             1.5x multiplier (for killing the next highest or lowest size)/*display notification of this on screen*/
    74.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    75.         }
    76.         else
    77.         {
    78.             end multiplier
    79.         }
    80.         //wait for next Object to be killed, if it starts a combo, allow combo to start to build
    81.         //if combo is not continued, or if combo was not started, end routine (else?) and assign points
    82.         /*consider using a while statement with a variable that is turned on and off to 'wait' for next Object killed*/
    83.     }
    84.     if (//Object5 is killed)
    85.     {
    86.         if (Object5 is killed)
    87.         {
    88.             1.25x multiplier (for killing the same type of Object)/*display notification of this on screen*/
    89.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    90.         }
    91.             if (Object1  or Object4 is killed)
    92.         {
    93.             1.5x multiplier (for killing the next highest or lowest size)/*display notification of this on screen*/
    94.             tell script to wait for another Object to be killed and find out if it continues this combo or ends the combo
    95.         }
    96.         else
    97.         {
    98.             end multiplier
    99.         }
    100.         //wait for next Object to be killed, if it starts a combo, allow combo to start to build
    101.         //if combo is not continued, or if combo was not started, end routine (else?) and assign points
    102.         /*consider using a while statement with a variable that is turned on and off to 'wait' for next Object killed*/
    103.     }
    104. }
    105.  
    Is there anyway I could go about simplifying this code (a lot of if/else statements seems like it wouldn't be the most effective way of doing this)?

    And, aside from simplifying, how would I go about actually making the code work as I intend it to do so? I'm not sure how to make the script 'wait' for another object to be destroyed (I'd want it to only wait maybe 2 seconds because if the player took longer than 2 seconds to destroy another object, they don't deserve a combo), nor am I aware of how to make the script check to see if the player is continuing the combo or ending the combo after the second object is destroyed (I suppose I could do a LOT of redundant if/else statements, but that seems very inefficient).

    Any help would be appreciated.
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Your pseudo-code is a bit redundant. I'd focus less on which specific objects were killed and more on their relative sizes:

    Code (csharp):
    1. if (objectSize - previousObjectSize == 1)
    2.     multiplier *= 1.5;
    And you can use Time.deltaTime to create a timer that resets your multiplier after two seconds. Or use a coroutine if you actually need your code to wait:

    http://download.unity3d.com/support/documentation/ScriptReference/Coroutine.html
     
  3. ecnalyr

    ecnalyr

    Joined:
    May 23, 2010
    Posts:
    38
    Thank you very much, got it working and I got it streamlined.
     
  4. davidsirmons

    davidsirmons

    Joined:
    Mar 16, 2014
    Posts:
    190
    Sorry to resurrect an old thread if that's a problem. Similar to what he was doing, I'm trying to get a score value for individual 2d objects based on their name. I have about ten so far, with more to arrive soon. I've trudged through scripting files and even added an instinctual guess or two onto a few lines (not a programmer) and gotten most things to work. But I'm wondering...is that above code:

    ------------------

    if (objectSize - previousObjectSize == 1)

    multiplier *= 1.5;

    --------------------------------------

    ....all that he required in the C# file? Further, rather than looking for object size, what about getting a specific object name, or since it's a prefab I set up, maybe I can find some way to make this work with that? Please share your insights if you would. I'd be truly grateful!