首页 > 解决方案 > 如何在 Unity Left-Right Touch Controls 中制作?

问题描述

我正在尝试在 Unity 中制作简单的左右控件,但没有成功......我想将屏幕“拆分”成两个区域,如下图所示,所以当您按下右半部分时,角色会转到右半部分。 。 我希望有一个人可以帮助我。

它应该像这样工作: 我多么想要

标签: unity3dcontrolstouch

解决方案


这取决于您所针对的平台。您将需要以像素为单位获取屏幕宽度。

float screenWidth = Screen.width 

对于 Mobile,您将在 update 方法中执行以下操作。

Touch touch = Input.GetTouch(0);
if(touch.position.x > (screenWidth/2))
 {
   //The User has touched on the right side of the screen
 }else
{
 //The user hase touched the left side of the screen 
}

要为非移动平台获取此信息,只需使用 Input.mouseposition 而不是 Input.GetTouch。至于您是否还想知道如何左右移动玩家,那应该是一个不同的问题。因为运动基于 Unity 中的许多不同变量。(即 2D、3D、现实世界物理......等)。因此,如果您还需要帮助,请参阅此链接,该链接将解释一些简单的移动脚本


推荐阅读