Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
756 B
2
Indexable
public TMP_InputField inputField;
public TMP_Text speedText;

float startTime;
int charactersTyped;

void Start()
{
    // Attach the OnValueChanged listener to the input field
    inputField.onValueChanged.AddListener(OnValueChanged);
}

void OnValueChanged(string text)
{
    // Start measuring typing speed when the first character is entered
    if (charactersTyped == 0)
    {
        startTime = Time.time;
        InvokeRepeating("UpdateSpeed", 0f, 1f); // Update speed every second
    }

    charactersTyped = text.Length;
}

void UpdateSpeed()
{
    float timeElapsed = Time.time - startTime;
    int cpm = Mathf.RoundToInt(charactersTyped / timeElapsed * 60f);
    speedText.text = "Typing Speed: " + cpm + " CPM";
}