[英]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.