在 Unity3d 中禁用触摸保持

Disable Touch Hold in Unity3d

我正在使用脚本,所以当用户触摸屏幕时,播放器会在空中跳跃 但问题是当用户按住触摸时玩家卡在空中并且永远不会掉下来 当用户只触摸一次播放器跳一次时,我如何禁用触摸保持?

Rigidbody2D Rigid;
public float UpForce;
public float RightSpeed;

void Start()
{
    Rigid = GetComponent<Rigidbody2D> ();
}

void Update()
{
    transform.Translate (Vector2.right * Time.deltaTime * RightSpeed);
}

void FixedUpdate()
{
    foreach (Touch touch in Input.touches)
    {
        Rigid.AddForce (Vector2.up * UpForce);
    }
}

}

你只需要添加触摸阶段的检查。在这种情况下,TouchPhase.Began is appropriate since it will jump when user touches the screen. If you want it to jump when user releases the touch then use TouchPhase.Ended.

foreach (Touch touch in Input.touches)
{
    if (touch.phase == TouchPhase.Began)
    {
        Rigid.AddForce(Vector2.up * UpForce);
    }
}

:

您应该始终在 Update() 函数中而不是 FixedUpdate() 函数中检查输入事件。我建议您将该代码放在 Update() 函数中。您的物理代码应该放在 FixedUpdate() 函数中。