1

我目前正在创建一个隐藏对象游戏,我一直被困在如何为我的游戏添加计时器和倒计时。我目前有一个在点击所有对象后生成的分数,但是如果用户点击每个对象后分数逐渐上升,我会很高兴。到目前为止,这是我的代码。

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;

        }
    }

}

4

1 回答 1

0

您可以将协程用于计时器(协程在一定时间后执行代码)。如果您希望玩家点击一个对象后您的分数逐渐增加(我认为这就是您要问的),那么这样的事情应该可以工作:

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++;
    }

}

我不确定 score 变量在哪里,所以我创建了一个新变量;您可以将其设置为您自己的,无论它在哪里。如果您需要更多帮助,请随时提出。

于 2017-09-05T16:14:57.140 回答