Search code examples
c#unityscriptunity-game-engine

Unity3d Time.deltaTime and framerate issue


I am creating a game in which player can assign certain task to npc, and with task complexity it will take that much amount of time (5 minutes for complex task and 1 minute for simple task). To achieve this I created a timer with Time.deltatime.

Starttime = Starttime + Time.Deltatime;

but as per unity docs deltatime is difference between two frames so now on pc game is running on 60 frames so it will takes 60 frames to complete one minute but on mobile it takes longer time than required , So please can anyone tell me how to fix this issue


Solution

  • do not use Time.deltaTime or any similar function ever, for any reason - ever.

    For beginners with Unity, for all timers and all issues relating to time, just use Invoke

    Your code will look like this...

    void Start()
     {
     Debug.Log("user begins task .. must complete in 60 seconds");
     Invoke("TimeIsUpForUser", 60f);
     } 
    
    private void TimeIsUpForUser()
     {
     Debug.Log("time's up! user must be finished by now");
     }
    

    It's that simple.

    By way of example, your whole code may look something like this..

    void Start()
     {
     ShowMiniPuzzleOnScreen();
     Invoke("TimeIsUpForUser", 60f);
     } 
    
    private void UserHasPlacedFinalPieceOfPuzzle()
     {
     MessageScreen("Congratulations! You get 100 coins!");
     balance += 100;
     CancelInvoke("TimeIsUpForUser");
     }
    
    private void TimeIsUpForUser()
     {
     HideMiniPuzzle();
     PlaySadMusic();
     MessageScreen("You suck! You are too slow. You lose 50 points.");
     balance -= 50;
     }