1- // Copyright 2022 Google LLC
1+ // Copyright 2022 Google LLC
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -38,15 +38,13 @@ public class CarMove : MonoBehaviour
3838 private const float NoVelocity = 0.01f ;
3939 private const float TurboVelocity = 1.5f ;
4040
41- private int _clickCounter ;
42- private float _firstClickTime ;
43- private const float ClickDelay = 0.15f ;
4441
4542 private const int NORMAL_SPEED = 1 ;
4643 private const int TURBO_SPEED = 2 ;
4744
4845 // Achievement for going this far
49- private const float ACHIEVEMENT_DISTANCE = 100.0f ;
46+ private const float ACHIEVEMENT_DRIVE_DISTANCE = 100.0f ;
47+ private bool _driveAchievementUnlocked = false ;
5048
5149 private InputAction _garageAction ;
5250 private InputAction _pgsAction ;
@@ -58,10 +56,8 @@ public class CarMove : MonoBehaviour
5856
5957 private void Start ( )
6058 {
61- _clickCounter = 0 ;
62- _firstClickTime = 0f ;
63- _gameManger = FindObjectOfType < GameManager > ( ) ;
64- _pgsController = FindObjectOfType < PGSController > ( ) ;
59+ _gameManger = FindFirstObjectByType < GameManager > ( ) ;
60+ _pgsController = FindFirstObjectByType < PGSController > ( ) ;
6561 // Get the carObj corresponding to the car game object the script attached to.
6662 _carObj = CarList . GetCarByName ( carName ) ;
6763 _rigidbody2D = GetComponent < Rigidbody2D > ( ) ;
@@ -93,23 +89,31 @@ private void FixedUpdate()
9389 newDistance += GameDataController . GetGameData ( ) . distanceTraveled ;
9490 GameDataController . GetGameData ( ) . distanceTraveled = newDistance ;
9591#if PLAY_GAMES_SERVICES
96- CheckDistanceAchievement ( newDistance ) ;
92+ if ( ! _driveAchievementUnlocked )
93+ {
94+ CheckDistanceAchievement ( newDistance ) ;
95+ }
9796#endif
9897 _odometerText . text = newDistance . ToString ( "N1" , CultureInfo . CurrentCulture ) ;
9998 }
10099
101100#if PLAY_GAMES_SERVICES
102101 private void CheckDistanceAchievement ( float newDistance )
103102 {
104- if ( _pgsController . CurrentSignInStatus == PGSController . PgsSigninStatus . PgsSigninLoggedIn )
103+ if ( _pgsController != null &&
104+ newDistance >= ACHIEVEMENT_DRIVE_DISTANCE &&
105+ _pgsController . CurrentSignInStatus == PGSController . PgsSigninStatus . PgsSigninLoggedIn )
105106 {
106107 var achievementManager = _pgsController . AchievementManager ;
107- if ( ! achievementManager . GetAchievementUnlocked (
108- PGSAchievementManager . TrivialKartAchievements . Tk_Achievement_Distance ) &&
109- newDistance >= ACHIEVEMENT_DISTANCE )
108+ if ( achievementManager != null )
110109 {
111- achievementManager . UnlockAchievement (
112- PGSAchievementManager . TrivialKartAchievements . Tk_Achievement_Distance ) ;
110+ if ( ! achievementManager . GetAchievementUnlocked (
111+ PGSAchievementManager . TrivialKartAchievements . Tk_Achievement_Drive ) )
112+ {
113+ achievementManager . UnlockAchievement (
114+ PGSAchievementManager . TrivialKartAchievements . Tk_Achievement_Drive ) ;
115+ }
116+ _driveAchievementUnlocked = true ;
113117 }
114118 }
115119 }
@@ -119,7 +123,10 @@ private void Update()
119123 {
120124#if PLAY_GAMES_SERVICES
121125 // Checks to see if we are due to update the distance traveled leaderboard
122- _pgsController . UpdateLeaderboard ( ) ;
126+ if ( _pgsController != null )
127+ {
128+ _pgsController . UpdateLeaderboard ( ) ;
129+ }
123130#endif
124131 // Use keys to control menu/gameplay
125132 if ( _garageAction . WasPressedThisFrame ( ) )
@@ -174,7 +181,6 @@ private void ProcessClickAction(int clicks)
174181 break ;
175182 }
176183 }
177- _clickCounter = 0 ;
178184 }
179185
180186 private void Drive ( )
0 commit comments