简体   繁体   English

如何用计时器逐渐增加我的分数?

[英]How to increase my score gradually with a timer?

I am currently creating a hidden object game and I have been stuck on how to add a timer and countdown to my game. 我目前正在创建一个隐藏的对象游戏,我一直坚持如何添加计时器和倒计时到我的游戏。 I have currently a score which generates after all of the objects have been clicked however I would love if the score went up gradually once the user clicked on each one. 我目前有一个分数,在所有对象被点击后生成,但是如果用户点击每个对象后分数逐渐上升,我会很高兴。 here is my code below so far. 到目前为止,这是我的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class trackingclicks : MonoBehaviour {

    //static variable added to count users clicks
    public static int totalclicks=0;
    //"mouseclick" keycode variable to look for mouse click
    public KeyCode mouseclick;

    public Transform scoreObj;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    //checks the change in time, aka how much time has passed- bonus time starts at 90
        clickcontrol.timeBonus -= Time.deltaTime;

        if (clickcontrol.remainItems == 0) 
        {
            clickcontrol.totalScore += (70 + (Mathf.RoundToInt(clickcontrol.timeBonus)));
            scoreObj.GetComponent<TextMesh>().text = "Score : " + clickcontrol.totalScore;
            clickcontrol.remainItems = -1;

        }
    //Check for mouse click
        if (Input.GetKeyDown (mouseclick)) 
        {
            totalclicks += 1;

        }

        if (totalclicks >= 5) 
        {
            Debug.Log ("FAIL!!!");
            totalclicks = 0;

        }
    }

} }

You can use coroutines for timers (coroutines execute code after a certain amount of time). 您可以将协同程序用于协程(协同程序在一定时间后执行代码)。 If you want your score to increment gradually once the player clicks on an object (I think that's what you're asking), then something like this should work: 如果你希望你的分数在玩家点击一个对象后逐渐增加(我认为这就是你所要求的),那么这样的事情应该有效:

public int score;

IEnumerator IncrementScore(int amount, int interval) { //amount is the amount of score to add over the time, interval is the time IN SECONDS between adds
    for (int i = 0; i < amount; i++) {
    yield return new WaitForSeconds(interval);
    score++;
    }

}

I wasn't sure where the score variable was so I made a new one; 我不确定得分变量在哪里,所以我做了一个新的; you can set it to your own, wherever it is. 您可以将它设置为您自己的,无论它在哪里。 If you need more help, feel free to ask. 如果您需要更多帮助,请随时提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM