按住手机上的按钮

Holding button on moblie

我想为 android 移动设备 phone 制作一些控件。想要制作一个按钮,如果玩家触摸并按住他开始移动到 right/left。如果他停止握住它,向侧面的运动就会停止。

我有这个脚本可以通过按住某个键来移动:

void FixedUpdate()
{
    // Add a forward force
    rb.velocity = new Vector4(0, 0, ForwardForce * Time.deltaTime, rb.velocity.y);

    if (Input.GetKey("d"))  // If the player is pressing the "d" key
    {
        // Add a force to the right
        rb.velocity = new Vector4(SidewaysForce * Time.deltaTime, 0, ForwardForce * Time.deltaTime, rb.velocity.x);
    }

    if (Input.GetKey("a"))  // If the player is pressing the "a" key
    {
        // Add a force to the left
        rb.velocity = new Vector4(-SidewaysForce * Time.deltaTime, 0, ForwardForce * Time.deltaTime, rb.velocity.y);

    }

创建 2 个 UI 个按钮。

左边 1 个按钮,右边 1 个按钮。

为每个按钮添加事件触发组件。

为动作创建一个class,例如:

public class PlayerMovement : MonoBehaviour {

 public float speed = 3;

 public void MoveLeft(){
     transform.Translate(-Vector3.right * speed * Time.deltaTime);
 }

 public void MoveRight(){
     transform.Translate(Vector3.right * speed * Time.deltaTime);
 }

}

在 UpdateSelected 上调用它并使用 Pointer Up / Pointer Down 事件。