Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Fading sprite and Time.deltaTime

Discussion in '2D' started by therocketman2018, May 15, 2020.

  1. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    Hello. I am making a sprite fade with given time, and it works correctly, but should i use Time.deltaTime in this example so the fading effect could flow with the same speed in every computer? How to use it in this example?

    Code (CSharp):
    1. void OnTriggerStay(Collider other)
    2.     {
    3.         if (!isFadedAway)
    4.         {
    5.             if (other.gameObject.name == "Player")
    6.             {
    7.                 if (!startTimeGiven)
    8.                 {
    9.                     startTime = Time.time;  //SHOULD I USE DELTATIME IN HERE?
    10.                     startTimeGiven = true;
    11.                 }
    12.  
    13.                 t = (Time.time - startTime) / duration; // OR HERE
    14.  
    15.                 sprite.color = new Color(1f, 1f, 1f, Mathf.SmoothStep(maximum, minimum, t));
    16.  
    17.              
    18.             }
    19.         }
    20.        
    21.        
    22.     }
     
  2. Nyfirex

    Nyfirex

    Joined:
    Jun 26, 2019
    Posts:
    179
    Time.time is the actual game time. For example if the game is running and you started the game 20s ago, Time.time will return 20s. Time.deltaTime is the time that there's between two calls of Update(). Update() calls depend on the performance of your computer. For example if there are 200 calls of Update() in one second, Time.deltaTime would return about 1/200. (Careful that this value isn't always the same, timing calculations aren't always the same). On a slower computer you wouldn't have 200 calls in 1 second, maybe 150 so Time.deltaTime would return about 1/150. Time.deltaTime doesn't have always the same value, there can be like 0,01s between 2 frames but the next frame can be 0,5s, it depends what's happening in your game. In your code it seems that you want to get the time so Time.time is fine to use. You can also get the same result using Time.deltaTime
     
    therocketman2018 likes this.
  3. therocketman2018

    therocketman2018

    Joined:
    Apr 25, 2019
    Posts:
    46
    Thank you very much! your explanation was very clear and helped me! :)