【问题标题】:How To Add Mobile Touch Controllers In Unity如何在 Unity 中添加移动触摸控制器
【发布时间】:2022-01-10 14:29:09
【问题描述】:

我正在开发一款游戏,到目前为止,我允许用户通过使用计算机键盘上的上下箭头键上下移动来玩游戏。

但现在我想让用户能够在手机上通过向上或向下触摸,或者向上或向下滑动来在游戏中移动。

我正在为此使用 c#。

这是分配给播放器对象的当前代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    public float playerSpeed;
    private Rigidbody2D rb;
    private Vector2 playerDirection;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float directionY = Input.GetAxisRaw("Vertical");
        playerDirection = new Vector2(0, directionY).normalized;
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2(0, playerDirection.y * playerSpeed);
    }
}

我需要做什么才能实现这一目标?

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    你应该尝试制作透明的按钮

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    • 如果你不介意,你能告诉我怎么做吗?
    【解决方案2】:

    你在这里有很多可能性! 但是可能有比其他更好的解决方案;)

    您可以制作一个表示向上和向下箭头的 UI,当您按下时,它会以您想要的任何行为做出响应

    您可以挖掘一点 Input 文档,尤其是与touches 有什么关系。

    您可以从那里获得屏幕空间中的触摸位置,并用它做任何您想做的事情!

    选择取决于您尝试制作的游戏或应用程序的类型。希望对您有所帮助;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      相关资源
      最近更新 更多