在Unity中使用移动触摸输入的无限跑者

本文关键字:输入 无限 触摸 移动 Unity | 更新日期: 2023-09-27 18:17:19

我正在用Unity制作一款2D平台游戏,我在脚本上遇到了一些问题。

我正在使用c#,我需要一个触摸脚本,与我在屏幕的一半右侧或在屏幕的两侧跳跃进行交互。我到处都找遍了,但什么也没找到。如果有人能帮助我,我将非常感激。

在Unity中使用移动触摸输入的无限跑者

我知道的两个简单方法:

当玩家点击/触摸屏幕时检测触摸区域:

c#

if (Input.GetMouseButtonDown(0))
{                
    var touchArea= new Rect(Screen.width/2, 0, Screen.width/2, Screen.height);
    if (touchArea.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
    {
        Jump();
    }
}

或者创建一个巨大的不可见的按钮,占屏幕的一半:

c#

var touchArea = new Rect(Screen.width/2, 0, Screen.width/2, Screen.height);
if (GUI.Button(touchArea, "", new GUIStyle()))
{
    Jump();
}

注意:Input.GetMouseButtonDown(0)适用于鼠标点击和触摸(android/iphone)。