我正在制作一款游戏,用于计算用户在规定的时间限制内所执行的按压速度。我能够创建一些费率代码。
rate++;
if (rate == reset_the_timer_lim) {
timer_limit--;
rate = 0;
//print(counter_of_presses);
//okay print the amt of button presses button/rate
//so we get a number of keypresses in alotted time unit
//int calc the rate
for (int x = 0; x < 2; x++)
{
rate_nums[x] = counter_of_presses;
print(rate_nums.ToString());
}
}
这是按钮代码
if (Input.GetKeyDown (KeyCode.Space))
{
counter_of_presses++;
counter_press.text = counter_of_presses.ToString();
}
我有一个单独的计数器(费率),可以计算另一个数字。
例如,
如果我设置reset_the_timer_lim = 25,则速率计数最多为25并重置为零。然后取出按下的数量(Input.getKey Code(2nd block))并尝试将它们存储到数组中。它减去最终的费率减去初始费率。
但是,当我运行此代码时,数组中的两个元素都是相同的。它不会等待重置速率,我不知道从哪里开始。
编辑5/12/18
public class press_game : MonoBehaviour {
public int timer_limit = 120;
private int rate;
public GameObject player_heart;
//public GameObject camera_back_up;
public int reset_the_timer_lim;
private int counter_of_presses;
public int amt_of_presses_before_end;
public Text show_time;
public Text counter_press;
public Text get_it_2_here;
public int[] rate_nums;
private float sum, sum2;
public float amt_to_widen_heart, amt_to_widen_cam;
public GameObject a1a, a2a, a3a, a4a, a5a, a6a, a7a, a8a, a9a, a10a, a11a, a12a, a13a, a14a, a15a, a16a;
// Use this for initialization
void Start () {
rate_nums = new int[2];
get_it_2_here.text = amt_of_presses_before_end.ToString ();
//a1a.SetActive(true);
}
// Update is called once per frame
void Update () {
sum = sum + amt_to_widen_heart;
sum2 = sum2 + amt_to_widen_cam;
rate++;
if (rate == reset_the_timer_lim) {
timer_limit--;
rate = 0;
//print(counter_of_presses);
//okay print the amt of button presses button/rate
//so we get a number of keypresses in alotted time unit
//int calc the rate
for (int x = 0; x < 2; x++)
{
rate_nums[x] = counter_of_presses;
print(rate_nums.ToString());
}
}
//print (timer_limit);
show_time.text = timer_limit.ToString ();
//get_it_2_here.text =
if (timer_limit < 0) {
show_time.text = "Out of time!!";
//counter_of_presses = 0;
counter_press.text = "OUT OF TIME!";
if(counter_of_presses < amt_of_presses_before_end){
print("You have lost the game!");
//counter_of_presses = 0;
}
else{
print ("You have won this game!");
//counter_of_presses = 0;
}
}
if (Input.GetKeyDown (KeyCode.Space)) {
counter_of_presses++;
counter_press.text = counter_of_presses.ToString();
player_heart.transform.localScale += new Vector3(transform.localScale.x + sum, transform.localScale.y + sum, transform.localScale.z);
//camera_back_up.transform.localScale += new Vector3(transform.localScale.x + sum2, transform.localScale.y + sum2, transform.localScale.z);
}
}
}
答案 0 :(得分:0)
我已经为你编写了这一小段代码,它是非常基本的,只有三秒的计数率,之后它再次开始couting但是它会给你一些想法
using UnityEngine;
public class RateCounter : MonoBehaviour
{
[SerializeField] float timeooutValue = 3;
[SerializeField] float startTime;
[SerializeField] int counter;
[SerializeField] float currentRate;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (Time.time - startTime > timeooutValue)
{
startTime = Time.time;
counter = 0;
currentRate = 0;
}
else
{
counter++;
currentRate=counter/(Time.time-startTime);
}
}
}
}