Unity中反应时间计算不正确

问题描述 投票:0回答:1

我正在编写一个应用程序,其中有三个对象是红色的。当它们变成绿色时,玩家必须以最快的速度点击屏幕,并计算反应时间。但是我感觉反应时间大多数时候都是错误的。

我的代码如下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


public class light : MonoBehaviour
{


    public Button theButton; 
    public ColorBlock theColor;

    public float time_left;
    public GameObject Replay_Game_Button;
    public GameObject TimeText;
    public GameObject ErrorText;
    public bool open=false;
    public float startTime;
    public float endTime;
    public bool set=false;
    public int counter;



    // Start is called before the first frame update

    void Start()
    {
        time_left=Random.Range(0.0f, 3.0f);
        theButton = GetComponent<Button>();
        theColor=GetComponent<Button>().colors;
        theColor.normalColor = Color.red;
        theButton.colors=theColor;
    }

    // Update is called once per frame
    void Update()
    {
        time_left -= Time.deltaTime;
        if ( time_left < 0 && set==false)
        {
            if (set==false) {
                startTime=Time.time;
                set=true;
            }
            open=true;
            theColor.normalColor=Color.green;
            theButton.colors = theColor;
        }
        if (/*Input.touchCount>0*/Input.GetMouseButtonDown(0)  && open==false){
            displayError();
            return;
        }
        else if (/*Input.touchCount>0*/Input.GetMouseButtonDown(0) && open==true){
            displayTime();
            return;
        }
    }

    public void displayTime(){
        endTime=Time.time-startTime;
        counter= PlayerPrefs.GetInt("counter");
        if (counter==5){
            PlayerPrefs.SetFloat("Time",endTime);
        }
        else if (counter==4){
            PlayerPrefs.SetFloat("Time2",endTime);
        }
        else if (counter==3){
            PlayerPrefs.SetFloat("Time3",endTime);
        }
        else if (counter==2){
            PlayerPrefs.SetFloat("Time4",endTime);
        }
        else if (counter==1){
            PlayerPrefs.SetFloat("Time5",endTime);
        }
        SceneManager.LoadScene("Replay");
    }

    public void displayError(){
        SceneManager.LoadScene("Error Scene");
    }

}

我可能做错了什么?

c# unity3d
1个回答
0
投票

事情尝试buff上的分钟,这个了

   time_left=Random.Range(0.0f, 3.0f);

它可以返回0秒

看起来

if ( time_left < 0 && set==false)
        {
            if (set==false) {
                startTime=Time.time;
                set=true;
            }
            open=true;
            theColor.normalColor=Color.green;
            theButton.colors = theColor;

应该像这样放在启动函数的底部,并从更新循环中移除。

  //...
  startTime=Time.time;
  set=true;
  open=true;
  theColor.normalColor=Color.green;
  theButton.colors = theColor;
© www.soinside.com 2019 - 2024. All rights reserved.