Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 跳跃的统一触控检测_C#_Unity3d - Fatal编程技术网

C# 跳跃的统一触控检测

C# 跳跃的统一触控检测,c#,unity3d,C#,Unity3d,嗨,伙计们!我试图删除跳转按钮,并用触摸控件替换它,但是当我测试它时,脚本会将任何触摸都算作跳转。我的目标是让它只在玩家触摸屏幕上除操纵杆以外的任何部分时检测到它。有人知道我将如何实现这个目标吗? 这是我的密码: if(Input.GetButtonDown("Fire1")) { Rb.AddForce(Vector3.up * Jump); } 为了忽略操纵杆的区域,您必须过滤触摸,而不是通过所有触摸,并且只保留“有效”的触摸,以

嗨,伙计们!我试图删除跳转按钮,并用触摸控件替换它,但是当我测试它时,脚本会将任何触摸都算作跳转。我的目标是让它只在玩家触摸屏幕上除操纵杆以外的任何部分时检测到它。有人知道我将如何实现这个目标吗? 这是我的密码:

    if(Input.GetButtonDown("Fire1"))
    {
        Rb.AddForce(Vector3.up * Jump);
    }

为了忽略操纵杆的区域,您必须过滤触摸,而不是通过所有触摸,并且只保留“有效”的触摸,以便那些是有效的

  • 目前正处于这一阶段

  • 在操纵杆上方。假设
    画布中有一个UI元素
    ,如果触摸在任何UI元素上,您可以使用传入touche,它返回
    true

为了使过滤更容易,您可以使用

在这里使用
Any
基本上只是一种写作的捷径

foreach(var touch in Input.touches)
{
    if(touch.phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
    {
        Rb.AddForce(Vector3.up * Jump);
        break;
    }
}
您可以根据需要扩展和优化条件。例如,您也可以简单地通过使用
touch.position
忽略屏幕特定区域中的所有触摸,例如

if(Input.touches.Any(touch => touch.phase == TouchPhase.Began && touch.position.x > MIN_X && touch.position.y > MIN_Y && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
     Rb.AddForce(Vector3.up * Jump);
}

MIN_X
MIN_Y
中,您定义了一个覆盖操纵杆的矩形

每次有人移动操纵杆时,都会发生相同的情况
if(Input.touches.Any(touch => touch.phase == TouchPhase.Began && touch.position.x > MIN_X && touch.position.y > MIN_Y && !EventSystem.current.IsPointerOverGameObject(touch.fingerId))
{
     Rb.AddForce(Vector3.up * Jump);
}