Linking your Achievements/Stats to your in-game functionality

Overview

Using the demo scene is great, but you bought the product as you want to know things like how many bullets you've fired, or how many goblins you've killed right? Not to worry, I have you covered!

Example

Lets say you have a Gun script that handles the firing of your guns. You want to track how many bullets you've fired, then after a certain amount you get an achievement for it. Easy, no problem. It may look something like:

public class GunFire : MonoBehaviour
{
    public GameObject bulletPrefab; // Assign your bullet prefab in the Inspector
    public Transform bulletSpawn;   // Assign the spawn point for the bullet (usually at the gun's barrel end)
    public float bulletSpeed = 1000f; // Speed at which the bullet will be fired

    void Update()
    {
        if (Input.GetButtonDown("Fire1")) // Default fire button (usually left mouse button)
        {
            Fire();
        }
    }

    void Fire()
    {
        // Create a new bullet instance at the bulletSpawn position and rotation
        GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);

        // Add a forward force to the bullet to propel it forward
        bullet.GetComponent<Rigidbody>().AddForce(bulletSpawn.forward * bulletSpeed);

        // Optional: Destroy the bullet after a certain time to avoid memory leaks
        Destroy(bullet, 2.0f); // Destroys the bullet after 2 seconds
    }
}

NOTE - this is not a great example of a gun firing script but it will do for an example!!

Notice that in your Fire() function, that is where you are handing the firing of the gun.

In the Tracker Pro Manager I've simply set up a new achievement called "Spray and Pray":

I've also set up a stat in the Stat Manager section called Bullets Fired:

All we need to do now is add some functions to your Fire() functionality.

Simply add these two lines:

AchievementMaster.AddValueToAchievement("Spray and Pray", 1);
StatMaster.IncrementStat("Bullets Fired", 1);

This way, when a bullet is fired, it then adds to the achievement and the stat! When the value for the achievement reaches the needed value, it automatically grants the achievement. You can also grant the achievement manually yourself using:

AchievementMaster.GiveAchievement("Spray and Pray");

Last updated