简体   繁体   中英

Problem increasing an achievement in Google Play Games in Unity

I have an achievement that increases with the time survived. I calculate the survived time with Time.time. When I die, I increase the achievement with the Time.time value to increase the seconds survived but it increases much more than it should. My code:

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;

    public class Timer : MonoBehaviour
    {
        private float StartTime;
        public static float TimerControl;

        void Start()
        {
            StartTime = Time.time;
        }

        void Update()
        {
            //The player is alive
            if (PlayerController.dead == false)
            {
                TimerControl = Time.time - StartTime;
            }

            //The player is dead
            if (PlayerController.dead == true)
            {
                PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

                });
            }
        }
    }

The amount of survived time that is increased in achievement is much greater than it should.

Any suggestion? Thanks!

Give this a try, if it doesn't work tell me what the debug is giving you.

using UnityEngine;
using UnityEngine.UI;
using GooglePlayGames;
public class Timer : MonoBehaviour
{
    void Update()
    {
        //The player is dead
        if (PlayerController.dead == true)
        {
        Debug.Log("Time : " + Time.time);
        PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(Time.time), (bool success) => {
            });
        }
    }
}

edit: okay I think I know whats happening you are incrementing it on the update loop so you must be implementing it many times

bool recorded = false;
// ...
        //The player is dead
        if (PlayerController.dead == true && !recorded)
        {
            PlayGamesPlatform.Instance.IncrementAchievement(GPGSIds.achievement_survivor, (int)(TimerControl), (bool success) => {

            });
        recorded = true;
        }

Have you tried using Time.deltaTime ? Or placing that block of code in -

void FixedUpdate() { 
    //code 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM