【问题标题】:Getting error: Cannot implicitly convert type `UnityEngine.Touch' to `bool'出现错误:无法将类型“UnityEngine.Touch”隐式转换为“bool”
【发布时间】:2015-08-17 22:05:08
【问题描述】:

每当我运行此代码时,我都会收到此错误:

错误 CS0029:无法将 UnityEngine.Touch' 类型隐式转换为 布尔'

这里有什么问题?当手指触摸屏幕时,我正在尝试将gameObject 向上移动。

private Rigidbody rb;

void Start() {

rb = GetComponent<Rigidbody>();

 }

 void Update () {

     if(Input.GetTouch()) {


         transform.position += Vector3.up;
     }

  }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    Input.GetTouch 返回什么?

    返回表示特定触摸状态的对象。

    你在这里做什么?

    if(Input.GetTouch())
    

    您可以使用对象,而不是使用布尔表达式或布尔变量!这不能编译。在那里,在 if 语句中,您必须使用条件或布尔变量。

    我们如何识别用户是否进行了触摸?

    if(Input.touchCount > 0)
    {
    }
    

    【讨论】:

      【解决方案2】:

      因为这种情况:

      if(Input.GetTouch())
      

      不返回 boolean 类型。它返回touch。要检查你有没有触摸,你应该像这样使用它:

      void Update () 
      {
         if(Input.touchCount > 0) {
           transform.position += Vector3.up;
         }
      }
      

      如果您想了解有关触摸检测的更多信息 - 请查看this unity 手册。

      【讨论】:

        【解决方案3】:

        根据Unity API,public static Touch GetTouch(int index); 返回Touch,而不是bool

        【讨论】:

          猜你喜欢
          • 2017-10-05
          • 1970-01-01
          • 1970-01-01
          • 2015-03-28
          • 1970-01-01
          • 2014-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多