Home > front end >  Counting 100th of a second in C#
Counting 100th of a second in C#

Time:09-17

Hi im just starting out learning to code. Im creating a game in unity and my score counts 1 every second of game play. Im struggling on how i can count 0.01 every second. here is my code:

// Update is called once per frame
void Update()
{
    if(GameObject.FindGameObjectWithTag("Player") != null)
    {
        score  = 1 * Time.deltaTime;
        scoreText.text = ((int)score).ToString();
    }
}

}

CodePudding user response:

I think it's easier to do the opposite thing. You will set your score to time that had passed.

private int StartTime;

void Awake() 
{
    if(GameObject.FindGameObjectWithTag("Player") != null)
    {
        StartTime = Time.time;
    }
}

void Update()
{
    if(GameObject.FindGameObjectWithTag("Player") != null)
    {
        score = StartTime - Time.time;
        scoreText.text = ((int)score).ToString();
    }
}

CodePudding user response:

Why not simply do

void Update()
{
    if(GameObject.FindGameObjectWithTag("Player") != null)
    {
        score  = 0.01f * Time.deltaTime;
        scoreText.text = ((int)score).ToString();
    }
}

CodePudding user response:

This Script maybe helps you. check this out

using System;
using TMPro;
using UnityEngine;

public class TimeCounter : MonoBehaviour {
  #region Variables
  [Header("Add Time in Min")][Space(5)]
  [SerializeField] private float timeInMin;
  private float tickTime;
  private float timeinSec;
  public event EventHandler OnTimeOver;// On Time Over
  private TextMeshProUGUI timeCounterTextMesh;
  public static TimeCounter instance { get; private set; }
 // Const
 private const float RESET_TIME = 0f;
 private const float STD_TIMECONV = 60f;
 #endregion

 #region Build_In Methods
 // On Awake
  private void Awake(){
     instance = this;
 }
 // On Start
  private void Start(){
    SetTimer();
 }
 // On Update
  private void Update(){
     CountTime();
 }
 #endregion


 #region Custom-Methods
  private void SetTimer(){
     if (timeInMin >= 1){
         SetTimer_NotUnderOne();
     } else{
        SetTimer_UnderOne();
    }
  }
// Count Time
 private void CountTime() {
    tickTime -= Time.deltaTime;// Counting Sec
    if (tickTime <= RESET_TIME){
        tickTime = STD_TIMECONV; // Reset Sec
        if (timeInMin > RESET_TIME)
            timeInMin--;
        else TimeOver();// Game Over
    }
    //Displaying   Time
  Debug.Log("Min : "   timeInMin   " Sec : "   (int)(tickTime)); 
 }

#endregion

 #region Helper Methods
 private void SetTimer_NotUnderOne(){ 
    if (IsFloat(timeInMin)) { // Number is Float
        timeinSec = timeInMin * STD_TIMECONV;// Min to Sec
        float _timeInSec = (int)timeInMin * STD_TIMECONV;//Min to Sec 
      !FloatingPoint
        timeinSec -= _timeInSec;// Getting Remains Sec
        timeInMin = (int)timeInMin;// Ignore Floating point
        tickTime = timeinSec;
    }

    else{ // Number is Int
        timeInMin--;
        // Set Default Sec to 60
        tickTime = STD_TIMECONV;
    }
 }
 private void SetTimer_UnderOne(){
    tickTime = timeInMin * STD_TIMECONV;
    // Set Min To Zero
    timeInMin = RESET_TIME;
 }

 private bool IsFloat(float Value) => ((int)Value - Value != 0);

 // On Time Over
 private void TimeOver(){
    OnTimeOver?.Invoke(this, EventArgs.Empty);
    DestorySelf();
 }
 // Destory Self
  private void DestorySelf(){
    Destroy(gameObject.GetComponent<TimeCounter>());
  }
 #endregion
}
  • Related