c# - 按住手机上的按钮

标签 c# android unity-game-engine

我想为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/

相关文章:

c# - mongodb中的数据引用

android,滚动Horizo​​ntalScrollView到ListView结束

vector - Unity3D从V1到移动V2的线性插值

c# - 将列表转换为小字符串

3d - 我正在尝试以编程方式为角色的头发设置动画。最好的方法是什么?

c# - 在构造函数中设置 DataContext=this 和在 WPF 中绑定(bind)到 {RelativeSource Self} 之间的区别?

c# - EF 迁移不会自动更新数据库

android - 从 WebView 检测并获取播放视频 url

android - Android 音乐播放器的翻转暂停功能

c# - JSON.NET 可以使用 "sidecar"方法序列化对象吗?