簡體   English   中英

移動設備上的觸摸輸入

[英]Touch input on mobile device

我有此腳本用於在移動設備上輸入觸摸。 但是它只能激活一次,我需要運行它直到我將手指從屏幕上移開

public float speed = 3;


public Rigidbody rb;
public void MoveLeft()
{
    transform.Translate(-Vector3.right * speed * Time.deltaTime);
}
public void StopMoving()
{
    rb.velocity = new Vector2(0, 0);
}

public void MoveRight()
{
    rb.velocity = new Vector2(-speed, 0);
}
private void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        float middle = Screen.width / 2;
        if (touch.position.x < middle && touch.phase == TouchPhase.Began)
        {
            MoveLeft();
        }
        else if (touch.position.x > middle && touch.phase == TouchPhase.Began )
        {
            MoveRight();
        }
    }
    else
    {
        StopMoving();
    }
}

}

您只需要刪除兩個&& touch.phase == TouchPhase.Began部分。 只要屏幕上有一根手指,這將使總體if語句評估為true。

private void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.GetTouch(0);
        float middle = Screen.width / 2;
        if (touch.position.x < middle)
        {
            MoveLeft();
        }
        else if (touch.position.x > middle)
        {
            MoveRight();
        }
    }
    else
    {
        StopMoving();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM