简体   繁体   English

我的 If-Statement 运行时没有得到所需的输入?

[英]My If-Statement runs without getting the needed imput?

Touch touchForThrow;

void Update()
    {
        if (Input.touchCount > 0)
            touchForThrow = Input.GetTouch(1);

        if (touchForThrow.phase == TouchPhase.Began)
        {
            Debug.Log(Input.touchCount);
        }
    }

The second If-statement is running without a touch imput.第二个 If 语句在没有触摸输入的情况下运行。 touchForThrow.phase == TouchPhase.Began should not be possible, even the Debug.Log says that 0 touches are identified, but it still runs the statement. touchForThrow.phase == TouchPhase.Began应该是不可能的,即使 Debug.Log 说识别了 0 次触摸,但它仍然运行该语句。

You are waiting for one touch but are trying to get the second one!您正在等待一次触摸,但正在尝试获得第二次触摸! Remember that indices in c# are 0-based!请记住,c# 中的索引是从 0 开始的!

Also Touch is a struct and thereby always has a default value. Touch也是一个结构体,因此总是有一个默认值。 Even if your first if is not true it already has a dummy touch value where the phase might be Began by default.即使您的第一个if不是真的,它也已经有一个虚拟触摸值,默认情况下该阶段可能是Began的。

It should probably rather be它可能应该是

if (Input.touchCount > 0)
{
    touchForThrow = Input.GetTouch(0);

    if(touchForThrow.phase == TouchPhase.Began)
    {
        Debug.Log(Input.touchCount);
    }
 }

If you want the second touch you should probably rather first check if there are at least two touches如果您想要第二次触摸,您可能应该首先检查是否至少有两次触摸

if(Input.touchCount > 1)

TouchPhase.Began is the default value of phase. TouchPhase.Began 是相位的默认值。 The code worked properly, after nesting the second If-statement in the first one.在将第二个 If 语句嵌套在第一个中后,该代码可以正常工作。

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

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