如何编码播放器使用 ui 按钮在移动设备触摸屏输入上移动
how to code player to move on mobile device touch screen input using ui button
我想知道如何在 unity 2D 中单击 UI 按钮时让我的播放器移动。我对如何对 UI 按钮进行编码以接收输入并移动玩家直到 UI 按钮未被按下感到更加困惑,但如果有人也能帮助移动,我仍然会很高兴,但是按钮更重要。我正在用 c#
编码
我希望它像这样工作,但显然这只是伪代码:
public void whenClicked();
{
如果(左键被点击)
向左移动玩家
如果(单击右键)
向右移动玩家
if(upButton 被点击)
移动玩家跳跃
}
你可以在你的对象中添加一个 BoxCollider2D 组件并编写函数
OnMouseDown(){} // calls one frame when there was touch of boxcollider
OnMouseDrag(){} // calls every frame when there is touch of boxcollider
OnMouseUP(){} // calls one framу when there touch of boxcollider was stopped
此功能不仅适用于 PC,而且适用于 Android。
移动角色有两种主要方式:通过变换组件移动(因为你有 2D 游戏,你很可能在角色上有 RectTransform)和通过 Rigidbody 组件移动。
转换:
按钮上的脚本应如下所示:
public GameObject character; // The link to the character you want to move
private bool doMove; // whether the character must move or not
public float speed;
private void OnMouseDown () {
doMove = true;
}
private void Update () {
if (doMove) {
character.transform.Translate(Vector3.right * Time.deltaTime * speed);
}
}
它正在向右移动。您还可以使用 Vector3.up、Vector3.left、Vector3.down、Vector3.forward、Vector3.back
尝试变速,你会得到你想要的。
但是我有一个问题。我刚刚看到您在问题中写道您使用 UI 按钮。您是指带有 BoxCollider 的图像还是带有 Button 组件的字面上的 GameObject。如果是第二个,我建议您从 button_objects 中删除组件 Button,因为只需一个带有 OnMouseDown 函数的脚本就足够了。必须将此脚本添加到所有按钮(向上、向右...)。您还应该将下一个字符串添加到您的代码中:
public Vector3 direction;
// and also edit Update function this way:
if (doMove) {
character.transform.Translate(direction * Time.deltaTime * speed);
}
然后在 Unity 中在此脚本中设置下一个参数:
For button up direction = x=0, y=1, z=0
For button down direction = x=0, y=-1, z=0
For button right direction = x=1, y=1, z=0
For button left direction = x=-1, y=1, z=0
因此,每个按钮都会使角色朝自己的方向移动;
刚体呢。如果你使用它,你可以做同样的事情,但是替换函数 transform.Translate by GetComponent().AddForce(Vector3.right*speed, ForceMode2D.Impulse);
但我个人认为通过 Transform 移动更容易
我想知道如何在 unity 2D 中单击 UI 按钮时让我的播放器移动。我对如何对 UI 按钮进行编码以接收输入并移动玩家直到 UI 按钮未被按下感到更加困惑,但如果有人也能帮助移动,我仍然会很高兴,但是按钮更重要。我正在用 c#
编码我希望它像这样工作,但显然这只是伪代码:
public void whenClicked();
{
如果(左键被点击)
向左移动玩家
如果(单击右键)
向右移动玩家
if(upButton 被点击)
移动玩家跳跃
}
你可以在你的对象中添加一个 BoxCollider2D 组件并编写函数
OnMouseDown(){} // calls one frame when there was touch of boxcollider
OnMouseDrag(){} // calls every frame when there is touch of boxcollider
OnMouseUP(){} // calls one framу when there touch of boxcollider was stopped
此功能不仅适用于 PC,而且适用于 Android。
移动角色有两种主要方式:通过变换组件移动(因为你有 2D 游戏,你很可能在角色上有 RectTransform)和通过 Rigidbody 组件移动。
转换: 按钮上的脚本应如下所示:
public GameObject character; // The link to the character you want to move
private bool doMove; // whether the character must move or not
public float speed;
private void OnMouseDown () {
doMove = true;
}
private void Update () {
if (doMove) {
character.transform.Translate(Vector3.right * Time.deltaTime * speed);
}
}
它正在向右移动。您还可以使用 Vector3.up、Vector3.left、Vector3.down、Vector3.forward、Vector3.back 尝试变速,你会得到你想要的。
但是我有一个问题。我刚刚看到您在问题中写道您使用 UI 按钮。您是指带有 BoxCollider 的图像还是带有 Button 组件的字面上的 GameObject。如果是第二个,我建议您从 button_objects 中删除组件 Button,因为只需一个带有 OnMouseDown 函数的脚本就足够了。必须将此脚本添加到所有按钮(向上、向右...)。您还应该将下一个字符串添加到您的代码中:
public Vector3 direction;
// and also edit Update function this way:
if (doMove) {
character.transform.Translate(direction * Time.deltaTime * speed);
}
然后在 Unity 中在此脚本中设置下一个参数:
For button up direction = x=0, y=1, z=0
For button down direction = x=0, y=-1, z=0
For button right direction = x=1, y=1, z=0
For button left direction = x=-1, y=1, z=0
因此,每个按钮都会使角色朝自己的方向移动;
刚体呢。如果你使用它,你可以做同样的事情,但是替换函数 transform.Translate by GetComponent().AddForce(Vector3.right*speed, ForceMode2D.Impulse); 但我个人认为通过 Transform 移动更容易