单击某个对象有时只能工作(Unity 3D)

时间:2017-01-18 12:04:46

标签: c# unity3d

哟!

我有一个非常基本的激活码,玩家点击一个对象,对象就可以完成它。问题在于,有时它适用于第一次点击,以及它在第二次,第三次或第四次点击时起作用。

我怀疑这与更新频率有关,但我不知道如何正常工作。

这是我的代码:

void OnTriggerStay(Collider col)
    {
        if (col.gameObject.tag == "Player")
        {
            if (Input.GetMouseButtonUp(0))
            {
                if (Activated.GetComponent<raycastTest> ().SystemEnabled == true) 
                {
                    Activated.GetComponent<raycastTest> ().SystemEnabled = false;
                } 
                else if (Activated.GetComponent<raycastTest> ().SystemEnabled == false) 
                {
                    Activated.GetComponent<raycastTest> ().SystemEnabled = true;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

OnTriggerStay与FixedUpdate对齐。如果它被设置为60(我认为它是默认值)但你的计算机运行100fps,那么你有40帧在物理过程中被丢弃。

但输入与操作系统一起运行,因此每秒将对输入进行100次检查。

因此,通过逻辑,你有40%的机会错过命中。

如果您在这40个丢弃的帧之一中按下,因为您检查按钮按下,它仅对该帧有效。下一帧它消失了。

解决方案是在您进入/退出时标记,并在按下时检查该标志:

Traceback (most recent call last):
  File "C:\Python27\1.wedding.py", line 28, in <module>
    driver = webdriver.Firefox()
  File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__
    self.service.start()
  File "C:\Python27\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
WebDriverException: Message: 'geckodriver' executable needs to be in PATH.