Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

Score And Level System For Unity3D :

Need a generic scoring and level behaviour for Unity3D? This should work for you. I use a script similar to this in quite a few of my games.

How to use: Copy and paste this code in to a new C# script named Scoring.cs and then attach the script to your player object, GUI object, or whatever needs to keep track of a score.

How it works: The player’s level and score are tracked by way of two simple integer variables. Score starts at 0, and level starts 1. You can change these starting values by assigning new ones in some other script which is useful when you restore a previously saved game.

A list of scores is stored and compared against each Update() to see if the player has enough points to level up. The level up can be as simple as the points scored in Space Invaders or more complex such as tracking experience in an RPG.

Usually in a video game, the first few levels have a non-linear progression of points required to level up. You can specify this non-linear progression by entering the points required for each of the first few levels in the Inspector. Take a look at the member variable m_nextLevelScore, which is exposed in the Unity3D inspector as an array of integers. Once you have specified the first few point values (default values are given in the source code), you specify how many more points are required per level beyond the non-linear progression.

Take a look at the code, at the top of the class you’ll see that the member variable m_nextLevelScore has some default values. To go from level 1 to level 2 requires 3,000 points. From level 2 to level 3, 7,000 points are required. And so on. When you hit the last value in the list, 80,000 points, the progression becomes linear, so the member variable m_nextLevelScoreProgression takes over, requiring 100,000 points for the level after level 10, i.e. the 80,000 points. Every level from then on will require an additional 100,000 points.

The maximum level that can be reached is handled by the member variable m_maximumLevel. The default is set at 100, but you can set it to any value you like. The function that checks for level up, CheckForLevelUp() is virtual, so you can override it in an inherited class to instead handle a computed level progression similar to Fallout 3.

You can specify a sound effect that will play when the player levels up using the m_nextLevelSound member variable which is exposed in the Inspector. You can specify multiple sounds, or just one, or even none at all. If you only have a single level up sound effect, the same one will be used repeatedly. If you have more than one, but not a distinct sound effect for every individual level then the last sound effect specified in the list will be used for each level beyond the number of sound effects available.

Assumptions: Negative levels are not supported. Levelling down is not supported. Negative scores work, but probably shouldn’t be used.

Note: Oops! Posted the wrong version of the code earlier that was for a specific piece of game logic. This one fixes the oversight. If you downloaded the other code already, switch it out for the updated version posted below. It gets rid of the limitation on ridiculously high maximum levels.

The code:


/*-----------------------------------------------------------------------------
 * Scoring.cs - Keeps track of the player's score and levels up as required.
 * Copyright (C) 2010 Justin Lloyd
 * http://www.otakunozoku.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 * 
-----------------------------------------------------------------------------*/

using UnityEngine;
using System;
using System.Text;


[RequireComponent(typeof(AudioSource))]
public class Scoring : MonoBehaviour
{
    /// <summary>
    /// Audio clips to play for each level up sound.
    /// </summary>
    public AudioClip[] m_nextLevelSound;

    /// <summary>
    /// Maximum permitted level.
    /// </summary>
    public int m_maximumLevel = 100;

    /// <summary>
    /// The list of scores required to advance to the next level.
    /// </summary>
    public int[] m_nextLevelScore = { 0, 3000, 7000, 12000, 18000, 25000, 34000,
44000, 56000, 69000, 80000 };

    /// <summary>
    /// The number of required points to score to advance to the next level once
the score has gone beyond the provided list of points.
    /// </summary>
    public int m_nextLevelScoreProgression = 100000;

    /// <summary>
    /// The player's current score.
    /// </summary>
    private int m_score = 0;

    /// <summary>
    /// The player's current level.
    /// </summary>
    private int m_level = 1;

    /// <summary>
    /// The minimum level permitted.
    /// </summary>
    private const int MinimumLevel = 1;

    /// <summary>
    /// The player's score.
    /// </summary>
    public int Score
    {
        get
        {
            return m_score;
        }

        set
        {
            m_score = value;
        }

    }

    /// <summary>
    /// Adjust the score by the specified number of points. Negative values
    /// will subtract points.
    /// </summary>
    /// <param name="points" />Number of points to adjust the current score
by.</param>
    public void AdjustScore(int points)
    {
        m_score += points;
    }

    /// <summary>
    /// Adjust the current level by the specified number of levels. Negative
    /// values will subtract levels. Does not adjust the score to match. The
    /// new level will be clamped to within the maximum permitted level.
    /// </summary>
    /// <param name="levels" />Number of levels to adjust the current level
by.</param>
    public void AdjustLevel(int levels)
    {
        m_level = Mathf.Clamp(m_level + levels, MinimumLevel, m_maximumLevel);
    }

    /// <summary>
    /// The player's current level. Specifying a new level will ensure that the
    /// new level is clamped to the maximum permitted level.
    /// </summary>
    public int Level
    {
        get
        {
            return m_level;
        }

        set
        {
            m_level = Mathf.Clamp(value, MinimumLevel, m_maximumLevel);
        }

    }

    /// <summary>
    /// Play the audio for level up sound.
    /// </summary>
    public void PlayNextLevelSound()
    {
        int levelUpIndex = Mathf.Clamp(m_level, MinimumLevel, 
m_nextLevelSound.Length - 1) - 1;
        if (m_nextLevelSound[levelUpIndex] == null)
        {
            return;
        }

        this.audio.PlayOneShot(m_nextLevelSound[levelUpIndex]);
    }

    /// <summary>
    /// Checks for completion of the current level and advances to the next
    /// level if the score is high enough.
    /// </summary>
    public virtual void CheckForLevelUp()
    {
        // if we have reached the maximum level, do nothing
        if (m_level >= m_maximumLevel)
        {
            return;
        }

        // check for the next required score
        int nextLevelScore = 0;
        // if there are no more scores in the level score progression array
        //      switch over to linear progression
        //      otherwise, use the non-linear progression
        if (m_level >= m_nextLevelScore.Length)
        {
            nextLevelScore = (m_level - m_nextLevelScore.Length + 1) *
m_nextLevelScoreProgression;
        }
        else
        {
            nextLevelScore = m_nextLevelScore[m_level];
        }

        // if we have the required score to level up, advance to the next level
        if (m_score >= nextLevelScore)
        {
            m_level = Math.Min(m_level + 1, m_maximumLevel);
            PlayNextLevelSound();
        }

    }

    void Update()
    {
        CheckForLevelUp();
    }

}

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!