我想为android手机做一些控件。想要制作一个按钮,如果玩家触摸并按住他就开始向右/向左移动。如果他停止握住它,向侧面的运动就会停止。
我有一个通过按住某个键来移动的脚本:
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 个按钮。
为每个按钮添加事件触发组件。
为 Action 创建一个类,例如:
公共(public)类 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 事件。
关于c# - 按住手机上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45537008/