unity3d: 4. Countdown of time

Hi! I will show you today, how to make timing in unity3d. It is really simple, it’s issue few lines. We will can calling button function only each three second. Look on the code below.

At the beginning we have defined 2 variables, variable “wait” as you probably guess, it define waiting time for us to execute the code in our case we want to be 3 seconds, variable “countdown time” we will counting down the time.

The rest of the script must be in the “Update” function, because the update function is performed every frame, if you have 30 FPS during the game, 30 frames are performed per second, and the “Time.delataTime” function in the line “timing + = Time.deltaTime;” It counts down the time execution of each frame and adds the time of all these frames to the variable “timing”, in this way the “Time.deltaTime” function can count the execution time of each frame.

Then in the if statement, two conditions must be met, the player must press the “N” button and the variable “timing” must be greater than or equal to the variable “wait”, when the conditions are performed, Debug.Log function is performed and the variable “timing” needs to be reset to zero to be able to count down again from the beginning.

Now compile and assign this script to the object you control and after three seconds, when you press the “N” button, the console displays the message “All conditions have been done correctly“. Variables “timing” and “wait” are set to “public” so during the game you will see in the interface of the unit in the “timing” field that actually the time is counted down.

This content also you can find on steemit: https://steemit.com/unity3e/@slawas/unity3d-4-countdown-of-time

And on medium: https://medium.com/@sawomirkowalski/unity3d-4-countdown-of-time-500ce96c1cdb

That’s it for today, see you !!!!!

If that post was useful for you share it with your friends :)

Post a comment

Notify about