Unity关于手游端摇杆移动、摇杆按钮冲突问题
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、摇杆移动问题
1.问题重现
①摇杆移动时,通过控制角色的刚体运动。
②当摇杆移动到游戏区域外松开时,后面对摇杆进行操作无响应。
③使用的是Drag事件来写的拖动逻辑。
2、解决
解决思路:摇杆控制角色移动时,不能通过控制角色的刚体移动,而要通过改变角色的位置来移动。
(有BUG)刚体移动代码如下(hor、ver为获取了摇杆移动的方向,判断设置了区间-1,1):
direction.x = hor;
direction.y = ver * 0.7f;
rig.velocity = direction * moveSpeed * 50 * Time.deltaTime;
if (rig.velocity.x >= 0.05)
{
transform.rotation = new Quaternion(0, 0, 0, 0);
}
else if (rig.velocity.x <= -0.05)
{
transform.rotation = new Quaternion(0, 180, 0, 0);
}
(可用)使用Translate的移动代码如下:
direction.x = hor;
direction.y = ver * 0.7f;
player.transform.Translate(direction*moveSpeed*Time.deltaTime);
(可用)最后附上完整代码:
private float moveSpeed;//移动速度
private Vector2 direction;//移动方向
private PlayerBehaviour behaviour;//角色行为
// public Transform Yingzi;//影子
public Transform big; //摇杆背景
public Transform small;//摇杆中心
private float radius = 250;//摇杆半径
private Vector2 moveCenter;//移动中心
private Vector2 mouseToCenterVect;//鼠标移动到中心的向量
private float mouseToCenterDistance;//鼠标到中心点的距离
private float hor;//获取水平方向
private float ver;//获取垂直方向
public Transform player;//主角
// Start is called before the first frame update
void Start()
{
behaviour = player.GetComponent<PlayerBehaviour>();
}
/// <summary>
/// 开始拖动时
/// </summary>
public void